X
27Nov

How to implement multiple service contract in a single WCF service.

First lets us create one service by using “class library project”.

STEP1: Create class library project

image1

Step2: Delete class1.cs file which is auto generated.

Step3: Add wcf service for this project. Right click on right click on class library project->Add->new Item(click)->select wcf service from installed templates and rename is as ” Multipleservicecontrats.cs”

Click add button

image-2

image3

This generates two files Multipleservicecontrats.cs and IMultipleservicecontrats.cs(solution Explorer)

Step 4: Change Multipleservicecontrats.cs define as below

[Service Contract]
public interface IproductInformation
{
[operationcontract]
String GetInformation();
}


[Service Contract]

public interface Iproductprice
{
[operationcontract]
String GetPrice();
}


Now IMultipleservicecontrats.cs  file looks like this

image4

Now we got two service contract:

  1. IProductInformation is available to everyone i.e. outside the firewall and client with in firewall.
  2. Iproductprice is only available to clients who are behind the firewall.

Now we will expose IProductInformation service using HTTP protocol so http can go through the fire wall so this will be accessible to everyone and Iproductprice service using Net TCP protocol.

Step 5: Now look at Multipleservicecontrats.cs class we will implement both service contract interfaces and change the two methods as below.

Multipleservicecontrats.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace multiplecontract

{

    public class Multipleservicecontrats : IProductInformation, Iproductprice

    {

            public string GetInformation()

        {

            return "our porduct information available to everyone";

        }

        public string GetPrice()

        {

            return "product price  available to client who behind the firewall";

        }

    }

}

image5

Now we have to expose these two service contracts by creating two end point.

End point? (Every service must have Address that defines where the service resides, Contract that defines what the service does and a Binding that defines how to communicate with the service. In WCF the relationship between Address, Contract and Binding is called Endpoint)

One end point going to make use of IProductInformation service available and other end point going to make use of Iproductprice service available.

Now our service is ready now we have to host the service in order to do that we have to create one console application.

Now we will create one console application in the same solution and name it as productservicehost.

image6

Once console application is created. Now add a service reference system.servicemodel

To console application.

In console application references right click-> add reference-> (add reference window) select .NeT tab select System.ServiceModel and click ok

image7

Then, add an Application configuration file (App.config). In this App.config file we are going to add configuration to our service.

Update the App.config file as shown below.

In our service we have two service contracts now we will expose each service contract by creating endpoint. With different bindings basicHttpBinding and netTcpBinding.

 

 

  
    
      
        
        
        
        
          
            
            
          
        
      
    
    
      
        
          
        
      
    
  

Now add class library project (multiplecontract) to console application (productservicehost)

image8

Now change the code inside Program.cs file as below

 

Program.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace productservicehost

{

    class Program

    {

        static void Main(string[] args)

        {

            using (ServiceHost host = new ServiceHost(typeof(multiplecontract.Multipleservicecontrats)))

            {

                host.Open();

                Console.WriteLine("Host started@" + DateTime.Now.ToString());

                Console.ReadLine();

            }

        }

    }

}




Run the application

Now we will create asp.net empty web application to consume WCF service as

Clientapplication.

image9

Add new asp.net web form to Clientapplication lets its name as Home.aspx

Now add service reference to Clientapplication.

Once you give service address and click Go button it will show which are  the service contracts  exposed.

If you click on IproductInformation  Getinformation operation contract is exposed.

In namespace name it as multipleservicecontract and click ok.

image10

Once we add service reference it will generate a proxy class

If we see web.config file automatically it will generate two  endpoints because services is exposed with two diffenet end points.


 


    
        
    
    
        
            
                
            
            
                
            
        
        
            
            
                
                    
                
            
        
    


Now change Home.aspx page as shown below


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="clientapplication.Home" %>



    


    
    
           
   
   

Above html code I have two button controls  one is Get Info and other one is Get price and tow label controls to display the text received form service.

Home.aspx.cs


using System;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace clientapplication

{

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

        protected void btninfo_Click(object sender, EventArgs e)

        {

            multipleservicecontract.ProductInformationClient client = new multipleservicecontract.ProductInformationClient();

            lblinfo.Text = client.GetInformation();

        }

        protected void btnprice_Click(object sender, EventArgs e)

        {

            multipleservicecontract.IproductpriceClient priceclient = new multipleservicecontract.IproductpriceClient();

           lblprice.Text= priceclient.GetPrice();

        }

    }

}




In this case I am able to create client for both the contracts because my service and client are  running on same machine. If my service is running behind the firewall we can create a proxy class but it will block the service methods i.e. Iproductprice service contract methods.

image11

 

 

Related

Dependency Injection on SignalR

                &nbs...

Read More >

WCF SOAP and REST Services

WCF (Windows Communication Foundation) is a programming platform and runtime system for building, co...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *