views:

417

answers:

2

I have a Silverlight Class Library that is being used by both Silverlight application and a regular C# WCF Service.

The Silverlight application calls the WCF service to read/write some data. They both use the common library to manipulate the data being passed.

Everything compiles fine, but when we run the application, the webservice throws the following error when the call to the silverlight library is made:

"Could not load file or assembly 'System.Xml, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified."

This is because the silverlight class library is referencing v2.0.5 of System.Xml, but the WCF Service is referencing v3.5 of System.Xml.

Is there a way I can reference both versions and not get the error?

+2  A: 

No this is not supported in the CLR (without a good deal of hacking). The reason why is because of a fundamental limitation of the CLR. Namely that one and only one mscorlib can be loaded into an instance of the CLR.

If you have 2 versions of System.Xml.dll, the will reference 2 different versions of mscorlib. This is especially true for a Silverlight and non-Silverlight project which have radically different mscorlib and BCL DLL's. Therefore when you try to load the second System.Xml DLL, it will eventually try and load the different mscorlib which is bound to fail.

The reason I added the "without a good deal of hacking" caveat is binding redirection. I'm sure there's some lovely binding magic you could insert into app.config which would redirect the Silverlight System.Xml to the full System.Xml to get it to functionally load. However this would almost certainly lead to worse off errors down the road as the program executed.

JaredPar
Isn't this coming to .NET 4.0?
ParmesanCodice
This doesn't make sense. Silverlight runs on the client in the Silverlight plug-in (moonlight) whereas the WCF service executes on the server. There is no reuse of the same CLR here. If anything this is a service contract and datatype marshalling issue.
Jeff Yates
@Jeff, how does this not make sense? The user says they are getting an error trying to load a second System.Xml into their process and the version number clearly indicates it's the Silverlight version. I agree the scenario is odd but I have to go with what the OP is saying the symptoms are. I believe my analysis of the scenario described by the OP is 100% correct. Not sure why I'm getting the downvote here.
JaredPar
+1. This answer has this issue nailed. The only solution I've seen for this sort of thing is compile the code twice. Once with the set of SL references to generate a downloadable assembly for the clients use and another with the standard set of references for the servers consumption
AnthonyWJones
@ParmesanCodice: I think you are refering to the ability for a __process__ to host multiple AppDomains that are of different CLR versions. Previously all AppDomains in a process would have to use the same CLR version. The issue in question would remain.
AnthonyWJones
@Jared: I actually agree with you (even before you replied) but SO won't let me remove my downvote unless you edit. Sorry about that.
Jeff Yates
@AnthonyWJones Thanks
ParmesanCodice
@Jeff, no worries. I'm at rep cap today so it won't make a difference.
JaredPar
+2  A: 

Providing you have the source for the common library then the best approach is to have 3 projects, once for SL, one for WCF and one for the shared library source. You then can reference the source files from the shared lib in the SL and WCF projects using visual studio's add as link option. The source files can then be compiled against the correct .Net library versions. The nice thing about this is due to the source being reference copies, when you make changes to the shared lib, both the SL and WCF projects get updated without any duplication.

We've been using this approach in our product and it works very well.

HTH

Andy Britcliffe
Thanks Andy, That solved our issue.
Zak