Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用反射调用WCF服务?_C#_Wcf_Wcf Binding - Fatal编程技术网

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

C# 如何使用反射调用WCF服务?,c#,wcf,wcf-binding,C#,Wcf,Wcf Binding,我有WCF服务端点;我无权访问该服务的接口(即合同)。我需要调用它的端点详细信息和方法名 我怎样才能在C#中做到这一点;我正在使用NetCPBinding 提前谢谢 Ocean不确定这是否正是您想要的,但是您可以使用WsdlImporter联系WSDL并获取元数据(如DataContracts和OperationContracts)。例如: MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGe

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

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

提前谢谢


Ocean不确定这是否正是您想要的,但是您可以使用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
        }
最后,您可以相应地执行这些方法:

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对象


如果您仍然需要它,希望这能让您走上正确的轨道。

您是在尝试连接到任意服务还是单个已知服务?如果您有程序集,您可以简单地引用程序集并实例化您正在谈论的类型,而无需进行反射。请解释为什么你不能这样做,以便有人能提供一些建议。这里有一个答案可能会有所帮助:我不会事先知道这项服务;我将无法访问程序集,因此无法实例化。是的,这将是任意的服务。而且我也没有合同。。。
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);