Working with WCF Services

Today , I started with creating a WCF Service for a learning process and quite enjoyed working around it .. Well just a bit of twist used was that I used a MVC Application to call the WCF Service .. Infact there are number of other ways in which one can consume the service. For eg using a console app,windows,asp.net web app etc
Question is
 How do I start ?
Answer lies in these 4 steps
1.  Create a WCF Service using Visual Studio
2.  Host the WCF Service on IIS
3.  Reference the Service from the Client app
4.  Create a Client application using visual studio

So lets start with the 1st one now 

1. Create a WCF Service using Visual Studio

This is quite simple ,, just create a new project ( WCF Service) under your visual studio and name it say WcfService.When you open the solution explorer you will find the Service1.svc and IService1.cs has been built already for you. Here IService1 is a interface already built for us.

Let me now open my Service1.svc.cs .I have put in some piece of code as  follows . One will also see some other part of code snippet along with this ,but since we are not going to use it I have removed it from my Code ..  
namespace WcfService
{
       
 public class Service1 : IService1
    {
        public string HelloWorld(string name)
        {
            return "You are logged in as  " + name;

        }

    }
}

As of now we are just be interested in the method called HelloWorld . Lets also have a look in our Iservice1.cs ( Interface)

namespace WcfService
{
   
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string HelloWorld(string name);

      
    }
}

This is very basic and simple Service I have created so far .. The Attribute[ServiceContract]
 over here describe what service will do .
[OperationContract]over here will specify that we can use this HelloWorld string in our app.If we do not specify [OperationContract] ,we will no longer be able to use it when we run the application ..
Now lets run the application and see how it would look like 


As you see the figure here,, the service is now up and running .. 

2. Host it on Your IIS

Well as of now we have hosted this service on our localhost, so when I close the Service I wont be able to no longer use it. In order to make use of it even if it is closed, Let me host it on IIS
For eg I have already created a Hosted Web Service  in my IIS..
http://localhost:82/Service1.svc
For more inform on hosting WCF Services on IIS, one can check the following link below, which will give you in detail how to go about it

2. Reference the Service from the Client application

Create a new Project and add Service Reference by right clicking on the Website 





The address bar will be empty initially, we can add the hosted address in our address bar and hit Go, which will bring the Service1 service which we created.

4. Create a Client application using visual studio for Testing

In my case I am using MVC Application, and have created a Method called GetWCF () which calls the service
using System.ServiceModel;
using System.Runtime.Serialization;
using WebAppTemplate.UI.Web.ServiceReference1;

public class HomeController : Controller
       {
        //Method which calls WCF Service
        public string GetWCF(Service1Client client)
       {
           
            client = new Service1Client("BasicHttpBinding_IService1");

            return client.HelloWorld("Muhhahaha");
           
        }
         }
As you see we have a ServiceClient object in our method which will accept Endpoint name as “BasicHttpBinding_IService1” which can be found in the webconfig file
<client>
      <endpoint address="http://localhost:82/Service1.svc" binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
        name="BasicHttpBinding_IService1" />
</client>


Last and not the least I have used a html.actionlink to call my service when I click on it J

 @Html.ActionLink("Call to WCF Service", "GetWCF","Home", new Service1Client())




No comments:

Post a Comment