WCF Tutorial

Working with the Interface:

We need a service contract to create a new service. The service contract is the interface of the service. It consists of all the methods which are exposed   along with input parameter(s) and return value.  

Interface IService1.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace WcfServiceLibrary1

{

    [ServiceContract]

    public interface IService1

    {

       

        [OperationContract]

        int Addition(int x,int y);

        [OperationContract]

        Customer GetDataUsingDataContract(Customer cust);

    }

    [DataContract]

    public class Customer

    {

        String name;

        string contactNo;

        [DataMember]

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        [DataMember]

        public string ContactNo

        {

            get { return contactNo; }

            set { contactNo = value; }

        }

    }

}

In the following class we can see that interface IService1is implemented.

Service1.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace WcfServiceLibrary1

{

    public class Service1 : IService1

    {

      

        public int Addition(int x, int y)

        {

            return x + y;

        }

        public Customer GetDataUsingDataContract(Customer cust)

        {

            cust.Name = “Sanjit”;

            cust.ContactNo = “1234567”;

         

            return cust;

        }

    }

}

Note: The <ServiceContract()> attribute is used to define the class or interface as the service class and it needs to precede the opening declaration of the class or interface. The method defined in the interface is exposed through the WCF service as part of the service contract and hence <OperationContract()> is applied in the method. The data  contract uses the [DataContract] and [DataMember] attributes.

Consuming the WCF service:

Create a new project named ConsumeWCF where Service1 is added using the Add Service Reference option from References in the Solution Explorer.

The below code snippet shows how to consume the service method:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

namespace CosumeWCF

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            MyServiceRef.MyServiceClient MS = new CosumeWCF.MyServiceRef.MyServiceClient();

            int value=MS.Addition(18, 35);

        }

    }

}

How to view wsdl file?

In order to view the wsdl file right click on Service1 and then select View in Browser and from there wsdl can be accessed.

Conclusion:

With the advent of WCF, communication between applications and systems has become much easier. WCF is a means to build distributed applications in a Microsoft environment, clients however may or may not use any Microsoft technology in order to consume WCF services.


Pages: 1 2




Array

No comments yet... Be the first to leave a reply!