Thursday, 20 February 2014

Creating a WCF Service

How to Create a Basic WCF Service

In this demonstration you will see how to create a basic Window’s Communication Foundation service.
Let’s go ahead and create new console-based application using C# language called WCF demo.

We need to add a reference to the system.ServiceModel name space, as well as a using statement at the top of our code module.

Then we can get started by first creating an interface in our program, and we will call this interface IGreeting. IGreeting will contain one method GetGreeting that will return a string value and accept a string value.

In order to make our interface a contract for communication service, we need to apply the necessary attributes. In this case, we apply the ServiceContract attribute to the actual interface itself, and any methods that we have inside of our interface need to have the OperationContract attribute applied to those as well.

So now our interface is complete, and we essentially have the workings of our contract in place. We will now create a class called Greeting that will implement the IGreeting interface.

And we will add a functionality here to simply return a simple text string concatenated with the value that gets passed in, and we are all set to go ahead and start creating our service host.

So we will create a ServiceHost instance. Note that we have two options available here: we can use a singleton instance of an object, or we can get at the type of our specific service and utilize that.

We will use the typeofGreeting, and we will create a new URI because we are actually hosting this on our local machine. The URI will consist of local host with a port number, and of course, our service name.
We can now open our host, and we can let the user know that the host is actually up and running.

And we can inform the user how they can shut down our host, and wait for Visual Studio or the user rather to press the Enter key, and at which point in time, we will close our host and the service will shut down gracefully.

Now that we have completed writing the code for this, we need to go ahead and build our project so that we can actually create the executable file, which will be required in our next step.

So now that we have created that functionality, we need to go to our project and we need to add an App.config file.

So we will add the Application Configuration file to our project, and you will currently notice that App.config is very generic and only has the opening and closing tags with nothing in between.

We are going to go ahead now, and go through the process of running the Edit WCF Configuration wizard, which will allow us to apply the necessary settings to our App.config file.

The first step is to create a new service. This is where we had to build our project first so that our WCFDemo.exe does exist, and we can get at our service that we have created inside. Also note that we now want to look at the service contract that we will be using because we only created the one interface within our application with the attributes applied to it, IGreeting is the only one that shows up.

This time we will base our communication mode on TCP rather than HTTP. Again it was local host. We are going to specify a port of 1234 this time around, and Greeting is the address for the Endpoint of our particular service.

So the wizard has created that for us. We will click on finish. Note that we have our hosting Endpoints available.

We need to next create a Service Behavior, and our Service Behavior Configuration we are going to utilise the serviceMetadata element.

We will add the serviceMetadata element to our Service Behavior. Note that it is called NewBehavior. We come back up to our WCFDemo.Greeting service, apply the new behaviour to that, save the file, and then go back to Visual Studio and very quickly open our App.config file, and you can see the changes that have taken place inside our App.config file.


So let’s go ahead and run this and see how our application behaves. Currently we have no clients that will be accessing this. The service just simply comes up and runs. It says the service host is running, press ENTER to close the host, and we are finished.

No comments:

Post a Comment