Monday, July 23, 2007

How to do HttpPost with .NET Web Service?

What kind of Web Service binding does .NET create for you when you use "Add Web Reference"? Right, it is Soap and Soap2, and from the wizard we have no control over it. Note however that Service Help Page does use HTTP POST even if it is not enabled by the underlying . Don’t let this fact confuse you: your auto generated WSDL won’t have the HTTP-POST binding.

So if by some reason I want my my .NET Web Service to use HTTP POST, how would I do it? Read on!

Service Side: tell Web Service to support it and expose it in web.config.

<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="Documentation"/>
<add name="HttpPostLocalhost"/>
Now your WSDL has the HTTP-POST binding, and can generate HTTP-POST stubs.

Client Side: tell WS client to use it. That means, client proxy should inherit from HttpPostClientProtocol rather then SoapHttpClientProtocol. Add Web Reference doesn’t give a control of what always inserts. Instead, use wsdl /protocol:HttpPost. What I did:
1) Generate another proxy with wsdl (in command line, wsdl /out:ReferenceHttpPost.cs /protocol:HttpPost Service1.wsdl).
2) Add it to my project.
3) Extract interface from Web Reference auto-generated file (reference.cs). Danger! Refresh Reference will kill the change. So I better rename it to something like ReferenceSoap.cs, and update the project to use it.
4) Create a factory that returns either SOAP or HTTP-POST based proxy.
5) Make WS client use the factory to get a desired proxy.

Tip 1. The proxy generated by wsdl will have URL hardcoded in a constructor, while the Web Reference one knows to use the URL from config file. Copy-paste constructor code from Web Reference generated so that they are the same. Something like
public Service1() {
this.Url = global::WinClient.Properties.Settings.Default.

Tip 2. If running wsdl /protocol:HttpPost generates Warning: no class generated, it simply means wsdl misses POST binding. See Service Side.


AddThis Social Bookmark Button


Post a Comment

<< Home