Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

May 6, 2011

Have WCF Service hosted on windows service.

This one is quite old, anyway, as I am using my blog for adding notes to myself as well, I will repeat the solution so I can later find it :).

Maybe you will find it useful as well.

To host a WCF service on top of Windows service you need to do the following:

1. Create the windows service that will host the WCF Service.

2. In the windows service add references to the following assemblies:

2.1. System.ServiceModel

2.2. System.ServiceProcess.

3. Declare the following in the Service class:

ServiceHost host = null;

4. In the OnStart method of the service, instantiate the host variable and the WCF Service instance:

protected override void OnStart(string[] args)
{
    if (host != null)
    {
        host.Close();
    }


    SomeService.ServerAgent server = new SomeService.ServerAgent();
    host = new ServiceHost(server);
    host.Open();
}

5. We need to handle Service stop as well, add the following code to the OnStop method of the Windows Service:

protected override void OnStop()
{
    host.Close();
    host = null;
}

6. All we need to do now is to set some bindings / endpoints. NOTE: They should go to the app.config file of the WINDOWS service. Just copy them from the WCF Service web.config.

You are done.

Install and start the windows service and navigate to the Url Address you added as endpoint. You should be able to browse it.

Apr 10, 2010

Nasty shock when handling WCF service exceptions ....

I just wanted to share a recent interesting experience while handling WCF service exceptions.

Here is the case.
Imagine you have to consume a WCF service. You need to handle if the service is gone and do some additional things (let's say you want to display a message).

Let's have an example. Imagine you have the following service:
http://someservice.com/Calculator
This service is used to calculate let's say the interest rate for a loan.
You want to be able to use the service and if the service is not there - you will want to store the borrower data in the db so you can re-submit it ...
What I did in order to test this scenario was to change :

http://someservice.com/Calculator
to
http://someservice.com/CalculatorUnavailable
so I can simulate that the service is gone.
In this case the .NET framework will throw System.ServiceModel.FaultException. It's OK, I handled it and pass it for testing.

After a few days we end up with the service gone again. And the bad news was that it was working on my machine. I needed to simulate it somehow and the problem was obviously some kind of restriction on the server.
Since we wanted to handle such scenarios as well, I wanted to somehow simulate it on my PC.

Finally after about half an hour of debugging I found out the reason.
It was the Server not able to find the domain name record for the service URL.
Then it was easy for me to figure out that if I change the url from :

http://someservice.com/Calculator
to
http://UnavailableSomeService.com/Calculator (note that it matters if you change the TLD (top level domain) or the path. You will have two different exceptions ;).
It appeared that in such cases the exception that .NET will throw is System.ServiceModel.EndpointNotFoundException.

I handled it as well and voilla!