Click here to Skip to main content
Click here to Skip to main content

Creating Web Services in a Class Library project

, 29 Nov 2007
Rate this:
Please Sign up or sign in to vote.
This article explains how you can create Web Services in a Class Library project

Introduction

Recently I worked on a plug-in based system, and one of the approaches of this system was to allow each plug-in (dll) to have the capability to host web services. I had never heard about such task before, I made a search on the Web and didn't find anything about that, so I decided to think and fortunately I came with a pretty good solution.

The HTTP Handler for .asmx

The first thing I did was to open the Web.Config file located in %windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG and look at the following entry inside <httpHandlers>:

<add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="False" />

Using Reflector, I found that the WebServiceHandlerFactory builds and returns an IHttpHandler in the GetHandler of the IHttpHandlerFactory interface, that was all I needed to know.

Creating the WebServiceBase class

So here it is what I did:

I created a new Class Library project and in this project, a class named WebServiceBase, this class could be anywhere, since it's an abstract class that will be the base class for the web services. The definition of this class is below:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Reflection

Public MustInherit Class WebServiceBase
    Inherits WebService
    Implements IHttpHandlerFactory
    
    Private Shared wshf As New WebServiceHandlerFactory
    Private Shared coreGetHandlerMethod As MethodInfo = GetType(WebServiceHandlerFactory).GetMethod("CoreGetHandler", _
    BindingFlags.Instance Or BindingFlags.NonPublic)

    Public Function GetHandler(ByVal context As System.Web.HttpContext, ByVal requestType As String, ByVal url As String, _
    ByVal pathTranslated As String) As System.Web.IHttpHandler Implements System.Web.IHttpHandlerFactory.GetHandler
        Return DirectCast(coreGetHandlerMethod.Invoke(wshf, New Object() {Me.GetType, context, context.Request, context.Response}), IHttpHandler)
    End Function

    Public Sub ReleaseHandler(ByVal handler As System.Web.IHttpHandler) Implements System.Web.IHttpHandlerFactory.ReleaseHandler

    End Sub

End Class

Note the Reflection hack to call the CoreGetHandler method from WebServiceHandlerFactory, this is needed because this method is declared as Friend, and this method is the one that actually creates the IHttpHandler instance.

It's almost done! All you have to do now is create the web services, which will be classes that inherit from this one, so here is an example:

Imports System.Web.Services

<WebService(Namespace:="http://tempuri2.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WSTest
    Inherits WebServiceBase

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function
End Class

Remember that everything above is in a Class Library, so compile this project, create a new web site and add this project as a reference.

This Class Library project I called WSLibrary, and as the WSTest is nothing more than a HttpHandler, you should add a new entry to your web.config for each Web Service you created, in this example above, I added:

<httpHandlers>
  <add path="WSTest.asmx" verb="*" type="WSLibrary.WSTest" validate="false"/>
</httpHandlers>

Now, every time a request to WSTest.asmx is made, it will be handled by the WSLibrary project. It can be changed very easily to suit your needs.

Conclusion

This approach worked very well for the project I was working on. I'm sure there are more things that could be made to make it even better, like you can make a way that only one entry in the httpHandlers is necessary for as many web services as you have.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Bruno Piovan
Web Developer
Brazil Brazil
I work as a software architect for a mobile entertainment company and as such, I'm aways working on the architecture of new solutions for the company needs. I also teach Microsoft Official Courses (.Net) at the evenings and I currently have MCP, MCAD, MCTS (Web, Windows, Distributed), MCPD (Web and Enterprise) and MCT.

Comments and Discussions

 
QuestionCan you give me a whole example? PinmemberJLKEngine00825-Jun-08 21:10 
QuestionCan we use it in ASP .NET AJAX ? Pinmembersatriya18-Feb-08 0:10 
GeneralRe: Can we use it in ASP .NET AJAX ? PinmemberBruno Piovan18-Feb-08 9:16 
GeneralRe: Can we use it in ASP .NET AJAX ? Pinmembergkelly25-Feb-08 20:01 
GeneralRe: Can we use it in ASP .NET AJAX ? PinmemberBruno Piovan26-Feb-08 3:51 
GeneralRe: Can we use it in ASP .NET AJAX ? Pinmembergkelly26-Feb-08 18:35 
GeneralRe: Can we use it in ASP .NET AJAX ? PinmemberBruno Piovan27-Feb-08 3:00 
Newshow to use the Service?? PinmemberJLKEngine00825-Jun-08 21:10 
GeneralRe: Can we use it in ASP .NET AJAX ? PinmemberUnagii12-Jun-09 6:07 
GeneralYou solved my problem. PinmemberAshaman27-Nov-07 3:17 
GeneralRe: You solved my problem. PinmemberBruno Piovan27-Nov-07 13:24 
GeneralGreat Idea Pinmembermerlin98126-Nov-07 5:30 
GeneralRe: Great Idea PinmemberBruno Piovan26-Nov-07 6:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 29 Nov 2007
Article Copyright 2007 by Bruno Piovan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid