Thursday, 19 April 2007

Logging SOAP messages in .NET Web Services

C# Web Services provide an easy interface to incoming SOAP data because the SOAP message has already been deserialised at the entry point of a WebMethod.

The downside is that sometimes you'll need to have access to the full SOAP request: body, headers and everything for tasks such as diagnosing any errors (which message broke the web service) or providing graceful handling of third party web services.

At first you'd think that the Request object can provide this information with methods such as Request.BinaryRead() but unfortunately when you're using SOAP all you get there is a querystring.

The solution here is to use the SOAP Extensions. Here is some sample code:

public class SoapMessageLogger : SoapExtension
{
//...
public override void ProcessMessage(SoapMessage message)
{
switch(message.Stage)
{
case SoapMessageStage.BeforeDeserialize:
LogResponse(message); break;
case SoapMessageStage.AfterSerialize:
LogResponse(message); break;

// Do nothing on other states
case SoapMessageStage.AfterDeserialize;
case SoapMessageStage.BeforeSerialize;
default: break;
}
}
//...
}

More info on this can be found here and here.

Friday, 30 March 2007

Using WFetch to debug your HTTP connection

This is a really handy tool for debugging http request and response data if you wish to go a bit lower level than pressing F5 on the browser.

You have the ability to create custom header and body request items which is also perfect for tuning your SOAP messages.

Thursday, 29 March 2007

Using session state in an .asmx web service

You are building an ASP.NET web service and wish to expose some functionality that relies on session state information. A standard .aspx page will handle session state normally but web services do not have acess to the same session data as the rest of your applicaiton.

The trick is that in order to access session state information within a web method you must enable session state management explicitly in your .asmx file as follows:

[WebMethod(EnableSession = true)]

By specifying the EnableSession value you will have a managed session to play with. If you dont set this value the Session object will be null and you will encounter an object reference exception when trying to access the Session object from either this.Session or System.Web.HttpContext.Current.Session.

Friday, 23 March 2007

Quickly setting up SSL under IIS with SelfSSL

Its fairly easy to setup https functionality when you're running an Apache web server however when trying to do the same thing in IIS you will encounter the problem of generating a certificate and having to rely on a Certification Authority such as Verisign or your own certification server to have the certificate authorised.

This problem is easily solved with Microsoft SelfSSL utility, which can be downloaded here. It will only install under XP or Windows 2003 however you can copy the SelfSSL.exe file directly into a 2000 computer and run it without a problem.

A typical command option you can run (this will give you a certificate valid for 120 days) is:

selfssl.exe /N:CN=(computername) /K:1024 /V:120 /S:1

And thats it! This will take care of the certificate generation and installation. Watch out if you are running multiple Web Sites under the one server as you will have to fiddle with the /s tag to point the SSL certificate in the right direction.