ProxyFactory

264082

ProxyFactory greatly simplifies the usage of WCF proxies. Before ProxyFactory a programmer had to manage the lifetime of the WCF proxy. With ProxyFactory you just program against a contract, no worries about error handling.

ProxyFactory actually generates a thin proxy "dynamically" that lives between the caller and the real WCF proxy. The thin proxy will actually send all calls through the ProxyFactory CallMethod member. This thin proxy is generated using Reflection.Emit.

With the help of ProxyFactory, in process calls and and external WCF calls both look the same. You can easily move a service from in process to external without much effort, just a configuration change.

ProxyFactory factory = new ProxyFactory();

// In Process Example
var proxy = factory.Proxy<IInProcess>();
proxy.TestMe("hi");

// External Example (notice that it is the same!!!
var proxy = factory.Proxy<IExternal>();
proxy.TestMe("hi");


In Process Services

With in process services the WCF service is being hosted within the process of the caller. In process WCF services use named pipes for communication. The easiest way to call an in process service is to apply the InProc attribute to the contract.

    [ServiceContract]
    [InProc(typeof(MyService))]
    public interface IMyService
    {
        [OperationContract]
        string TestMe(string input);
    }


After applying the attribute you can easily call the WCF by just creating an instance of the "generated" thin proxy using ProxyFactory.

ProxyFactory factory = new ProxyFactory();
IMyService proxy = factory.Proxy<IMyService>();
proxy.TestMe("hi");


In Proc calls can also be configured to using an configuration section. The configuration section below configures the IConfigContract to be implemented using the BasicTests.ConfigContract service.

  <ServiceHelpers>
    <inproc>
      <endpoint
        contract="BasicTests.IConfigContract"
        implementation="BasicTests.ConfigContract, BasicTests" />              
    </inproc>
  </ServiceHelpers>


External Hosted

Externally hosted WCF services can be easily called using the same proxy factory code. The example below creates a WCF service endpoint hosting using TCP. The endpoint can then be reached using the same thin proxy generated by ProxyFactory.

string uri = "http://localhost/servicehelpers";
using (var host = new ServiceHost(typeof(ExternalService), new Uri(uri)))
{
    host.AddServiceEndpoint(typeof(IExternalService), new WSHttpBinding(), uri);
    host.Open();

    var factory = new ProxyFactory();

    // Generate a thin proxy.
    var proxy = factory.Proxy<IExternalService>();

    // Call the TestMe method. This will proxy the messsage thru the ProxyFactory.CallMethod.
    Assert.AreEqual("hi", proxy.TestMe("hi"));
}


The configuration for the proxy can be easily handled with the custom DPL configuration section.

  <ServiceHelpers>
    <external>
      <endpoint
        contract="BasicTests.IExternalService"
        address="http://localhost/servicehelpers" />     
     </external>
  </ServiceHelpers>


Last edited Aug 4, 2011 at 2:48 AM by chadmichel, version 6

Comments

No comments yet.