Search code examples
c#javaweb-servicesweb-configwcf-binding

Consume a Java Web Service through a C# Service Reference


There are many questions about how to do this using a Web Reference in C#, but I know how to do that. What I'm trying to accomplish is to have a portable dll that consumes the Java Web Services that I can reference in my projects instead of duplicating the functionality. One of the things is that with Web References the KeepAlive of the request is set to true. That doesn't work for the environment I'm developing in and it has to be false. What I did with Web References is create an abstract class that inherits SoapHttpClientProtocol and change the Reference.cs to inherit from the abstract class. The abstract class looked like this:

[System.Web.Services.WebServiceBinding(Name = "JavaWebReferenceProxy")]
public abstract class JavaWebReferenceProxy : SoapHttpClientProtocol
{
    public JavaWebReferenceProxy()
    {
        base.Timeout = Settings.Instance.SoapTimeout;
    }

    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest rq = base.GetWebRequest(uri);
        ((HttpWebRequest)rq).KeepAlive = Settings.Instance.SoapKeepAlive;

        return rq;
    }
}

This allowed me to override the GetWebRequest and the constructor to set values that were in the web.config. I'm wondering how I can do this with a Service Reference to the Java Web Service. I've found some examples for the simplified serviceModel section in 4.0 (which is the framework I'm using), but I need to specify the url and the timeout as well. I'm not sure if I can use the simplified serviceModel or if I need to use the full implementation. I also am unsure if I can use the Service Reference at all. I'm just looking for a little guidance if anyone has implemented something like this.

What should the serviceModel section look like in my config file? The 3 things I need to be able to specify is the URL, the Timeout and the keep-alive. Thanks in advance.


Solution

  • You need custom binding for that. Try this:

    <system.serviceModel>
      <bindings>
        <customBinding>
          <binding name="myBinding" sendTimout="00:05:00">
            <textMessageEncoding messageVersion="Soap11" />
            <httpTransport keepAliveEnabled="false" />
          </binding>
        </customBinding>
      </bindings>
      <client>
        <endpoint name="myEndpointName" address="http://..." binding="customBinding" 
                  bindingConfiguration="myBinding" contract="MyReference.IMyService" />
      </client>
    </system.serviceModel>
    

    SendTimeout should set timeout for operation completion (including receiving response), keepAliveEnebled controls persistent HTTP connection and address in edpoint is address of the service.