Archive for the 'WCF' Category

Web Service Test Client

Today, I have to work on WCF web service test client. I need to develop a client which can be used to test our WCF web service. This tool should provide beautiful graphical interface. QA department need this tool to test our service easily. I searched on internet and found many tools. Many of them are commercial, some tools are free. I shortlisted two tools which are available on codeplex. These are wizdl (https://wizdl.codeplex.com/) and Storm (http://storm.codeplex.com/). These tools are good for web service testing  but I like Storm. Its look is very profession and very easy to use. I highly recommend this tool if you want to test your web service.

 

 

ELMAH With WCF Web Services

After a long time, I again started  Asp. Net job. On this job, I faced first challenge which is to log and monitor all WCF web services un-handled exceptions. For logging, I used ELMAH (Error Logging Modules and HTTP handlers) for Logging all the applicaion exception. This is open source (https://code.google.com/p/elmah/) and can be easily integrated with any Asp.Net web applications. Lets started with simple steps that I used to integrate it with WCF. These are

  1. Download it from https://code.google.com/p/elmah/wiki/Downloads?tm=2 according to your architecture.
  2. Add ELMAH.dll to your project by clicking Add Reference (use .net 1.1 dll, you will find it inside bin folder).
  3. Add the following configuration inside your web.config or app.config, these are

 

<sectionGroup name=”elmah”>

<section name=”errorLog” requirePermission=”false” type=”Elmah.ErrorLogSectionHandler,Elmah”/>
<section name=”security” requirePermission=”false” type=”Elmah.SecuritySectionHandler, Elmah”/>

</sectionGroup>

 

Add above configuration inside <configSections>, if does not exists, create it inside configuration tag.

 

<elmah>
<errorLog type=”Elmah.SqlErrorLog, Elmah” connectionString=”your connection sring” />

</elmah>

 

Add above lines below <configSections>, I used SQL server for logging, you can use XML , SQLlite etc. You can change this setting according to your data source.

 

<httpModules>

<add name=”ErrorLog” type=”Elmah.ErrorLogModule, Elmah”/>
<add name=”ErrorFilter” type=”Elmah.ErrorFilterModule, Elmah”/>

</httpModules>

 

Add the above lines, inside <system.web> tag.

 

<serviceHostingEnvironment multipleSiteBindingsEnabled=”true” aspNetCompatibilityEnabled=”true” />

 

Add above line inside <system.serviceModel>

 

<location path=”elmah.axd”>

<system.web>

<httpHandlers>

<add verb=”POST,GET,HEAD” path=”elmah.axd” type=”Elmah.ErrorLogPageFactory, Elmah” />

</httpHandlers>

</system.web>

</location>

 

Add above lines below <system.webServer> tag. These lines will help us to view our log in the browser. elmah.axd will serve the log inside a grid when we open it in browser.

 

4. Create ErrorHandler class and paste this code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Dispatcher;

namespace YourNameSpace
{
public class ErrorHandler : IErrorHandler
{
#region IErrorHandler Members
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error,
System.ServiceModel.Channels.MessageVersion version,
ref System.ServiceModel.Channels.Message fault)
{
if (error == null)
return;
if (HttpContext.Current == null) //In case we run outside of IIS
return;
Elmah.ErrorSignal.FromCurrentContext().Raise(error);
}
#endregion
}
}

 

This class will caught all exceptions and manually triger the Elmah error signal module so that it can be logged.

 

5. Add ServiceErrorBehaviorAttribute class and paste the below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace YourNameSpace
{
public class ServiceErrorBehaviorAttribute : Attribute, IServiceBehavior
{
Type errorHandlerType;
public ServiceErrorBehaviorAttribute(Type errorHandlerType)
{
this.errorHandlerType = errorHandlerType;
}

#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription
serviceDescription,
System.ServiceModel.ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint>
endpoints,
System.ServiceModel.Channels.BindingParameterCollection
bindingParameters)
{ }

public void ApplyDispatchBehavior(ServiceDescription
serviceDescription, System.ServiceModel.ServiceHostBase
serviceHostBase)
{
IErrorHandler errorHandler;
errorHandler =
(IErrorHandler)Activator.CreateInstance(errorHandlerType);
foreach (ChannelDispatcherBase cdb in
serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher cd = cdb as ChannelDispatcher;
cd.ErrorHandlers.Add(errorHandler);
}
}
public void Validate(ServiceDescription serviceDescription,
System.ServiceModel.ServiceHostBase serviceHostBase)
{ }
#endregion
}
}

 

6. Add the below lines above your service classes

[ServiceErrorBehavior(typeof(ErrorHandler))]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

7. Use sql script to create tables and stored procedure (it will be found inside db folder)

8. Now you can run and enjoy. You can test it by throwing some random exceptions. You can access the log viewer by using http://yourwebserviceaddress/elmah.axd

Thanks

 

References

[1] http://dotnetslackers.com/articles/aspnet/Getting-ELMAH-to-work-with-WCF-services.aspx

 


Asp.Net Problems and their Solutions