jump to navigation

Hosting WCF Services in IIS March 3, 2012

Posted by fofo in C#, Visual Studio 2010, WCF, WCF Service.
Tags: , ,
trackback

I am going to start a series of posts regarding the various options we have when we want to host a WCF Service. I am going to show you (in seperate posts) how to host WCF Services in IIS, in Window Services and self-host them.

In this post I will show you to host WCF Services in ASP.Net Web Applications in IIS.I will use a simple a WCF service that just completes some basic math tasks.The main focus in this post is not to implement a very complicated WCF Service. The main focus is demonstrating how to host the WCF Service in IIS.

Let’s build the WCF Service first.

1) Launch Visual Studio. From the available templates use “WCF Service Library”. Choose a suitable name for that project.I have named mine WCFCalculatorWebHost. I will use C# as the development language.

2) Rename the IService.cs and Service.cs to ICalculator.cs and Calculator.cs

3) I will not be deleting anything from the existing code in those files. I will add some code in the ICalculator.cs

[ServiceContract]
public interface ICalculator
{

[OperationContract]
double Add(double a1, double a2);

[OperationContract]
double Multiply(double a1, double a2);

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

4) Now we need to implement the methods we wrote in the ICalculator.cs file.


public class Calculator : ICalculator
    {

        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }

        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }

        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }

5) Build and run your application.Visual Studio will launch the WCF Test Client  so we can test the service.The service will be hosted in the WCF Service Host application that VS launches automatically.Test that the service works as expected.

6) Now we are ready to host the WCF Service in an ASP.Net web application.Add a new website project to your solution.Αdd a WCF Service template.Give it an appropriate name. I have named it CalculatorServiceHost.Delete the IService.cs and Service.cs files from the App_Code special folder.

7) Add a reference to the WCF Service in the CalculatorServiceHost project.Right-click to the CalculatorServiceHost and simply choose add a reference and browse until you find the WCF Service and click OK in the dialog box.

8) Now I need to modify the Service.svc file in my hosting application project. I rename it firstly to Calculator.svc. The contents of this file are something like this

<%@ ServiceHost Language="C#" Debug="true" Service="WCFCalculatorWebHost.Calculator" %>

We have to locate the service in the Service attribute.What we have done now is to tell ASP.Net how to map requests to the .svc file to the WCF Service Library assembly.

9) Select the CalculatorServiceHost project and hit Ctrl+F5.The service will be hosted in the ASP.Net development server.In my case the address (the service is hosted) is

http://localhost:18578/CalculatorServiceHost/Calculator.svc

Have a look at the picture below to see what I mean.

10) Now we need to launch the test client.Open a Visual Studio command prompt window and type

wcftestclient http://localhost:18578/CalculatorServiceHost/Calculator.svc and hit Enter. It is going to talk to our service and find its metadata.It is going to launch the familiar test client so we test the service. Have a look at the picture below.

11) Now we need to host the service inside IIS ( the full version ). I assume that you have it installed or know how to install it. If you need any help have a look at this post.

12) Go to Start–>Run and type inetmgr to launch the IIS console.Go to the Default Web Site inside the console and add an Application.Give in a name in the Alias field. I have named it CalculatorServiceSite.Then in the Physical Path browse to the application (CalculatorServiceHost in my case) we created previously to host the WCF service in the ASP.Net development server and click OK. Have a look at the picture below.

Now choose CalculatorServiceSite and switch to the Content View.There you can find the Calculator.svc file.Right-click on this file and hit Browse.Now notice that I am hosting inside the full version of IIShttp://localhost/CalculatorServiceSite/Calculator.svc.Notice there is no port number.Have a look at the picture below.

13) Now we need to launch the test client again.Open a Visual Studio command prompt window and type

wcftestclient http://localhost/CalculatorServiceSite/Calculator.svc and hit Enter. It is going to talk to our service and find its metadata.It is going to launch the familiar test client so we test the service. Have a look at the picture below.

I have demonstrated how to host the WCF Service we created, in IIS and then use the default test client application provided by the VS to talk to it and test its functionality.You could obviously build your own client and test it.

Email me if you need the source code.

Hope it helps!!!



About these ads

Comments»

1. Dot Net Rules : Hosting WCF Services in IIS - March 3, 2012

[...] a very complicated WCF Service. The main focus is demonstrating how to host the WCF Service in IIS.(read more) Share Posted: Σάββατο, 3 Μαρτίου 2012 8:34 μμ από το μέλος [...]

2. How to configure WAS for TCP endpoints in IIS « DOT NET RULES - March 5, 2012

[...] In this post I would like to show you how to activate Windows Activation Services or WAS for TCP based activation.What this means is that it is possible to host WCF Services in IIS and expose TCP endpoints. Recently I have posted 3 posts on WCF hosting options. You can find them here , here and here. [...]

3. new school culinary - April 2, 2013

Hello! I’ve been following your blog for a long time now and finally got the bravery to go ahead and give you a shout out from Dallas Tx! Just wanted to say keep up the excellent work!

4. Alfurqan - May 4, 2013

Very Useful.

Thank you

5. ender saraç - May 25, 2013

I do not know whether it’s just me or if everyone else experiencing problems with your website. It looks like some of the text in your content are running off the screen. Can somebody else please provide feedback and let me know if this is happening to them as well? This may be a problem with my internet browser because I’ve had this happen before.
Cheers

6. lui - July 5, 2013

Thanks a lot. Excellent demonstration, and very helpful

7. twitter.com - March 4, 2014

I drop a comment when I especially enjoy
a post on a website or if I have something to
contribute to the conversation. It is caused by the fire communicated in the post I browsed.
And after this post Hosting WCF Services in IIS | DOT NET RULES.
I was moved enough to drop a thought ;-) I actually
do have 2 questions for you if you don’t mind. Is it simply me or do a few of these comments appear like they are left by
brain dead people? :-P And, if you are writing on other social sites, I’d like to keep up with anything fresh you have to post.
Could you list the complete urls of your shared pages like
your twitter feed, Facebook page or linkedin profile?


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

Follow

Get every new post delivered to your Inbox.

Join 1,786 other followers

%d bloggers like this: