Access The Service Using Different Contract Or Multiple Service Contracts

In this post I am going to explain how to access service using different contract. Before entering into this concept, we should know the following two things:

What is contract

The contract is nothing, it provide the information about the services and data available in the WCF service to the outside world or the client.

What is service contract and it usage

The service contract is used to expose the set of operation or method available in the service endpoint. If we want, say simply it provides the set of callable method available on the service endpoint.

Again I raise one question. Is it possible to implement the multiple endpoints to the same service? What is your answer? If you say yes, I really appreciate you, otherwise I will feel good for getting an opportunity to say to you.

Yes, it is possible to implement the multiple service contact to the same service. For explaining it to you, I will take one sample to show the customer details. In this service the customer only can see their information. They do not access others information, but the organization can see any customer information. So we will see a sample. Firstly, create one service library to implement the service. If you want to know about how to create service library please refer my article:

Once you created the service library add WCF service and add two service contracts in the interface like the following:

  1. [ServiceContract]  
  2. public interface ICustomerService  
  3. {  
  4.     [OperationContract]  
  5.     string GetInformation(string name);  
  6. }  
  7. [ServiceContract]  
  8. public interface ICompanyService: ICustomerService  
  9. {  
  10.     [OperationContract]  
  11.     string GetCutomerInformation(string name);  
  12. }  
The first service contract is going to be used by the customer and the second service contract is going to be used by organization. Here I inherit the first service contact into the second service because the organization wants to get access both information.

Implement the second service (ICompanyService) in the class. Because it inherit the first service contract. So it has the operations or methods of the both contract. For that purpose, I implement this in the class “CompanyService” and implement the class mentioned as in the following code:
  1. public class CompanyService: ICompanyService  
  2. {  
  3.     public string GetCutomerInformation(string name)  
  4.     {  
  5.         return "It is information regarding the customer " + name;  
  6.     }  
  7.     public string GetInformation(string name)  
  8.     {  
  9.         return "Dear " + name + ",\n It is your own information";  
  10.     }  
  11. }  
That’s all! Our service implementation is complete now. So we will add another project to configure and host the service.

First, create one console project named “Server” and add application configuration file in it. Then add the following configuration in it.
  1. <system.serviceModel>  
  2.     <services>  
  3.         <service name="ServiceWithMultipleContract.CompanyService" behaviorConfiguration="mexBehaviour">  
  4.             <endpoint address="ICustomerService" binding="basicHttpBinding" contract="ServiceWithMultipleContract.ICustomerService"></endpoint>  
  5.             <endpoint address="ICompanyService" binding="netTcpBinding" contract="ServiceWithMultipleContract.ICompanyService"></endpoint>  
  6.             <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>  
  7.             <host>  
  8.                 <baseAddresses>  
  9.                     <add baseAddress="http://localhost:8080" />  
  10.                     <add baseAddress="net.tcp://localhost:8081" /> </baseAddresses>  
  11.             </host>  
  12.         </service>  
  13.     </services>  
  14.     <behaviors>  
  15.         <serviceBehaviors>  
  16.             <behavior name="mexBehaviour">  
  17.                 <serviceMetadata httpGetEnabled="true" /> </behavior>  
  18.         </serviceBehaviors>  
  19.     </behaviors>  
  20. </system.serviceModel>  
After placing the configuration, we need to host the application in the self host. For that, put the following code.
  1. ServiceHost host = new ServiceHost(typeof(ServiceWithMultipleContract.CompanyService));  
  2. Console.WriteLine("Try to start service");  
  3. host.Open();  
  4. Console.WriteLine("Serive started.."); ;  
Build the server application and run it in the administrator. If it started the console window will show like the following:

build the server application

Now our service is ready. So we can capture the service in the client application. We are going to add one console application named “Client” and add service reference to the service in it.

Client

Note: The above picture show two service contract. If the customer want to access the service then we will access through ICustomerService and the organization will use the ICompanyService to access the service.

So by using this, we can restrict the service access level. So the customer use the following code to access the service.

CustomerServiceClient customerClient = new CustomerServiceClient();
Console.WriteLine(customerClient.GetInformation("sakthikumar"));

Then run the application. You will get the following output,

output

Add the following code to get the service in the organization.
  1. CompanyServiceClient companyClient = new CompanyServiceClient();  
  2. Console.WriteLine("Message received from Customer Servive Contract.");  
  3. Console.WriteLine(companyClient.GetInformation("sakthikumar"));  
  4. Console.WriteLine(companyClient.GetCutomerInformation("sakthikumar"));  
Then run the application and the output will be like this,

run the application

Note the above two set of code. They use different service client to access service information. It will be helpful to you for developing access privilege based service.

That’s all. Please feel free to mention your comments if you have any question or doubt.