Web service inside a Windows service without IIS

Update: If you have any questions about this article or any other web-services related queries then please ask! I see alot of traffic coming from Google about web-services and related issues/problems which this text doesn’t cover but I can probably help with! Leave a comment and I will respond quickly – if I don’t have the answers then I bet I know someone that does.

Skip to the Ramble End section to get into the guts of the article… the first part is just me talking too much as usual.

I’ve decided I need to get out more (it was either me or my shrink – I cant remember. Ed.); see the sights, expand my horizons, meet new media dissemination platforms.

I find myself writing and documenting code for my team mates at Objectify – tidbits of information here and there, how-to’s, faq, code snippets, my secret pasta recipe etc. – all usually transmitted in email form, with the occasional Word or PDF doc if they are lucky. If they are really lucky I may enter it into our internal FAQ system… for a fee.

Why not blog about some stuff too? See just for interests sake if anybody is interested in anything I say.

I thought I would start off nice and easy – ease into the Blogoshere nice and slowly (note to self – blog about hating the word “Blogoshere”… oh someone already did, damn). So I’m opening up the innings with a little ditty about hosting web services inside a windows service without the need for IIS. I wrote this doco about a year and a half ago, so its basically a translation of some old stuff… Sure its nothing too technical, its not ahead of the curve so to speak, and its probably been done a thousand times but we all gotta start somewhere. If we get time we may even throw in some SOAP headers and AES encryption for good measure – nothing like a pre-shared key to brighten your day. Okay well maybe in the next entry.

I first came across the need for a web service inside a windows service when thinking about ways to decentralise the Objectify CMS debugging system. Basically the debugging system goes way back to the days of the first CMS, all written in VB6 mind you, and uses mail slots to transfer messages from an array of COM objects to the debugger output application. Problem was that clients (and internal testers etc.) had to log into the server and run the application to see the messages. Now I know what you are going to say – you can pipe mailslot data between machines, problem solved… but I wanted to try something new, so web services it was.

Ramble End

We will end up with a simple web service windows service (WSWS?) with an interface to query which will do some simple processing for posterity’s sake and return a message to the client.
Firstly, you are going to need Web Services Enhancements 3.0 from Microsoft. Hop along to http://msdn2.microsoft.com/en-gb/webservices/Aa740663.aspx and grab a copy – make sure you install the developer option (the Visual Studio developer option if you are using VS). It will distribute itself with your app when you build the installer (if you choose to) so no need to install the MSI on your target machine.

Start your new project by creating a class library. I called mine something original – Web Service inside a Windows Service. This is where we will be placing the bulk of the code. Create a new public class called HostWebService.

Add in references to Microsoft.Web.Services3 and System.Web.Services.

Begin your code by adding the following namespace includes:

using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Addressing;
using Microsoft.Web.Services3.Messaging;
using System.Net;

Okay so we are ready to go. Too easy. In the constructor we need to do some things to start our little web service listener. First, we need to know the local machine’s DNS address, and use this address to create a URI for our new Web Service (i.e. the address that will be used in the client application to query the service).

 string hostName = Dns.GetHostName();
 IPHostEntry ipEntry = Dns.GetHostEntry(hostName);
 Uri address = new Uri("soap.tcp://" + ipEntry.AddressList[0].ToString() + "/MyLittleWebService");

By the way, did I mention that if you know a better way to do any of this the please feel free to comment – i.e. is that the best way to get the IP of the local machine?

You can see above that the Uri address=xxxxx line is where you set the actual URI of the service, you can change the last part from MyLittleWebService to something more fitting if you like.

Now we need to create the actual web service code… It’s basically the same code that is generated by Visual Studio when you create a web service (the code behind in the Service.asmx file). Create a new class called WebServiceCode.cs and paste in the following:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://myURI/MyLittleWebService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceCode : System.Web.Services.WebService
    public WebServiceCode()

    public string GetMessages(string guid)
        return "At the third stroke the time will be " + DateTime.Now.ToString();


Nothing too tricky there, stock standard web service stuff. Next we go back into the HostWebService class and add the following just below the URI stuff:

 SoapReceivers.Add(new EndpointReference(address), typeof(WebServiceCode));

And we’re done… almost. Well we’re done to the point that if you run that code (from a test app or something) then you will have a windows app that is hosting a web service. Thats great, but how do you query said service?

Accessing the new service

Before we can access the new web service we need to add it as a Web Reference to our project. Normally this is nice and easy, just right click and add a Web Reference in Visual Studio and let WDSL do the work. Problem is this new web service isn’t published anywhere yet, and even when its installed on the target machine and running, its URI will be unknown to the client.

Lets start by getting the web reference. Create a new ASP.NET web service project (call it tempservice or something, you wont need to keep it for long) go to the Service.asmx code behind and paste in the code from your WebServiceCode.cs file. Then right click the Service.asmx file and View in Browser. Copy the URL from the browser window that pops up, and add it as a web reference to your main project (right click References, Add Web Reference, paste into field, click Go, click Add Reference).

Great we have a description of our web service for the client app to use when querying.

Create another class called WebServiceQuery. Add the following to the namespace references section:

 using System.Net;

