Tuesday, May 8, 2012

Your first WCF Service - Step by step

Today I'm gonna make a demo for those who want to step into the world of WCF. I'm not going into details about what is WCF. In short, it is a platform to build distributed applications using a unified modeling language.

Step 1. Create a new WCF Service Application
Open Visual Studio -> File-> New -> Project. Select from the WCF panel a WCF Service Application and give it a name. Mine is called Service1.

Step 2. Configure the service using the confiugration file
In the newly created project double click the Web.config file. Here we will have to add a service element an endpoint element and a binding. This step is not mandatory because Wcf creates by default an endpoint and a service, but for the sake of this exercise I will create new elements. 

The Binding - this is where we specify what protocol do we use (http, tcp, etc.), how our data is formatted, what security do we use for sending the message and other things. So first we create a bindings element where we will use a wsHttpBinding like so:
<binding name="MyBinding">

<security mode="None">


The Service - here we add our endpoints and configure the service behavior if we have one


The EndPoints - The endpoint is a structure where we have to specify the Address, Binding  and Contract.
Address - the location (url) where we find the endpoint
Binding - the protocol and other settings that are used to transport data
Contract - the interface (set of operations) that is exposed through this endpoint.
Inside the <service> tag add the definition of the endpoint:
<service name="WcfServiceBlog.Service1">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyBinding" contract="WcfServiceBlog.IService1">

Our service is ready.

Step 3. Create a client that consumes the service
Create a new console application. Expand the project, Right click on References -> Add Service Reference
A popup should appear where you have to enter the url of the service. Because the service is in the same solution you can click the Discover button.

After your service is discovered, select it and click Ok. In thi moment the tools of visual studio would create the necessary proxy classes so that we can use the service.
In the Main program write the following code:
static void Main(string[] args)
using (ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client())

Set your client application as a startup project. Right click on project -> Set as startup project.
Run your first wcf service application! :)

Friday, February 24, 2012

WCF NetTcp in IIS

Today I will make a short demo about hosting wcf services with nettcpbinding in iis.
There are cases when is more suited for your service to use a nettcpbinding for different reasons: speed, reliability, etc.
But in this case you are limitted to choose the hosts of your service from console app, windows service. So what if you want to host your service in iis and still expose nettcpbindings ?

So here are a few stept that must be done before we can use our service. For a more in-depth description check this post IIS 7.0 and WAS

Step 1. Enable Non-Http protocols
Go to Control Panel->Programs->Turn Windows features on/off and under .NetFramework 3.5.1 be sure to tick "Non-Http activation" (remember that iis supports only http)

Step 2. Enable WAS and PortSharing.
The previous step will make available some services (wcf services by the way) that will help us. So go now in your services list: Task Manager->Services tab and be sure that the following services are running:

WAS - is the Windows process activation service
NetTcpPortSharing -  is the WCF TCP port sharing service. It implements a centralized TCP listener so that multiple processes can listen on the same TCP port. This service is available even if IIS 7.0 is not installed.
NetTcpActivator - is the WCF TCP Activation Service. It communicates TCP activation requests to WAS.
Step 3. Create a WCF Service with a NetTcpBinding and create the endpoints.

Create the binding in the bindings tag, and then create 2 endpoints. One for your contract and one mex.
Note: If you don't create the mex endpoint, you won't be able to add a reference in your project.
Step 4. Publish the service in iis and configure it

Right click on the service->Publish. Be sure to tick 'Mark as IIS application'
In IIS, expand your newly created application, go in content view and browse your service. You will get this error:
This tells us that currently we don't have support for tcp protocol so here is what you have to do in order to eneable it: in iis, right click on application->Manage Application->Advanced Settings and add "net.tcp" next to http and then click ok.
 Now the error should be gone
Step 5. Add a reference to your project
And that's it :).