How To Get a WCF Service Up And Running In 5 Minutes

Objective

In today’s tutorial I will demonstrate how to implement a simple service that takes in a GUID and returns an int, how to host it in IIS and how to test it in the WCFTest tool to make sure it works.

Step One – Create the service

Create a new WCF service project in Visual Studio:

Screenshot_1

Step Two – Define the contract

In step two you need to define your services contract.  This is a simple interface that your code file will inert from that will define what methods will be exposed from your service.  By default you should see two files, service.cs and IService.cs.  The interface we want to change is the IService.cs file.

using System;
using System.ServiceModel;
namespace Service.Interfaces
{
[ServiceContract]
public interface IService
{
[OperationContract]
string Get(Guid reference);
}
}

This, hopefully, should be pretty straightforward.  Inherit from our new interface, implement the method and return the input parameter reference as a string.
Step Three – Define The Service

using Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace Service
{
public class GetService : IService
{
public string Get(Guid reference)
{
return reference.ToString();
}
}
}

This class should, hopefully, be pretty straightforward. All it does is implement the IService interface and returns the input parameter as a string.

Step Four – Configure your web.config

<service name="Service.GetService">
<endpoint address="" binding="wsHttpBinding" contract="Service.Interfaces.IService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/MyService/Service1/" />
</baseAddresses>
</host>
</service>
</service>
</services>

The main changes we need to make here are :

  1. Change the contract location to reflect the new name and name and namespace.  I put my contract in an Interfaces folder.
  2. Change the binding type from basicHttpBinding to wsHttpBinding for better security.  Read here for the reasons.
  3. Check the service name is OK

You can ignore everything else

 

Step Five – Create the SVC file.

The SVC file will form the end address for your service.  This is the file you will use as part of the URL to access the WSDL.  Visual Studio can create the file for you but for clarity we will create our own version as it’s very simple.  In visual studio create a new test file and then call it, GetService.svc.

 

Screenshot_3

 

open your new GetService.svc file and add;

<%@ ServiceHost Service="Service.GetService" %>

That’s it.  You should now have a service you can talk to.

 

Step Six – Configure IIS

Open IIS and create a new website.  Call it whatever you want .  In my project I’m calling it MyService and using port 8080.

Screenshot_4

 

Using notepad, open your host file ‘C:\Windows\System32\drivers\etc’ (make sure you’re running notepad in ‘Administrator mode’) and add the following line.

127.0.0.1  MyService

You should make sure that the IUsr account has modify permissions.

Step Seven – Use the WCFTest tool to test your service

Visual studio comes witha handy tool to test your WCF service.   The program is not the easiest to find. as it’s located within the Visual Studio file structure.

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\WcfTestClient.exe

When the test tool has loaded hit File -> Add Service

Screenshot_5

Things to note:

  •  WCF will only find MyService if it has been added to your host file
  • You must put in the port number you are using (I’m assuming you are not using port 80)
  • You must point the service to the file we created in Step Five

The End

Screenshot_6

 

You should now be able to click on the Get() method.  A GUID should have already been automatically created for you as an input parameter.  All you have to do is click Invoke and you should now see the GUID returned to you as a string.  This proves you have a working service that does as you expect.

As always things can and do go wrong when setting services up, here are some issues that have thrown me:

 

  • IIS has not registered correctly, run this in a command prompt :
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis.exe -iru
  • WCF has not registered correctly, run this in a command prompt :
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319>ServiceModelReg.exe -r

 

The source code for this is available via my Github account here.

 

 

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *