Walkthrough on creating WCF 4.0 Service and Hosting in IIS 7.5

Objective

This article will give step by step walkthrough

1. How to create a basic WCF 4.0 Service?

2. How to host WCF Service in IIS 7.5?

3. Hot to test service in a client.

Create WCF Service

Create WCF service. Open visual studio select new project and then from WCF tab select WCF Service application to create a new WCF service.

clip_image002

Delete the default code created by WCF from IService1 and Service1.svc.cs

a. So delete the data contract

b. Modify the service contract as below. Just make one operation contract to return a string .

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService5
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetMessage();

    }
}

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService5
{
    public class Service1 : IService1
    {
        public string GetMessage()
        {
            return "Hello From WCF Service ";
        }
    }
}

Leave the default configuration created by WCF.

Web.Config

<?xmlversion="1.0"?>
<configuration>
  <system.web>
    <compilationdebug="true"targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>         
          <serviceMetadatahttpGetEnabled="true"/>         
          <serviceDebugincludeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironmentmultipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modulesrunAllManagedModulesForAllRequests="true"/>
  </system.webServer
</configuration>

Host WCF Service in IIS 4.0

Open IIS

a. Open the command prompt in administrator mode. To do click on Start button then type RUN in search and then in Run window type inetmgr to open IIS .

clip_image003

Now you will have IIS open like below

clip_image005

b. Right click on Sites and then click Add Web Site

clip_image006

c. Now a window will open.

clip_image007

Give any name of your choice as the site name. I am giving name here HostedWcfService

clip_image008

Now click on select and choose ASP.Net v4.0 from the drop down.

clip_image009

Now in the physical path section, we need to give physical path of the service. So to get the physical path of the service, right click on the WCF Service project in visual studio and click open project in windows explorer. Open the project in windows explorer and from address bar copy the path and paste that below. Or you can browse also. Just click on browse button and navigate to project folder of the WCF Service.

clip_image010

Now at the Binding section select HTTP. Give any port number. I am selecting the port as 4567

clip_image011

Do not give any name as host name. Leave it blank

clip_image012

Check the check box Start Web Site Immediately.

clip_image013

And now click OK on the window.

d. Now open visual studio command prompt in administrator mode. Right click and select as administrator

clip_image014

clip_image015

Type the below command

aspnet_regiis.exe /iru

clip_image017

You will get the message Finished Installing ASP.Net 4.0

e. Now go to inetmgr, you would able to see the site you just created in previous step.

clip_image018

Publishing the WCF Service

Go back to WCF Service. Right click and select Publish

clip_image019

On clicking of publish a window will come.

clip_image020

Give a Service URL. You are free to give any Service URL here. Just make sure host name is geeting resolved. In stead of localhost you can give IP address or your machine name also.

clip_image021

Give the name of the site HostedWcfService. We created this site in previous step.

clip_image022

Leave the credential inactive.

clip_image023

Now click on Publish.

clip_image024

You will get the message in bottom publish succeeded

Browsing the service hosted in IIS

Open Inetmgr and right click on site HostedWcfService. Then from Manage Web Site select Browse option.

clip_image025

When you click on browse site will get open in browser

clip_image027

You will get the above error. Just in address bar append Service1.svc with the address to open the service in browser.

http://localhost:4567/Service1.svc

And in browser you will get the service up and running.

clip_image029

So now you successfully created and hosted the WCF 4.0 service in IIS 7.5

Testing the Service at client

a. Create console application project.

clip_image031

b. Right click and add the service reference.

clip_image032

c. Give in address the address of service hosted in IIS and click GO.

clip_image033

d. Call the service as below ,

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client proxy = new Service1Client();
            Console.WriteLine(proxy.GetMessage());
            Console.Read();
        }
    }
}

Output

clip_image035

 

Conclusion

In this article we saw,

How to create a basic WCF 4.0 Service?

How to host WCF Service in IIS 7.5?

Hot to test service in a client.

I hope this article was useful. Thanks for reading. Happy coding.

