Welcome to MSDN Blogs Sign in | Join | Help

Hosting WCF Service in IIS

 

  

All right!! So I have created a WCF service and I want to consume it…what do I do? Where do I host the service? The first choice that comes to one’s mind (and which most of the examples talk about) is creating a console application and hosting the WCF service within this application. But this option is good only when you want to host the service quickly and test it out. In a practical scenario, we would require a service which sits there and is available all the time and allows a message based activation. Also, wouldn’t it be great if we can get rid of the extra code that we have to write just to host the service? Fortunately, WCF provides various options when it comes to hosting a service. These options are:

1.       Console application

2.       Winform application

3.       IIS

4.       Windows service

5.       Windows Activation Service.

Of course, the choice of the host restricts the type of transport that you can use for that service. I will not go into those details in this post. Let us look at how we can host the WCF service in IIS. When we use IIS to host WCF services, the services are integrated into ASP .NET. Thus the WCF services can take advantage of some of the inherent feature of ASP .NET such as process recycling, process health monitoring, message-based activation and idle shutdown. So let us get started…

Create a WCF service

1.       Create a blank solution and add a WCF Service Library project to it. For the purpose of this post, I am going to create a simple GreetingService that has a method Greet. This method accepts a single parameter name and returns a string “Hello “ appended to name

 

Add a WCF Service to the solution

 

2.       The following is the code for the GreetingService WCF service. We define a service contract, mark the operations in that contract that are going to be exposed to the external world and then implement the service.

 

  

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

 

namespace Wcf.Samples.ServiceLibrary

{

    [ServiceContract()]

    public interface IGreeting

    {

        [OperationContract()]

        string Greet(string name);

    }

}

GreetingService.cs

using System;

using System.Collections.Generic;

using System.Text;

 

namespace Wcf.Samples.ServiceLibrary

{

    public class GreetingService : IGreeting

    {

 

        #region IGreeting Members

 

        public string Greet(string name)

        {

            return "Hello - " + name;

        }

 

        #endregion

    }

}

 

 

  

3.       Now that you added the service contract and its implementation (the GreetingService), build the project.

 

Host the service in IIS

1.       Now we create a Web site that will host the WCF service that we created in the previous section. For this, right-click the solution in the solution explorer and from the context menu, select Add -> New Web site  ->WCF Service as shown in the following figure.

 

Add a new WCF Service web-site

 

2.       This will create a WCF Service Web site into your solution along with the standard folders that get created for a Web site project (App_Code, App_Data etc). A new type of file called Service.svc will be generated and placed into the root of the Web site project, along with the corresponding code-behind file (Service.cs). The Service.cs is provided to implement a WCF service, which then can be referred in the Service.svc file. Since we already have created the WCF service in a separate assembly, we will refer to it in the Service.svc. As such we don’t need Service.cs and hence, it can be deleted.

3.       Open Service.svc file and modify the single line in it like this:

<%@ ServiceHost Language="C#" Debug="true" Service="Wcf.Samples.ServiceLibrary.GreetingService" %>

4.       In the above statement we point to the fully-qualified class name that implements the service that we want to host in IIS.

5.       Add the reference of the Wcf.Samples.ServiceLibrary project to the web-site project and build the web-site.

6.       This completes the creation of WCF service. Now, in order to let the service communicate with the external world, we need to define the communication behavior of the service. For defining this behavior, we will use the Service Configuration tool that comes along with Visual Studio 2005.

7.       From the main menu of Visual Studio 2005, select Tools -> WCF Service Configuration Editor. The WCF Configuration Editor window will open up. Select File -> Open -> Config File… from the main menu. Browse and select the Web.config file of the WCF web-site.

8.       Once selected, the screen will be displayed as follows. The Web.config already has the configuration for the default service MyService under the Services node in the left-panel. Select this service and deleted it. We are going to create a new configuration for our service.

 

 

WCF Service Configuration Editor

 

