How to host a WCF service in console application using wsHttp binding

Introduction

In this article we will see how to host a wcf service using the wsHttpBinding. When you use http, you would normally host it in IIS, but if you would like to host it in a console application, you could follow this article. Here we will try to host a service with a wsHttpBinding and will expose a MEX endpoint in order to expose the metadata so that any tool which can generate a proxy can use it. Here in this example we are exposing the MEX endpoint particularly to be used in the WCF test client. This article will not cover how you can generate a wcf service. If you like to know how a basic service is created, you need to read Creating WCF Service and Self Hosting in a console application.

How to edit wcf configuration.

All your wcf configuration will be stored in the app.conf file of your host project. Here in our case as we are going to host it in a console application, it will be the app.conf of the console application. You could use the wcf configuration edit tool to edit this configuration. To open the wcf configuration tool, do as shown in the image.

edit wcf

Adding an endpoint with wsHttpBinding

Here we need to add an wshttp endpoint. Create your endpoint as shown in the image below.

Wcf service endpoint

Here in the address field, give the name of the interface where you have defined a service contract. Here the address name could be anything but we will keep it as the name of the interface, this is the relative address which we are defining; we will later add a base address for this. Add wsHttpBinding on to the binding column. Here we defined wsHttp as the binding for this endpoint. To the contract field, browse to the dll where we have the interface on which we have defined the service contract. You could point to the dll where we have this interface.

Adding a base address for the endpoint.

Now we need to add a base address to the service, So that the complete address for the service will look like “Base Adress + Name of the service” Add it as shown in the image. Here the port number can be any port number except the reserved and already used.

Adding wcf base address

With this, we are done creating a basic endpoint.

Adding MEX Endpoint.

Now create a wsHttp MEX endpoint as shown in the image. Here IMetadataExchange is the default contract implementation from Microsoft. We will just add this text as the contract implementation of MEX. mex

Now this is a MEX end point exposed via http, so we need to enable some more setting for this MEX end point to work. Go to advanced->Service Behavior->MEX and set HttpGetEnabled to true as shown in the image below.

Enabling Mex over http

Adding Mex behaviour to the service.

Now we need to enable MEX behaviour to the service. Do as shown in the image. Select MEX from the dropdown for BehaviorConfiguration. Remember, MEX is the name given to the Mex endpoint.

Adding Mex Behavior

Now we are ready, now remember to run the service host as Admin, because when we have http endpoint, it requires this or else it will throw an access violation exception. Now open the WCF test client and add the service endpoint to test it.

Wcf test client

Delphi how to check if you have read permission on a directory

It seems to be difficult to check the read permission on a directory in Delphi 2009. Here we will see how to find the read permission in an alternate way. Continue reading

Libish Varghese Jacob

Libish Varghese JacobI am currently working as a lead engineer in one of the leading wind turbine manufacturing firm. I have wide range of interests and getting my hands dirty in technology is one among them. I use this platform primarily as my knowledge base. I also use this platform to share my experience and experiments so that it might help someone who is walking the way I already did. The suggestions expressed here are the best to my knowledge at the time of writing and this may not necessarily be the best possible solution. I would pretty much appreciate if you could comment on it to bring into my notice on what could have been done better.