我需要调用 Web 服务上的操作,但我不知道请求信封会是什么样子(服务在运行时由用户附加)。
通常,我希望基于 wsdl 链接以编程方式生成 soap 信封。使用给定的链接获取操作列表和特定操作的结果:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:webservice.contentinn.com">
<soapenv:Header>
<urn:AuthHeaderElement>
<token></company>
</urn:AuthHeaderElement>
</soapenv:Header>
<soapenv:Body>
<urn:TestMethod>
<id></id>
</urn:TestMethod>
</soapenv:Body>
</soapenv:Envelope>
有人知道怎么做吗?
最佳答案
Answers to this question提出了几种方法:
- SoapUI : 这不是真正的程序化方法。
- Castle Dynamic Proxy :这更接近您所需要的,但还不够。
The example here可能是您想要的:
The DynamicProxy allows you to create the dynamic WCF client at runtime by specifying the WSDL URI of the service. The DynamicProxy does not depend on the precompiled proxy or configuration. The DynamicProxy uses the MetadataResolver to download the metadata from the service and WsdlImporter to create the contract and binding at runtime. The compiled dynamic proxy can be used to invoke the operations on the service using reflection.
The example shows how you can the dynamic proxy to invoke operations that use simple types and complex types. The flow of usage is as following.
Create the ProxyFactory specifying the WSDL URI of the service.
DynamicProxyFactory factory = new DynamicProxyFactory("http://localhost:8080/WcfSamples/DynamicProxy?wsdl");
Browse the endpoints, metadata, contracts etc.
factory.Endpoints factory.Metadata factory.Contracts factory.Bindings
- Create DynamicProxy to an endpoint by specifying either the endpoint or contract name.
DynamicProxy proxy = factory.CreateProxy("ISimpleCalculator");
OR
DynamicProxy proxy = factory.CreateProxy(endpoint);
- Invoke operations on the DynamicProxy
double result = (dobule)proxy.CallMethod("Add", 1d ,2d);
- Close the DynamicProxy
proxy.Close();
To run the example: Compile the solution, run the CalculatorService.exe and then run the CalculatorDynamicClient.exe
关于c# - 如何从 C# 中的 wsdl SOAP 请求信封,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6292859/