9.       Right-click the Services node and select “New service” from the context menu. A service with service type NewServiceType will be created. On the right-panel, select the property “Name” and click the ellipsis. Service Type Browser will open up. Browse the bin folder of the web-site and locate the service assembly Wcf.Samples.ServiceLibrary and double-click it. The Service Type Browser dialog now will list the service “Wcf.Samples.ServiceLibrary.GreetingService” service. Select this service and click Open. This will set the service-type that we are going to configure.

10.   Now for the above service, we first need to specify the end-point. Right-click Endpoints node in the left-panel of the WCF Service Configuration Editor and select “New Endpoint”. This will create a default end-point with its properties being displayed in the right-panel.

11.   Set the following properties:

 

Name

defaultEp

Address

http://localhost:28053/GreetingService/Service.svc

·         Here 28053 is the port-number where the local web-server is running. You need to check your port number and enter it here appropriately.

Binding

basicHttpBinding

Contract

Wcf.Samples.ServiceLibrary.IGreeting

·         You can click the ellipses to open the Contract Type Browser and select the appropriate assembly and contract from the bin directory of the web-site.

 

12.   This sets the basic communication for our service. In order to enable the service for metadata exchange (thereby allowing us the browse its wsdl), we need to set the metadata exchange properties for the service. To do this, expand the Advanced node in the left-panel, right-click Service Behaviors and select “New Service Behavior Configuration”. This will add a new Behavior Configuration by the default name NewBehavior. Behavior is a collection of attributes (here, service attributes) that can be set and applied to the service together. Right now, we are going to define a behavior that allows the metadata exchange on the service. Set the Name property of the new behavior configuration to mexBehavior.

13.   In the “Behavior element extension position” (lower part of the right-panel), click “Add” button. This opens up a dialog “Adding Behavior Element Extension Sections”. Select serviceMetadata from the list and click “Add” on the dialog. This adds the extension serviceMetadata to the grid in the right-panel. Double-click the extension to open up its property-page. Set the property HttpGetEnabled to true.

 

Setting the service behavior

 

 

14.   Now that we have defined the behavior separately, we need to associate the behavior with our service. To do this, select the Wcf.Samples.ServiceLibrary.GreetingService under the Services node in the left panel. The right-panel will display its properties. Select the BehaviorConfiguration property, and select “mexBehavior” from the drop-down.

15.   This sets the service configuration and allows the service to communicate with the external world. Save the configuration by selecting the menu File->Save. Close the WCF Service Configuration Editor.

16.   Test that the service is hosted by running the WCF Web Site application. The following screen should be displayed.

 

WCF Service Test page

 

You can test this service by creating a Console application and adding a proxy (generate the proxy by running the svcutil.exe utility). In the next post, I will explain how to invoke a WCF service hosted in IIS through the new WCF Adapter in BizTalk R2.

 

Till then… happy coding J

 

 

 

Published Monday, May 21, 2007 4:26 PM by Amit Lale
Filed under: , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Monday, May 21, 2007 7:18 AM by Hosting WCF service in IIS « Tuff Stuff

# Hosting WCF service in IIS &laquo; Tuff Stuff

Monday, May 21, 2007 9:05 AM by arun

# re: Hosting WCF Service in IIS

Good stuff!

Tuesday, May 22, 2007 10:02 AM by Jigar Mehta

# re: Hosting WCF Service in IIS

Thanks a lot for step by step instructions.

Its really good when somebody is starting to program in WCF, like me!! :)

Monday, June 04, 2007 9:30 PM by Scott Middleton

# Getting Started with WCF

With the new version of TeamGuide using a WCF service to distribute the Process MeNtOR content to our...

Tuesday, June 12, 2007 3:03 PM by kavit

# re: Hosting WCF Service in IIS

The contract name 'Wcf.Samples.ServiceLibrary.IGreeting' could not be found in the list of contracts implemented by the service 'GreetingService'.

Tuesday, June 12, 2007 3:03 PM by kk

# re: Hosting WCF Service in IIS

Help meee....!!!!!!!!!!

I am geeting error like this..

The contract name 'Wcf.Samples.ServiceLibrary.IGreeting' could not be found in the list of contracts implemented by the service 'GreetingService'.

Sunday, July 15, 2007 6:59 PM by Solon

