Click here to Skip to main content
15,879,535 members
Articles / Web Development / ASP.NET
Article

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 20072 min read 120.5K   61   23
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>:

XML
<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:

VB
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:

VB
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:

XML
<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


Written By
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 4:54
Shaun Sisk21-Nov-14 4:54 
Generaljson [modified] Pin
jora4422-Jul-10 18:23
jora4422-Jul-10 18:23 
QuestionIn IIS7 Pin
evolvement17-Mar-09 5:33
evolvement17-Mar-09 5:33 
AnswerRe: In IIS7 Pin
Akleinek20-Apr-10 0:46
Akleinek20-Apr-10 0:46 
GeneralASP.NET AJAX Pin
eeyrcr20-Jan-09 6:44
eeyrcr20-Jan-09 6:44 
GeneralRe: ASP.NET AJAX Pin
Bruno Piovan3-Mar-09 9:26
Bruno Piovan3-Mar-09 9:26 
QuestionWhy the need of Reflection Hack? Pin
User 45427185-Nov-08 12:24
User 45427185-Nov-08 12:24 
AnswerRe: Why the need of Reflection Hack? Pin
Bruno Piovan5-Nov-08 23:01
Bruno Piovan5-Nov-08 23:01 
GeneralRe: Why the need of Reflection Hack? Pin
User 45427186-Nov-08 7:49
User 45427186-Nov-08 7:49 
GeneralRe: Why the need of Reflection Hack? Pin
thearcaner23-Mar-09 8:49
thearcaner23-Mar-09 8:49 
QuestionCan you give me a whole example? Pin
JLKEngine00825-Jun-08 20:10
JLKEngine00825-Jun-08 20:10 
QuestionCan we use it in ASP .NET AJAX ? Pin
satriya17-Feb-08 23:10
satriya17-Feb-08 23:10 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Bruno Piovan18-Feb-08 8:16
Bruno Piovan18-Feb-08 8:16 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
gkelly25-Feb-08 19:01
gkelly25-Feb-08 19:01 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Bruno Piovan26-Feb-08 2:51
Bruno Piovan26-Feb-08 2:51 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
gkelly26-Feb-08 17:35
gkelly26-Feb-08 17:35 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Bruno Piovan27-Feb-08 2:00
Bruno Piovan27-Feb-08 2:00 
Newshow to use the Service?? Pin
JLKEngine00825-Jun-08 20:10
JLKEngine00825-Jun-08 20:10 
GeneralRe: Can we use it in ASP .NET AJAX ? Pin
Unagii12-Jun-09 5:07
Unagii12-Jun-09 5:07 
GeneralYou solved my problem. Pin
Ashaman27-Nov-07 2:17
Ashaman27-Nov-07 2:17 
GeneralRe: You solved my problem. Pin
Bruno Piovan27-Nov-07 12:24
Bruno Piovan27-Nov-07 12:24 
GeneralGreat Idea Pin
merlin98126-Nov-07 4:30
professionalmerlin98126-Nov-07 4:30 
GeneralRe: Great Idea Pin
Bruno Piovan26-Nov-07 5:02
Bruno Piovan26-Nov-07 5: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.