I use a WsdlImporter and ServiceContractGenerator to set up CodeDomProvider to get an assembly which (I thought) should allow me to create an instance of my HelloWorldService.
MetadataExchangeClient mexClient = new MetadataExchangeClient(metadataAddress);
mexClient.ResolveMetadataReferences = true;
MetadataSet metaDocs = mexClient.GetMetadata();
WsdlImporter importer = new WsdlImporter(metaDocs);
ServiceContractGenerator generator = new ServiceContractGenerator();
System.Collections.ObjectModel.Collection<ContractDescription> contracts
= importer.ImportAllContracts();
importer.ImportAllEndpoints();
foreach (ContractDescription contract in contracts)
{
generator.GenerateServiceContractType(contract);
}
if (generator.Errors.Count != 0)
{
throw new Exception("There were errors during code compilation.");
}
CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");
CompilerParameters parameters = new CompilerParameters();
parameters.CompilerOptions = string.Format(@" /lib:{0}", "\"C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0\"");
parameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
parameters.ReferencedAssemblies.Add("System.Runtime.Serialization.dll");
CodeCompileUnit codeUnit = generator.TargetCompileUnit;
CompilerResults results = codeDomProvider.CompileAssemblyFromDom(parameters, codeUnit);
foreach (CompilerError oops in results.Errors)
{
throw new Exception("Compilation Error Creating Assembly: " + oops.ErrorText);
}
return results.CompiledAssembly;
assembly.GetExportedTypes() and assembly.GetTypes() return five types: IHelloWorldService, IHelloWorldServiceChannel, HelloWorldServiceClient, and the two DataContract request and response types.
I was expecting to be able to use assembly.CreateInstance("HelloWorldService"); to get an instance of the service to be able to invoke methods on it, but of course this is null.
What am I missing? Any ideas greatly appreciated. Let me know if you need more detail.