如何使用反射调用WCF服务?

时间:2012-07-19 20:31:21

标签: c# wcf wcf-binding

我有WCF服务端点;我无法访问该服务的接口(即合同)。我有我需要调用的EndPoint详细信息和MethodName。

我怎样才能在C#中做到这一点;我正在使用netTcpBinding ...

提前致谢

海洋

1 个答案:

答案 0 :(得分:3)

不确定这是否正是您想要的,但您可以使用WsdlImporter联系WSDL并获取元数据(如DataContracts和OperationContracts)。例如:

        MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;

        // Get Metadata file from service
        MetadataExchangeClient mexClient = new MetadataExchangeClient(mexAddress, mexMode);
        mexClient.ResolveMetadataReferences = true;
        MetadataSet metaSet = mexClient.GetMetadata(mexAddress, mexMode);

        //Import all contracts and endpoints
        WsdlImporter importer = new WsdlImporter(metaSet);
        var contracts = importer.ImportAllContracts();
        ServiceEndpointCollection allEndpoints = importer.ImportAllEndpoints();

        //Generate type information for each contract
        ServiceContractGenerator generator = new ServiceContractGenerator();
        this.EndpointsForContracts = new Dictionary<string, IEnumerable<ServiceEndpoint>>();

        foreach (ContractDescription contract in contracts)
        {
            generator.GenerateServiceContractType(contract);
            // Inspect if the name matches one you're looking for, or do whatever
            //  else you might need
        }

然后你可以动态地将代码编译到内存中:

// Generate a code file for the contracts 
        CodeGeneratorOptions options = new CodeGeneratorOptions();
        options.BracingStyle = "C";
        CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");

        // Compile the code file to an in-memory assembly
        // Don't forget to add all WCF-related assemblies as references
        CompilerParameters compilerParameters = new CompilerParameters(
            new string[] { 
                "System.dll", "System.ServiceModel.dll", 
                "System.Runtime.Serialization.dll" });
        compilerParameters.GenerateInMemory = true;

        CompilerResults results = codeDomProvider.CompileAssemblyFromDom(compilerParameters, generator.TargetCompileUnit);

最后,您可以相应地执行这些方法:

Type clientProxyType = this.Cr.CompiledAssembly.GetTypes().First(t => t.IsClass && t.GetInterface(listServiceContracts.SelectedValue) != null && t.GetInterface(typeof(ICommunicationObject).Name) != null);
object dataContract = this.Cr.CompiledAssembly.CreateInstance(dataContractType.FullName, false, System.Reflection.BindingFlags.CreateInstance, null, null, CultureInfo.CurrentCulture, null);

// set the dataContract properties however they need to be set

// Get the first service endpoint for the contract
        ServiceEndpoint serviceEndPoint = this.EndpointsForContracts[listServiceContracts.SelectedValue].First();

        object clientProxyInstance = this.Cr.CompiledAssembly.CreateInstance(clientProxyType.Name, false, System.Reflection.BindingFlags.CreateInstance, null, new object[] { serviceEndPoint.Binding, serviceEndPoint.Address }, CultureInfo.CurrentCulture, null);

        Type myType = clientProxyInstance.GetType();
        object[] arg = { dataContract };

        // Now call the remote procedure via SOAP, get back response
        var returnVal = myType.InvokeMember("OPERATION YOU WANT TO EXECUTE", BindingFlags.InvokeMethod, null, clientProxyInstance, arg);

同样,这可能不适合你想要做的事情,你的帖子很久以前这可能是旧闻。但为了以防万一...希望这是有帮助的。您可能需要检查返回的类型,以确定如何正确设置DataContract参数。此外,上面假设您调用的方法只接受一个参数,即DataContract对象。

希望如果你还需要它,这会让你走上正轨。