Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » WCF » Walkthrough on creating WCF 4.0 Service and Hosting in IIS 7.5

Walkthrough on creating WCF 4.0 Service and Hosting in IIS 7.5

This article will give step by step walkthrough on how to create a basic WCF 4.0 Service, how to host WCF Service in IIS 7.5 and hot to test service in a client.

Author Rank :
Page Views : 7193
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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. 

1.gif

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

  1. So delete the data contract
  2. 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

  1. 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.

    2.gif

    Now you will have IIS open like below

    3.gif

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

    4.gif

  3. Now a window will open.

    5.gif

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

    6.gif

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

    7.gif

    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.

    8.gif

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

    9.gif

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

    10.gif

    Check the check box Start Web Site Immediately.

    11.gif

    And now click OK on the window.

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

    12.gif

    13.gif

    Type the below command

    aspnet_regiis.exe /iru

    14.gif

    You will get the message Finished Installing ASP.Net 4.0

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

    15.gif

Publishing the WCF Service

Go back to WCF Service. Right click and select Publish 

16.gif

On clicking of publish a window will come. 

17.gif

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. 

18.gif

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

19.gif

Leave the credential inactive. 

20.gif

Now click on Publish. 

21.gif

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.

22.gif

When you click on browse site will get open in browser 

23.gif

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. 

24.gif

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

Testing the Service at client

  1. Create console application project.

    25.gif

  2. Right click and add the service reference.

    26.gif

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

    27.gif

  4. 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 

28.gif

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dhananjay Kumar
I write few articles on Microsoft technologies. Read my blog at . | Mincracker MVP | Microsoft MVP
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Thannks you for this helpful article by m On December 7, 2010
Found it really helpful. Cheers!!
Reply | Email | Modify 
Very useful by Tamil On February 5, 2011
Hi, It is very useful article, save my time. thanx Tamil.
Reply | Email | Modify 
Thanks by Naseer On March 5, 2011
This very very helpful
Reply | Email | Modify 
Issue in IIS 6 for this by Sreenath On April 11, 2011
Hi, This was fantastic artical. But I am getting issue in browsing my service in IIS 6. Can you please help me out. I have followed the same steps.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.