47 thoughts on “Walkthrough on creating WCF 4.0 Service and Hosting in IIS 7.5

  1. rams September 9, 2010 / 2:32 pm

    thanks kumar! i pulled my hair for a day. i tried many things and messed my app. this morning i found your blog. my problem resolved. in the end, i found clientaccesspolicy.xml missing in wwwroot. your walkthrough presentation is excellent. thanks again

  2. Anil December 21, 2010 / 9:45 am

    Your article is quite helpful for beginners thanks for that ;-)

  3. Sam April 24, 2011 / 8:48 pm

    Okay, after reading countless web sites, this is the one that got it to work for me. Thanks so much for posting it.

  4. DougS May 25, 2011 / 12:52 am

    Thank you Dhananjay! This was all I needed to get going, very helpful.

  5. logicchild July 4, 2011 / 3:31 pm

    Hi,
    Thanks, I worked 100% on the local PC, but I’ve tried to use the client on another PC on the LAN, but the following message is displayed:

    There was no endpoint listening at that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

    What is this message?

    Thanks in advance

  6. FxM January 7, 2012 / 7:15 am

    DHANANJAY KUMAR ,
    Thanks. After a few other failed attempts from other how to’s I stumbled upon yours. I don’t know if
    the starts were aligned by then but it worked. Its good to see something work. Off to the next mountain :)

  7. sam February 1, 2012 / 2:12 am

    you saved me a lot of work here. Thanks so much

  8. Andreja February 9, 2012 / 10:23 pm

    Seriously i spent half day trying to deploy and this was piss easy!

    Question: Does Service Url needs to have different port number specified or can it be the same.
    When do I need this. For checking if service is published I can use localhost and port specified when App dir was set

  9. Alok February 25, 2012 / 6:49 pm

    I keep getting WcfService5.Service11′ does not implement interface member ‘WcfService5.IService1.GetMessage()’

    Am I missing anything here.

    Thanks

  10. Sachin Khot March 2, 2012 / 3:30 pm

    great kumar. Its awesome tutorial and helped me a lot. thanks a lot dude.

  11. REW March 9, 2012 / 11:16 pm

    My goodness, we really need more tutorials like this one! Great job!

  12. Carl Labuschagne April 23, 2012 / 12:49 pm

    Thank you! The only tutorial I could find which explains for beginners step by step.

  13. Dhananjay Kumar May 1, 2012 / 7:45 pm

    Glad to know that
    Thanks
    DJ

  14. Brent Allsop July 19, 2012 / 2:50 am

    Like everyone else, nothing was working till I used this walk through. Thanks!!

  15. Grateful User August 2, 2012 / 5:29 pm

    Man OH MAN….

    I am sure every one should agree that the folks at MSFT should be taking a look at this and seriously learning from it. The example on their page is simply mediocre at best

    Words cannot express how grateful I am for such a step by step, complete well rounded and well explained tutorial. Thank you so so so much.

  16. Ajad Patel September 18, 2012 / 5:01 pm

    this Artical Very Helpful for me.Thanks

  17. Rohit December 4, 2012 / 4:00 pm

    Excellent Article, take a bow……

  18. Mauricio Cuellar December 12, 2012 / 7:15 am

    Thanks for such detail article. I have followed and being able to get to the part where we get the WSDL definition. I am running into problems with getting metadata, both wfctestclient and the console app complain about this fact. If you can clarify why this happening is I will appreciate it. My email is macs@conssoft.net and again, thanks for sharing such a useful piece of information.

  19. Z December 29, 2012 / 4:37 am

    One of the best articles to start learning WCF. Thank you for your time.

  20. mikola January 9, 2013 / 7:17 pm

    Thank you!!!! very very much!!! =)

  21. John January 13, 2013 / 10:26 pm

    Great Article, helped me a lot.

  22. John Scanlon February 1, 2013 / 4:34 am

    Everything works fine until I get to “Add Service Reference”. I did everything exactly as specified in the walkthrough. When I follow the “Add Service Reference” procedure I get an error message as follows: “There was an error downloading metadata from the address. Please verify that you have entered a valid address.” – (yes, I’m positive the address is valid), And the Details section starts with “Metadata contains a reference that cannot be resolved: ‘http://localhost:4567/Service1.svc?wsdl’, and then later in the Details, “There was an error downloading ‘http://localhost:4567/Service1.svc?xsd=xsd0′. The underlying connection was closed: An unexpected error occurred on a receive.”

  23. Dharit February 1, 2013 / 12:43 pm

    Hello sir, i am new to wcf s and iis hosting, i recently tried hosting service on iis7 and it worked fine thanx alot for that. My question is will the same method work for iis8 which comes with windows and Visual Studio 10? or do i need to have some more tools for the..

  24. jason February 10, 2013 / 3:06 pm

    thank well done! i have being doing wcf for long time. but you hit it harder. goo teaching methods.

  25. Bikash Kumar Sahoo February 19, 2013 / 2:47 pm

    hi kumar,
    thanks for great useful of article u gave,
    can we do implement to develop service in one system and consume it another system …what will be the process ???

  26. megara February 23, 2013 / 1:17 am

    what about a wcf service that get info from a sql service DB?

  27. stelt April 24, 2013 / 7:31 pm

    Great walkthrough, everything easy and working, how simple it could be….I spend 2 days reading different articles on deploying, hosting, consuming WCF, but only this clarified everything. thanks

  28. Diego Melgarejo May 21, 2013 / 3:32 am

    What changes in web.config do I need to transform this WCF in a wsdualbinding?

  29. Amine October 8, 2013 / 9:52 pm

    Thanks a lot.

  30. vishal December 27, 2013 / 12:07 pm

    good article for freshers .. just want to know 1 things .. is this the only way to host service on IIS . can not we host the service in IIS from visual studio ?

  31. Ayyappan Sekaran January 2, 2014 / 2:35 pm

    thank u ……

  32. Darshan Kumar March 18, 2014 / 12:42 pm

    thank u. U are awesome :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s