Now create a public method called Query that returns a string (or you can just paste in the following code):

 public string Query(string targetMachineName)
      Web_Service_inside_a_Windows_Service.WebServiceCodeReference.WebServiceCodeWSE wsc = new Web_Service_inside_a_Windows_Service.WebServiceCodeReference.WebServiceCodeWSE();

      IPHostEntry ip = Dns.GetHostEntry(targetMachineName);
      wsc.Url = "soap.tcp://" + ip.AddressList[0].ToString() + "/MyLittleWebService";
      string result = wsc.GetMessages("some guid");

      return result;

NOTE: The object we create is the WSE version (WebServiceCodeWSE), without this the calls to soap.tcp will not work.

Simple – first we instantiate our web reference object. Then we set the URI of the newly instantiated object the same way we did for the service itself. Finally we query the service and return the value. Note the GUID parameter on the service call – this is just in there to demonstrate calling methods and passing in parameters, but you already knew how to do that.

Now you can create a test application, start the service and then query it! All that’s left is to create a Windows service and create an instance of your calling class.

Just a note on the URI creation parts – if you are getting an error do do with port parsing, check that the IP address its getting isn’t and IPV6 address…

Okay so that’s it, my first post. Its long and boring.

Next time I will visit security using SOAP headers and how to encrypt the SOAP data using AES (Rijndael).


21 thoughts on “Web service inside a Windows service without IIS

  1. Hi Jak,

    I haven’t fully read you stuff. But is this related to the article Service Station by Aaron: http://msdn.microsoft.com/msdnmag/issues/04/12/ServiceStation/default.aspx

    This was really good sample using the 2.0 framework.

    If so, I’m trying to utilize the power of ASMX to be accessed in UNIX by a java program, without USING IIS at all. I’m still in R&D stage but your insight would be much help.

    here’s my flow:
    ASMX (without IIS) –> UNIX (called inside a java program)

    1. same question is mine . i am also in R&D phase,no exact answer till now.pl any one help me.



  2. Hi Jeff,

    The service station article you linked is a little bit different than my samples here – the Service Station solution is basically allowing an entire ASP.NET site to be hosted in your application, whereas my samples only create a single SOAP listener per web service that you require (i.e. a little more light weight, although I would assume that behind the scenes there are more similarities than this).

    The solution I outline is great if you don’t need any transport level security (i.e. SSL) – although you should be able to implement my pseudo SOAP level and message level security (which I’ve done in Rijndael/AES) using Cryptix.

    I would also assume you have managed to connect up to a normal IIS web service in your Java code…

    In the mean time I have added a small sample app to help get you started here – it doesn’t show you how to create a windows service however (the web service is hosted directly in a windows forms application).

  3. Hi,
    Good article. I wish to implement it. But I need to know where this WebServiceCodeWSE comes from suddenly.Please clarify.
    Also let me know in “http://myURI/MyLittleWebservice”, myURI should be replaced with the machine’s IP number.

  4. Hi Kirubakaran,

    The [name]WSE object is available on the client side proxy class that is generated when you import the web reference.

    Just as a little side note – this article is quite old now… I’d suggest you investigate WCF in .NET 3.0 to acheive the same result much easier!



  5. Hi Jordan
    I have a server that gives some services to some win applications.
    I want to use these services in my web application by .net remoting.
    I did it but by means of iis that in this case iis connects to my database directly without connecting to my server.
    now I want to do this by connecting to my server.
    could u please help me?
    please email me the answer if possible
    Thank you very much

    1. Hi Zahra,

      I’m not completely sure what you are trying to achieve… .NET remoting and web services are a bit differnt from each other.

      Are you able to give me some more information so I can help you out?



  6. Hello,
    This is absolutely great! Such a small code can host a web service.
    But I have a small problem, how can I alter this code to work with http? (To be specific https.)
    If I can find an answer for that, it would be great.
    But please note that I’m not that fluent in C# and will be happy with a detailed answer.
    Thank you.
    Best Regards, Lochana.

  7. Hi,

    I also need to puplish an ASMX web service over http? Is that possible in an easy way like this? Kind of SoapReceivers.Add(“http://….”) without implementing my own http server would be nice.

    Thanks for any ideas.

    Kind Regards,

  8. Hello. I have problems with using Microsoft.Web.Services3 and Microsoft.Web namespaces. To work with those namespaces i try to add or find the dll for them. But i cant find the dll on .Net dll library or anywhere i searched.

    The question is “How can i add Microsoft.Web dll to references”


  9. Hi,
    Since you said you could try to help people that show up here through google … this is my problem:

    I need to have a webservice server that receives using http soap, and not tcp soap…
    Uri address = new Uri(“http://” + ipEntry.AddressList[0].ToString() + (…)

    I’m getting errors indicating that it’s not supported :”System.NotSupportedException WSE802: Unsupported SoapChannelCapabilities Flags”,

    if I use soap.tcp instead of http it deploys and listens for requests.

    I’ve been reading online that I need to reimplement the HttpSys Transport layer, but I’m not seeing how to connect it to … say your code example above … do you think you could help me out on this?


  10. Your directions after the “Accessing the New Service” section don’t make any sense. Could you just post a zip file containing your solution and projects?



  11. i am in need of something simialar but confused withis article
    i have WS developed in VS2005 which access the database & provides services to the frontend calling it
    my issue is currently as the WS is in Vs2005 i need to open VS & execute the WS once so that the other applications can access the data via the WS , if its nit excuted than data cannot be accessed

    so i was trying the above lines but nit getting thr’

  12. Pingback: قاموس

Comments are closed.