Web application developer

Hosting a WCF service in IIS

wcf

Written by: willem | Mar 25 2014 7:23 PM

In previous posts I described how to create a basic web service and connect to it without using a proxy generated by Visual Studio. The next step is to describe how to setup IIS to use WCF through Http and connect to it with a client.

This post is the third in a series of posts.

For this tutorial I’m using the code as described in my previous post. If you want to start here, download the solution.

Configuring IIS

First check if all necessary software is installed.

Start up “Windows Features”. I assume you already have IIS installed but if not you can do so here. If IIS is installed you can add WCF services to the .NET framework under the option .NET Framework 4.5 Advanced Services.

001

Click OK to install the WCF services.

Next start up IIS manager by clicking start and typing IIS in the start menu. Right click sites and click add website.

002

Fill in the settings like below picture. The path will be the path where the project will get published to. Don’t use the path to the project but a path to an empty folder.

Click “Select” to check the settings of the application pool.

003

Make sure you are using .Net version 4 (or higher) and pipeline mode. I chose to use the default app pool but you could create a new one.

004

Click ok twice so the web application is created.

Publishing the project

Next open the Visual Studio solution and right click the MessageService project. Click publish.

005

Choose the same path as you did while creating the web application and click “publish”.

006

Configuring the service

Open the web.config in the pubished folder (not the one in the solution), and add a binding to the system.servicemodel part.


<bindings>
	<basicHttpBinding>
		<binding name="InsecHttp">
			<security mode="None" />
		</binding>
	</basicHttpBinding>
</bindings>

Change the base address and the port of the service and use the new binding in the end point configuration.

<services>
      <service name="MessageService.MessageService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8734/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="InsecHttp" contract="MessageService.IMessageService">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>

Now the service should be running. To check this you can type the url of the service in the browser. In the publish folder there is a file with an svc extension. Go to this file in the browser: http://localhost:8734/MessageService.MessageService.svc

006_2

Here you see the url you can use in the WCF test client to test the service.

Connect the client to the service

Now the service is running and ready to receive requests. The next step is to connect the client to the service.

First prevent the service project from starting up while debugging. Right click the solution in Visual Studio and click “properties”.

007

Click “startup project” and make sure only the WcfClients project is started up.

008

Click ok to save.

Open MainWindow.xaml.cs and change the end point address in the Button_Click event.

private void Button_Click(object sender, RoutedEventArgs e)
{
	EndpointAddress endpoint = new EndpointAddress("http://localhost:8734/MessageService.MessageService.svc");
	Binding binding = new BasicHttpBinding();
	
	ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>(binding, endpoint);
	IMessageService service = factory.CreateChannel();

	MessageService.Message message = new MessageService.Message();
	message.Reverse = true;
	message.StringValue = InputTextBox.Text;

	OutputTextBox.Text = service.ProcessMessage(message);
}

Debug the solution and test if the client works.

009

Congratulations! You just connected your WCF client to an in IIS hosted service.

Download the complete code here.

2 Comments

  • I am genuinely thankful to the holder of this web page
    who has shared this enormous article at here.

    How To Start A Convenience Store said
  • Since the admin of this web page is working, no question very quickly
    it will be well-known, due to its quality contents.

    https://www.viagrapascherfr.com/vente-viagra-france/ said
Add a Comment

About Me

I'm a web application developer specializing in asp.net. I have skills in asp.net, C#, html5, javascript, asp.net mvc, design patterns and more.

more about me

Random background module

This module for Orchard CMS lets you upload a list of images. For every page it chooses one randomly and sets it a the background image.

Go to project page on CodePlex
Download module

The Monack Framework

This open source framework makes it easy to build and develiver custom web applications. Unfortunately I can't spent much time on it so it is still work in progress. 

More about MonackFr
Project page at CodePlex

Orchard

Since I decided to create this website in Orchard and didn't know anything about
it when I started, I also post my experiences with this CMS.