Click here to Skip to main content
14,698,418 members
Articles » Web Development » ASP.NET » General
Posted 24 Nov 2007


61 bookmarked

Creating Web Services in a Class Library project

Rate me:
Please Sign up or sign in to vote.
4.69/5 (21 votes)
29 Nov 2007
This article explains how you can create Web Services in a Class Library project


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=,
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:="")> _
<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:

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

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.


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.


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


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't get this to work with IIS 7.5 Pin
Shaun Sisk21-Nov-14 5:54
MemberShaun Sisk21-Nov-14 5:54 
Generaljson [modified] Pin
jora4422-Jul-10 19:23
Memberjora4422-Jul-10 19:23 
QuestionIn IIS7 Pin
evolvement17-Mar-09 6:33
Memberevolvement17-Mar-09 6:33 
AnswerRe: In IIS7 Pin
Akleinek20-Apr-10 1:46
MemberAkleinek20-Apr-10 1:46 
eeyrcr20-Jan-09 7:44
Membereeyrcr20-Jan-09 7:44 
GeneralRe: ASP.NET AJAX Pin
Bruno Piovan3-Mar-09 10:26
MemberBruno Piovan3-Mar-09 10:26 
QuestionWhy the need of Reflection Hack? Pin
PJonDevelopment5-Nov-08 13:24
MemberPJonDevelopment5-Nov-08 13:24 
AnswerRe: Why the need of Reflection Hack? Pin
Bruno Piovan6-Nov-08 0:01
MemberBruno Piovan6-Nov-08 0:01 
GeneralRe: Why the need of Reflection Hack? Pin
PJonDevelopment6-Nov-08 8:49
MemberPJonDevelopment6-Nov-08 8:49 
GeneralRe: Why the need of Reflection Hack? Pin
thearcaner23-Mar-09 9:49
Memberthearcaner23-Mar-09 9:49 
QuestionCan you give me a whole example? Pin
JLKEngine00825-Jun-08 21:10
MemberJLKEngine00825-Jun-08 21:10 
QuestionCan we use it in ASP .NET AJAX ? Pin
satriya18-Feb-08 0:10
Membersatriya18-Feb-08 0:10 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Bruno Piovan18-Feb-08 9:16
MemberBruno Piovan18-Feb-08 9:16 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
gkelly25-Feb-08 20:01
Membergkelly25-Feb-08 20:01 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Bruno Piovan26-Feb-08 3:51
MemberBruno Piovan26-Feb-08 3:51 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
gkelly26-Feb-08 18:35
Membergkelly26-Feb-08 18:35 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Bruno Piovan27-Feb-08 3:00
MemberBruno Piovan27-Feb-08 3:00 
Newshow to use the Service?? Pin
JLKEngine00825-Jun-08 21:10
MemberJLKEngine00825-Jun-08 21:10 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Unagii12-Jun-09 6:07
MemberUnagii12-Jun-09 6:07 
GeneralYou solved my problem. Pin
Ashaman27-Nov-07 3:17
MemberAshaman27-Nov-07 3:17 
GeneralRe: You solved my problem. Pin
Bruno Piovan27-Nov-07 13:24
MemberBruno Piovan27-Nov-07 13:24 
GeneralGreat Idea Pin
merlin98126-Nov-07 5:30
professionalmerlin98126-Nov-07 5:30 
GeneralRe: Great Idea Pin
Bruno Piovan26-Nov-07 6:02
MemberBruno Piovan26-Nov-07 6:02 

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

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