5,692,513 members and growing! (19,165 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Creating Web Services in a Class Library project

By Bruno Piovan

This article explains how you can create Web Services in a Class Library project
C# 2.0, C# 3.0, VB 8.0, VB 9.0, C#, VB, Windows, .NET, .NET 3.5, .NET 2.0, ASP.NET, WebForms, VS2005, VS2008, Visual Studio, Architect, Dev

Posted: 24 Nov 2007
Updated: 29 Nov 2007
Views: 20,854
Bookmarked: 42 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
13 votes for this Article.
Popularity: 4.92 Rating: 4.42 out of 5
0 votes, 0.0%
1
1 vote, 7.7%
2
0 votes, 0.0%
3
3 votes, 23.1%
4
9 votes, 69.2%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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

About the Author

Bruno Piovan


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.
Occupation: Web Developer
Location: Brazil Brazil

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralWhy the need of Reflection Hack?memberPJonDevelopment13:24 5 Nov '08  
GeneralRe: Why the need of Reflection Hack?memberBruno Piovan0:01 6 Nov '08  
GeneralRe: Why the need of Reflection Hack?memberPJonDevelopment8:49 6 Nov '08  
GeneralCan you give me a whole example?memberJLKEngine00821:10 25 Jun '08  
QuestionCan we use it in ASP .NET AJAX ?membersatriya0:10 18 Feb '08  
GeneralRe: Can we use it in ASP .NET AJAX ?memberBruno Piovan9:16 18 Feb '08  
GeneralRe: Can we use it in ASP .NET AJAX ?membergkelly20:01 25 Feb '08  
GeneralRe: Can we use it in ASP .NET AJAX ?memberBruno Piovan3:51 26 Feb '08  
GeneralRe: Can we use it in ASP .NET AJAX ?membergkelly18:35 26 Feb '08  
GeneralRe: Can we use it in ASP .NET AJAX ?memberBruno Piovan3:00 27 Feb '08  
Newshow to use the Service??memberJLKEngine00821:10 25 Jun '08  
GeneralYou solved my problem.memberAshaman3:17 27 Nov '07  
GeneralRe: You solved my problem.memberBruno Piovan13:24 27 Nov '07  
GeneralGreat Ideamembermerlin9815:30 26 Nov '07  
GeneralRe: Great IdeamemberBruno Piovan6:02 26 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Nov 2007
Editor:
Copyright 2007 by Bruno Piovan
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project