2

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?

BenMorel
  • 30,280
  • 40
  • 163
  • 285
eye
  • 304
  • 5
  • 22
  • RE: the services are added by users at runtime... Do you hope to invoke functions automatically? e.g. Is this some type of test harness/interface? Let's say that Service 1 accepts Lat/Lon and returns weather information. Now someone adds a Service2 that also accepts Lat/Lon. Do you want to go ahead and invoke it? (As it turns out, Service2 dumps a bucket of paint on that location.) My point is that aside from test scenarios, you might not want to automatically invoke some new service. And if that's the case, you may not need to generate the proxy dynamically. – Mark Maslar Jun 09 '11 at 14:39

2 Answers2

1

Answers to this question suggests a couple of approaches:

  • SoapUI: This is not really a programmatic approach.
  • Castle Dynamic Proxy: This is closer to what you sound like you need, but still not quite there.
  • 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.

    1. Create the ProxyFactory specifying the WSDL URI of the service.

      DynamicProxyFactory factory = new DynamicProxyFactory("http://localhost:8080/WcfSamples/DynamicProxy?wsdl");

    2. Browse the endpoints, metadata, contracts etc.

    factory.Endpoints factory.Metadata factory.Contracts factory.Bindings
    
    1. Create DynamicProxy to an endpoint by specifying either the endpoint or contract name.
    DynamicProxy proxy = factory.CreateProxy("ISimpleCalculator");
    

    OR

    DynamicProxy proxy = factory.CreateProxy(endpoint); 
    
    1. Invoke operations on the DynamicProxy
    double result = (dobule)proxy.CallMethod("Add", 1d ,2d);
    
    1. Close the DynamicProxy
    proxy.Close();
    

    To run the example: Compile the solution, run the CalculatorService.exe and then run the CalculatorDynamicClient.exe

  • There is a Java example here, too.

Community
  • 1
  • 1
Peter K.
  • 7,720
  • 4
  • 45
  • 69
1

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

  1. You could add a Service Reference to your project
  2. Run SVCUTIL: svcutil http://someurl?wsdl

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.

Mark Maslar
  • 1,063
  • 4
  • 16
  • 27