Sunday, February 06, 2011

Accessing WCF service without creating Proxy

Each time when we want to consume a WCF service, we need to create proxy at client side. To create proxy, service must expose metadata endpoint.
Normally

  • We create a WCF service
  • Expose metadata endpoint
  • Add service reference at client side to create the proxy.
  • Using the proxy calls the service operation contracts.

Normally we call the service as

MyServiceContractClient proxy = new MyServiceContractClient();
var res = proxy.GetData(9);
console.WriteLine(res);
console.ReadKey(true);

Let us assume we want to call the service using channel without creating proxy or adding the service reference. We need to follow the below steps

Step 1

Put all the DataContract or ServiceContract in a separate DLL or class library. Add the reference of System.ServiceModel in class library. And create the service contract as below,

MyServiceContract.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace ContractDll
{
 [ServiceContract]
   public  interface MyServiceContract
   {
     [OperationContract]
     string GetData(int value);
   }
}

Read more: Beyond Relational