# re: Hosting WCF Service in IIS

interesting

Tuesday, August 21, 2007 3:35 AM by General

# re: Hosting WCF Service in IIS

I`m getting error

No protocol binding matches the given address 'http://localhost:8000/GreetingService/Service.svc'. Protocol bindings are configured at the Site level in IIS or WAS configuration.

Wednesday, September 26, 2007 8:29 AM by naruto music videos myspace codes

# naruto music videos myspace codes

naruto music videos myspace codes

Friday, October 12, 2007 3:24 AM by seshadri

# re: Hosting WCF Service in IIS

Hi, if I add a new method in the service, then i removed service refference in the client and add a new reefference to that using service url. but it is accepting only old method in the proxy, i am unable to find my new method in the service. can you solve my problem. i have thought this due to caching. i have tried with new service project then my second method is accessing..

Thanks in advance

seshadri

Wednesday, October 24, 2007 4:46 AM by Divya

# re: Hosting WCF Service in IIS

Great stuff! THis works fine in VS2005(whish uses Cassini). If i try the same stuff in IIS 6.0 from my WIndows XP 64-bit, i get the error:

HTTP Error 401.1 - Unauthorized: Access is denied due to invalid credentials.

Internet Information Services (IIS)

Not sure why because I even tried having the aspnet account with administrator priveleges. It would really help if you could give us detailed steps on what we need to do to have things running on IIS?

Thanks for putting together this article in first place!

Friday, October 26, 2007 8:47 AM by Arvind Robin Kumar

# re: Hosting WCF Service in IIS

I got error when I host this in windows xp.

Thanks in advance...

Friday, October 26, 2007 10:35 AM by Arvind Robin Kumar

# re: Hosting WCF Service in IIS

That too if it is placed in virtual directory, access denied kind of error came.

Tuesday, November 06, 2007 4:52 PM by Carlos DeAngulo

# re: Hosting WCF Service in IIS

Your example is great, and works perfect... however, in the step to create a Web site that will host the WCF service, you have selected the Location to create this website in the FileSystem rather than HTTP. If you run through this same example, and select HTTP the example does NOT work anymore. Can you please explain why, and do you know how to fix it? Thank you for your help and time.

Wednesday, November 21, 2007 4:02 PM by Ram

# re: Hosting WCF Service in IIS

Is it possible to initialize and access a global object which will be accessed by multiple client requests? Say for example, we want to know the inventory of an item in the ware house. Inventory of all items is read from database and stored in a global memory object during the first client request or creating the WCF host. Subsequently, if any user  logs in, WCF should access this global memory object and return the inventory for the item requested without reading from the database again.

I am not able to initialize this object and each time a user requests comes, the whole process started all over again. Thanks in advance for your help

Wednesday, November 21, 2007 7:27 PM by Ram

# re: Hosting WCF Service in IIS

Sunday, December 16, 2007 2:32 AM by gnobber

# re: Hosting WCF Service in IIS

Do you need to create an .svc file for each service that you want to host in IIS? If so, how would you go about extracting the combined proxies and config of each service?

Thursday, December 27, 2007 7:52 AM by Arvind Kumar

# re: Hosting WCF Service in IIS

I made a WCF service library and use it in a wcf service but when i run this service i got the following error:

There is no compatible TransportManager found for URI 'http://localhost:2755/GreetingService/Service.svc'. This may be because that you have used an absolute address which points outside of the virtual application. Please use a relative address instead.

Can anyone help me to figure it out?

Tuesday, February 05, 2008 6:27 AM by Ramakrishnan

# re: Hosting WCF Service in IIS

Can you please let me know What is the difference between hosting the WCF application in IIS and other application?

Thursday, March 06, 2008 4:31 AM by rohini

# re: Hosting WCF Service in IIS

hi,

the article was nice.when i am trying to implement I`m getting this error

No protocol binding matches the given address 'http://localhost:8000/GreetingService/Service.svc'. Protocol bindings are configured at the Site level in IIS or WAS configuration.

Thursday, March 06, 2008 4:31 AM by rohini

