Mike V.

Good afternoon,

I am trying to write code that takes metadata from a running WCF service (knowing only the service's address), and using that metadata (after running it through a wsdlImporter and xsdDataContractImporter) to compile an assembly that (hopefully) contains the data contract object used by the web service. However, when the code reaches the "CompileAssemblyFromDOM stage, the compilation fails, leaving the message "Metadata file 'System.Runtime.Serialization.dll' could not be found". It's really rather annoying, since the file in question is one of Microsoft's (and in the GAC, for that matter).

The code I've been using is as follows:

System.ServiceModel.WSHttpBinding binding = new WSHttpBinding();
MetadataExchangeClient serviceData = new MetadataExchangeClient(binding);
WsdlImporter importer = null;
//Imports the metadata into a structure
importer = new WsdlImporter(serviceData.GetMetadata(new Uri("http://localhost:10000/someService wsdl"),
 System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet));
importer.ImportAllContracts();
//Now import the data as an xsd schema
System.Runtime.Serialization.XsdDataContractImporter dataImporter = new System.Runtime.Serialization.XsdDataContractImporter();
dataImporter.Import(importer.XmlSchemas);
//Next, we take the schema and push it into dynamically-generated assembly
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.CompilerParameters compilerOptions = new System.CodeDom.Compiler.CompilerParameters();
compilerOptions.GenerateExecutable = false;
compilerOptions.GenerateInMemory = true;
compilerOptions.OutputAssembly = "";
compilerOptions.IncludeDebugInformation = false;
compilerOptions.ReferencedAssemblies.Add("System.Runtime.Serialization.dll");
System.CodeDom.Compiler.CompilerResults cResults = provider.CompileAssemblyFromDom(new System.CodeDom.Compiler.CompilerParameters(), dataImporter.CodeCompileUnit);

After that last line of code processes, the CompilerResults.Errors property contains 1 item, the error message I've referenced above.

I appreciate any assistance out there, and apologize in advance if this is the wrong forum for the question (it seemed to be this or the WCF forum, but since I think the problem relates to the CodeDom, this appeared to be the right place).

-Michael Vidal

 



Re: .NET Base Class Library "Metadata file 'System.Runtime.Serialization.dll' could not be found" during dynamic compilation

Ronald Ricardo Ramirez Moran

You need to specify the full path or the fullname of System.Runtime.Serialization assembly.

A Good form to make it, is to obtaining the fullname using a common type from it as below:

compilerOptions.ReferencedAssemblies.Add(typeof(DataContractSerializer).Assembly.FullName);

or if you know the correct place of the dll (as I Wink):

string dllPlace = @"%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll";
compilerOptions.ReferencedAssemblies.Add(dllPlace
);

Best Regards,






Re: .NET Base Class Library "Metadata file 'System.Runtime.Serialization.dll' could not be found" during dynamic compilation

Mike V.

I had actually tried using the full path to the assembly, but just to be sure, I tried it again. Neither method (using the assembly reference or the full path) changes the error message.

I did actually find a way to make it work. Based on the suggestion, I went back and changed the reference in the CodeCompileUnit.ReferencedAssemblies property to the full path of the dll, and lo and behold, it worked. It only works that way with the full path, not with the type reference:

dataImporter.CodeCompileUnit.ReferencedAssemblies[0] = @"C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll";

(It also only requires this line...it works even without the assembly reference in the compiler options.)

This is rather bothersome, though, since that solution requires that I either know which index the referenced DLL will be in (debugging provided the information, but I see no guarantee that the DLL will be in that index for different data, and while I could test each member of CodeCompileUnit.ReferenceAssemblies for "System.Runtime.Serialization.dll", should this problem appear with other DLLs that happen to be referenced, it seems like they would have to be hard-coded like this one.

Am I just missing something I like that my code is working now, but the implementation doesn't feel right.





Re: .NET Base Class Library "Metadata file 'System.Runtime.Serialization.dll' could not be found" during dynamic compilation

xd

I am having a similar problem, have tried using both type name and full path with no success. could you please elaborate how this is happening thanks!