Tuesday, February 10, 2015

Using Channel Factory

Wrapping CreateChannel for WCF calls:


Code:
//view model
public void GetEmployees()
{
 var emp = ServiceFactory.GetService<IEmployeeService>();
}

//IServiceFactory
public interface IServiceFactory
{
 T GetService<T>();
}

//ServiceFactory class
public static class ServiceFactory: IServiceFactory
{
 public T GetService<T>()
 {
  T svc = default(T);
        ChannelFactory<T> factory = new ChannelFactory<T>(typeof(T).FullName);

        //any other logic or conditions you want
        //...

        svc = factory.CreateChannel();

        return svc; 
 }
}

No comments :