# re: Hosting WCF Service in IIS

hi,

the article was nice.when i am trying to implement I`m getting this error

No protocol binding matches the given address 'http://localhost:8000/GreetingService/Service.svc'. Protocol bindings are configured at the Site level in IIS or WAS configuration.

please help me in that

Saturday, March 15, 2008 8:48 AM by surresh

# re: Hosting WCF Service in IIS

sir,how to consume the wcf service using asp.net

Tuesday, March 18, 2008 2:02 PM by Ehsan

# re: Hosting WCF Service in IIS

To add a little more to this great thread is to publish the service in an IIS VD and then using it just a we use an ASMX file and consume it in an asp .net app by web refrencing it.  If we use the publishing route, we have to update the end point not to enclude the port number in its configuration.

Friday, March 28, 2008 10:37 AM by Andrew

# re: Hosting WCF Service in IIS

I also get the same issue that other readers mentioned. Is it something to do with IIS pointing to the wrong version of the Framework? Mine appears to point to 2.0, but 3/3.5 isn't available in the dropdown list.

No protocol binding matches the given address 'http://localhost/GreetingService/Service.svc'. Protocol bindings are configured at the Site level in IIS or WAS configuration.

Monday, March 31, 2008 11:15 PM by Tim W

# re: Hosting WCF Service in IIS

No protocol binding error is normally due to your configurations (both server and client).  The server will use a binding like basicHttp and your client should use the same.  

Thursday, April 03, 2008 12:36 PM by Ravindra Dewangan

# re: Hosting WCF Service in IIS

Oops !!! "Server Application Unavailable" error page when tried navigating to the "Services.svc" file in the service's virtual directory, through browser.

Later, registed the apsnet_wp (using aspnet_regiis -i -enable) for .Net 2.0 and it goes well.

Thursday, April 10, 2008 2:30 AM by Priya

# re: Hosting WCF Service in IIS

Thanks a lot for the details.

Very informative

Sunday, April 20, 2008 12:30 AM by alik levin's

# WCF Security Resources

This is a digest of WCF Security resources I was collecting for some time. Drop me a comment in case

Friday, May 02, 2008 5:44 AM by Bipin

# re: Hosting WCF Service in IIS

This is a very good article... as a starting i found it very usefull..

Can u plz help me on Hosting MSMQ service.

Because endpoint get changed here..same for protocol..

Thanks !

Sunday, May 11, 2008 12:23 PM by 江南白衣

# WCF Security Resources(转)

This is a digest of WCF Security resources I was collecting for some time. Drop me a comment in case it is useful.

Friday, May 16, 2008 9:35 AM by bache

# re: Hosting WCF Service in IIS

I was getting the same error message "No protocol binding matches the given address 'http://localhost:8000/GreetingService/Service.svc'. Protocol bindings are configured at the Site level in IIS or WAS configuration." as General and rohini.

The problem was that this address http://localhost:8000/GreetingService/Service.svc was different from the address specified in the web.config in endpoint section. So check your web.config. You can configure the website not to use the default Web server but to use IIS in the properties of the website.

Thank you for the tutorial!

Tuesday, May 27, 2008 2:17 AM by Thirumal

# re: Hosting WCF Service in IIS

How can we use IPC binding to host a service in WAS

Wednesday, June 25, 2008 3:30 PM by YourMomma

# re: Hosting WCF Service in IIS

Remove any addresses from your web.config, address="" and mex address="mex"

Let IIS set the URI based on where you place the files.

Monday, July 14, 2008 6:41 AM by cvetan

# re: Hosting WCF Service in IIS

Thank you, that helped me a lot :)

Monday, July 21, 2008 3:29 AM by Aditya k

# re: Hosting WCF Service in IIS

Hi, Amit this article was really useful and easy to understand.

Monday, July 21, 2008 6:46 AM by Charly

# re: Hosting WCF Service in IIS

Hi,

are there any restrictions when an iis wcf service consumes other wcf services?

client -> wcf service (iis, http) -> wcf service (self hosted, net.tcp/named pipe)

Can I use any communication protocol between the 2 wcf services?

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker