Thursday, 22 January 2009

Building SOAP Web-Service Clients in .NET 2.0 - Notes To Self ...

This post is a few notes to help me next time I come across issues building a web-service client ...

Some often used code snippets:

For completeness, init the client proxy:

If you are using WSE: ServiceWse svc = new ServiceWse();

Else: Service svc = new Service();

Set the web-proxy to null, so as to bypass a firewall:

svc.Proxy = new System.Net.WebProxy();

Set the version of SOAP - by default .NET uses 1.1 - incase you need another version, you can do this:

svc.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;

Code to use if your client needs to use UsernameToken authentication (WSE 3.0):

Microsoft.Web.Services3.Security.Tokens.UsernameToken usrToken = new Microsoft.Web.Services3.Security.Tokens.UsernameToken("UID", "PW", Microsoft.Web.Services3.Security.Tokens.PasswordOption.SendHashed); svc.RequestSoapContext.Security.Tokens.Add(usrToken);

Code to use if you need to bypass an invalid certificate:

System.Net.ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(myCertificateValidation); ... public bool myCertificateValidation(Object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors Errors) { return true; //You should check the cert yourself here }

This all relates back to a couple of previous posts that I have made on this matter:

http://bernard-on-technology.blogspot.com/2007/12/how-to-change-version-of-http-that-your.html
http://bernard-on-technology.blogspot.com/2008/02/add-or-remove-web-service-protocols-at.html

Error messages when instantiating the generated client proxy, such as:


WCF Service Reference: "The XML element 'BlahResponse' from namespace 'http://namespace' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute."

.NET 2.0 Web Reference: "Method webServiceClient.Blah can not be reflected."

Can be from any number of issues. However, first port of call should be to pick a function that you are confident works on the web-service (use soapUI to establish this), then comment out the code in Reference.cs (or Reference.vb) for all other web-methods. Retry instantiating the client proxy with only the web-method that works implemented. It could be that you have some web-methods in there that are not properly implemented at the web-server and are causing a bit of chaos in the generated client code.

Error messages in response to a request to the web-service:

In your SOAP envelope response (received from a Java web-service): "Error reading XMLStreamReader."

In this case, check the protocol versions for HTTP and SOAP that your client is using. You will definitely get this message if your SOAP version is not the same as that of the service, and I think the message is similar if the HTTP version is different(?).

Diagnosis tools to use:

Fiddler: for diagnosis of HTTP traffic - picks up incoming and outgoing XML - http://www.fiddlertool.com/fiddler/

soapUI: highly configurable test client for web-services - http://www.soapui.org/

SoapExtension class: allows you to capture incoming and outgoing SOAP traffic - http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension.aspx

No comments:

Post a Comment