Home of the AlmostImplementedException

WCF-Standalone: Webservices without IIS

The Windows Communication Foundation (WCF) is one of the best things Microsoft ever did for us. Whoever tried to build a Webservice, especially with REST and SOAP and then worked with WCF know what i mean. But today i just want to show you, how a WCF-Service can be used without an IIS and event without opening a port. That was very important for one of my projects because of the companies security rules.

First we’ll need a simple WCF-Service:

The Interface provides the contract the service is implementing. Here we define which methods the service provides. The second file is the actual Service.

And now the code to use the service as standalone. We’ll start with a plain old SOAP-Webservice

First we have to define an Uri with the adress and port. After that we create a new instance of ServiceHost with the type of the wcf-service and the uri.
After that we define the desired endpoint (otherwise ServiceHost will create every possible endpoint) for the contract (interface) of the wcf-service. Which in our case is BasicHttpBinding (simple SOAP).
Then we call open and the WCF-Service is running. Thats it!

Since BasicHttpBinding is a simple SOAP there is WsHttpBind for more (security-) features.This CodeProject-Post gives more details on Difference between BasicHttpBinding and WsHttpBinding.

Another possible endpoint and working without a port is NetNamedPipeBinding. In order to change the code above to use NamedPipes we just have to change 2 lines. First we need to change the uri to “net.pipe”.

And the line host.AddServiceEndpoint .. is replaced with

To use both ways together, just add both Endpoints and call the ServiceHost constructor with an array of both uri’s.
All possible endpoint-bindings can be found in System.ServiceModel;

To see a list of all enpoints defined by a ServiceHost just, just copy the following code behind host.Open();
Its for debugging only.

Now we have a Standalone-WCF-Service and can use it however we like.

For a starter you can use this WCF-Client-Code which is using the NamedPipe-Endpoint

To run this client example you need to add the following usings
using System.ServiceModel;
using System.ServiceModel.Channels;

And all examples needing a reference to the System.ServiceModel library.

Share :

, , , ,

Leave a Reply