开发者

FileNotFoundException when referencing Silverlight project from ASP.NET project

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 开发者_Go百科the issue still works.

Any ideas on this?


.NET framework and the runtime for Silverlight is a subset of the full .NET framework. That's why there is a separate project template called Silverlight class library that targets .NET Framework version for Silverlight. Silverlight assemblies such as your Silverlight class library cannot be used by other .NET projects like your ASP.NET MVC applications intented for the full .NET framework. The Silverlight assemblies are not portable. This is the problem you appear to be running into.

There is an effort to increase the portability of Silverlight assemblies so that they can be shared with other .NET applications. You can read more about this in Sharing Silverlight Assemblies with .NET Apps. If you limit the assemblies you reference in your Silverlight class library to those core ones listed as portable and binary compatible in the blogpost, then you should be able to use your Silverlight class library project in ASP.NET MVC application.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