As the title suggests, I am getting a FileNotFoundException when running a web page (ASP.NET MVC 2 project) that references a Silverlight class library. When a method on one of the classes from the Silverlight library is called, I get the following error:
Could not load file or assembly ‘System.Xml.Linq, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.
Ok, so a bit of an explanation about my setup. I have three projects; one is a Silverlight class library (using Silverlight 4) and the others are an ASP.NET MVC 2 web project, and a Silverlight project that has a Silverlight control that is hosted in the web project. I have referenced the Silverlight library in the web project and the Silverlight project (the one with the control). Within the SL control, I instantiate a class from the SL library, and call one of the class’s methods. That method contains calls to the System.Xml.Linq library classes. The FileNotFoundException occurs when calling that method, but not when instantiating the class.
Due to the nature of this project, I am unable to enclose any of the code I am using, but here is some arbitrary code to help illustrate the above explanation:
Class in Silverlight library
public class XmlClass
{
public void Execute()
{
// Calls to System.Xml.Linq classes
}
}
Silverlight control
public class SLControl : UserControl
{
// ...
private void SomeObject_SomeEvent(object sender, EventArgs e)
{
// Instantiate class
XmlClass xmlClass = new XmlClass();
// Execute
xmlClass.Execute(); // <-- Error occurs here.
}
}
Now the even weirder thing is; I had this error at another point in the project. In this other point, I had a standard .NET class library referencing the SL library and using the same calls as the SL control. I hit the error in the same place, and worked out that when the code was run, it was trying to look in the GAC for the relevant System.Xml.Linq assembly. As such, I installed it into the GAC and this fixed my issue.
So, I’m a little bit lost now, since the assembly is still in the GAC and the project that originally had the issue still works.
Any ideas on this?
Thanks to the post by Mehmet Aras, I have managed to work out a solution to my issue.
Basically, the issue seems to have arisen as a result of the referenced System.Xml.Linq assembly being different for both the Silverlight control and Silverlight class libraries. When the Silverlight projects were created, they contained a reference to the assembly that was found in the “C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\”. However, some when during the changing around of the references to fix the issue originally, I ended up referencing the same assembly but found in “C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\”. When I did this again for the project in error, it solved the issue.
So from that, I’m assuming that I had actually just referenced different assemblies. This has also lead me to believe that the assembly that is referenced when the project is created, is not the right one to use.