Reputation: 304
I need to invoke action on a web service, but i have no idea what the request envelope will look like (the services are attached at runtime by users).
Generally I'd like to generate soap envelope programmatically based on wsdl link. With given link get list of operation and such result for specific one:
<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>
Anyone have idea how to do that?
Upvotes: 2
Views: 5397
Reputation: 8108
Answers to this question suggests a couple of approaches:
The example here is probably what you're after:
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
Upvotes: 1
Reputation: 1121
You'll need to generate a proxy class; that'll generate everything needed for invoking the service's actions.
There are several ways to generate the proxy class
Once the proxy class is generated, it'll expose the service's actions as methods. Just invoke the desired method and the SOAP envelope will be generated for you.
Upvotes: 1