jump to navigation

How to configure WAS for TCP endpoints in IIS March 5, 2012

Posted by fofo in C#, Visual Studio 2008, Visual Studio 2010, WCF, WCF Service.
Tags: ,
3 comments

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.

1) We need to create our WCF Service first.We will use the exact same example we used when we hosted a WCF Service in IIS (full version) .Click here to see this post. You have to follow all the steps as they were described in that post. After you finish all the steps in that post you will have successfully hosted a WCF Service in IIS.

2) I will show you how to install WAS in your machine.Go to the Control Panel–> Program and Features and select Turn Windows features on or off.Then you need to select some options/components to install. Have a look at the picture below to see what those options are

3) Now I need to run some commands to enable TCP and other protocols on the Default Web Site. I will do that using the appcmd command.Open a Visual Studio 2010 command prompt as an administrator and browse to this folder C:\Windows\System32\inetsrv. Then type appcmd.exe set site “Default Web Site” -+bindings.[protocol=’net.tcp’,bindingInformation=’808:*’] and hit Enter.Then in the new command prompt type appcmd.exe set site “Default Web Site” -+bindings.[protocol=’net.pipe’,bindingInformation=’*’] and hit Enter. Then in the new command prompt type appcmd.exe set site “Default Web Site” -+bindings.[protocol=’net.msmq’,bindingInformation=’localhost’]

Hopefully these commands will execute successfully and these protocols are enabled on the Default Web Site.Now we need to enable those protocols on the CalculatorServiceSite website that hosts our WCF service.In the same Visual Studio 2010 command prompt window type

appcmd.exe set app “Default Web Site/CalculatorServiceSite /enabledProtocols:http,net.pipe,net.tcp,net.msmq

Now we have enabled those protocols for our site.Now we are ready to expose endpoints over alternate protocols e.g TCP,MSMQ. In this way we can have our WCF Service hosted in IIS and still consume it over the network with clients that support/understand the TCP/IP protocol.

Hope it helps!!!

Hosting WCF Services in IIS March 3, 2012

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

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!!!