tags:

views:

135

answers:

1

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.

+1  A: 

These techniques are used to create client-side classes. When you create an instance of HelloWorldServiceClient, you'll be creating an instance of the proxy class that can be used to communicate with the service.

John Saunders
Right. Currently I can't create an instance of it because I get a missing endpoint/config type error, which makes sense but is annoying. Thanks-- that gives me something to follow up with.
Donnelle