assembly.GetExportedTypes()不包含WCF服务

时间:2022-08-10 15:17:19

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.

我使用WsdlImporter和ServiceContractGenerator来设置CodeDomProvider以获得一个程序集(我认为)应该允许我创建一个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.

assembly.GetExportedTypes()和assembly.GetTypes()返回五种类型:IHelloWorldService,IHelloWorldServiceChannel,HelloWorldServiceClient和两个DataContract请求和响应类型。

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.

我原以为能够使用assembly.CreateInstance(“HelloWorldService”);获取服务的实例以便能够在其上调用方法,但当然这是null。

What am I missing? Any ideas greatly appreciated. Let me know if you need more detail.

我错过了什么?任何想法都非常感激。如果您需要更多细节,请与我们联系。

1 个解决方案

#1


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.

这些技术用于创建客户端类。当您创建HelloWorldServiceClient的实例时,您将创建可用于与服务通信的代理类的实例。

#1


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.

这些技术用于创建客户端类。当您创建HelloWorldServiceClient的实例时,您将创建可用于与服务通信的代理类的实例。