Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Creating WCF Service with wsHttpBinding and webHttpBinding together

In this tutorial, I am going to explain you how to create .NET WCF Service with wsHttpBinding and webHttpBinding together in single WCF application. Below topics will be covered in this tutorial.

  • Creating new WCF Service application project
  • Creating simple calculator service
  • Creating complex / CompositeType calculator service
  • Configuring service with wsHttpBinding and webHttpBinding
  • Calling simple and complex service using .NET application by creating Proxy Class (wsHttpBinding)
  • Calling simple and complex service using REST client like POSTMAN (webHttpBinding)
  • Calling simple and complex service using jQuery (webHttpBinding)

Creating new WCF Service application project

I will be using visual studio 2013 and .NET framework 4.5.
Go to FileNewProject. A new window will be open as shown below.
Now go to Visual C# and search for WCF Service Application and give project name and click on OK .

Now a wcf service will be created with default template as shown below.

1) Now rename IService1.cs to ICalc.cs and Service1.svc to Calc.svc.
2) Now right click on Calc.svc, go to View Markup and change service from Service="WcfService1.Service1" to Service="WcfService1.Calc".

Creating simple and Complex calculator service

Now open ICalc.cs file, delete all the default code generated and replace its with new code as shown below. Here we have declared two method Calculator1 and Calculator2 inside inteface, both are decorated with POST verb and returns Json data. Calculator1 method accepts two integer parameter where as Calculator2 method accepts a complex class objects and both returns addition of two numbers.

ICalc.cs Code
using System;
usingSystem.Collections.Generic;
using System.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface ICalc
    {
        //simple calculator service
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string Calculator1(int FirstNumber, int SecondNumber);

        //complex / CompositeType calculator service
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        Calculator2OutputCalculator2(Calculator2Input Calc2Input); // input and output is of some CompositeType type
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class Calculator2Input
    {
        [DataMember]
        public int FirstNumber { get; set; }

        [DataMember]
        public int SecondNumber { get; set; }
    }

    [DataContract]
    public class Calculator2Output
    {
        [DataMember]
        public string AdditionResult { get; set; }
    }
}

Now open Calc.svc.cs file, delete all the default code generated and replace its with new code as shown below. Here provide the implementation for both the methods.

Calc.svc.cs Code
using System;
usingSystem.Collections.Generic;
using System.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Calc : ICalc
    {
        public string Calculator1(int FirstNumber, int SecondNumber)
        {
            return "Addition is : "+ Convert.ToString(FirstNumber + SecondNumber);
        }

        public Calculator2OutputCalculator2(Calculator2Input Calc2Input)
        {
            Calculator2Output obj = new Calculator2Output();

            obj.AdditionResult = "Addition is : " + (Calc2Input.FirstNumber + Calc2Input.SecondNumber).ToString();

            return obj;
        }
    }
}

Configuring service with wsHttpBinding and webHttpBinding

Now open web.config file of wcf application. Replace code with code provided below. Here ws address is for wsHttpBinding and web is for webHttpBinding.

xml version="1.0"?>
configuration>

  appSettings>
    add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  appSettings>
  system.web>
    compilation debug="true" targetFramework="4.5" />
    httpRuntime targetFramework="4.5"/>
  system.web>
 
 
 
 
 
  system.serviceModel>
    services>
      service behaviorConfiguration="Default" name="WcfService1.Calc">
        endpoint address="web" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WcfService1.ICalc" />
        endpoint address


This post first appeared on ASPArticles, please read the originial post: here

Share the post

Creating WCF Service with wsHttpBinding and webHttpBinding together

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×