How to Create WCF 4.0 Service and Hosting in IIS 7.5

No.of Views6998
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  09 Sep 2010 10:09:25
Tag : WCF , Miscellaneous
In this article, i have walkthrough on creating WCF service in .NET 4.0 and host in IIS 7.5
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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. 

Image Loading

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

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="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 .

Image Loading

Now you will have IIS open like below

Image Loading

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

Image Loading

c.    Now a window will open.

Image Loading

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

Image Loading

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

Image Loading

 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. 

Image Loading

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

Image Loading

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

Image Loading

 Check the check box Start Web Site Immediately.

Image Loading

 And now click OK on the window.

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

Image Loading

 

Image Loading

Type the below command  aspnet_regiis.exe /iru

Image Loading

 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.

Image Loading

 

Publishing the WCF Service

Go back to WCF Service.   Right click and select Publish

Image Loading

 On clicking of publish a window will come.

Image Loading

 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.

Image Loading

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

Image Loading

 Leave the credential inactive.

Image Loading

 Now click on Publish.

Image Loading

 

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.

Image Loading

When you click on browse site will get open in browser

Image Loading

 

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.

Image Loading

 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. 

Image Loading

b.    Right click and add the service reference.

Image Loading

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

Image Loading

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

Image Loading

Conclusion

In this article we saw,

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.

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

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:subhashDate Of Posted:11/21/2012 8:05:55 AM
The web application could not be found
hi dhananjay, i refered this code for writing sp 2010 object model code in wcf, but the error could not found web applicaiton "htttp://xxxx" still exists......... Any ideas would be greatly appreciated.........
By:;'Date Of Posted:5/31/2011 4:09:15 AM
;/k
k,.il.lyi.
By:RRaveenDate Of Posted:9/11/2010 10:29:51 AM
Great Article
Hi Dhana, You have nice done nice job,its really great article
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top