Posts Tagged ‘Practical WCF’

Manually writing proxies

Posted: February 23, 2011 in WCF
Tags: , ,

Whoever comes from a soap service background always may have an assumption that for generating proxies you need to rely on svcutil.exe.If you think so,this assumption is wrong.You can create a proxy manually too.I am explaining here how you can do for a WCF service.  

The proxy class derives from the class ClientBase<T> & ClientBase<T> accepts a single generic type parameter which is the service contract this proxy encapsulates. Say you have a servicecontract IVendorServices,then ClientBase<T> gets replaced with ClientBase<IVendorServices>.

   [ServiceContract]
    public interface IVendorServices
    {

        [WebGet(UriTemplate = "v1/Ping")]
        [OperationContract]
        string Ping();
    }

public class VendorServices : IVendorServices
    {

        [Description("Service for Testing")]
        public string Ping()
        {
            return DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss:fffffffK");
        }

    }

public class VendorProxy : ClientBase<IVendorServices>, IVendorServices
    {
        public VendorProxy()
        {
        }
        #region IVendorServices Members

        public string Ping()
        {
            return Channel.Ping();
        }

        #endregion
    }

After you write the proxy class,if you place mouse over the interface name,it will create the members of class automatically.The Channel property of ClientBase<T> is of the type of  T parameter passed & if you write Channel.Ping(),it delegates the call to the method’s actual implementation & returns you the result.

 Hope you got some idea on how it works 🙂

Say,you have a WCF client that invokes a Windows Service.If you make changes to the Windows Service, before you call from client,you must first stop Windows Service and uninstall it. To stop the service, right-click the service in the SCM and select “Stop”.To uninstall the Windows Service type installutil /u bin\service.exe at the command prompt.

Note that if you stop the Windows Service and then run a client, an EndpointNotFoundException exception occurs when a client attempts to access the service.Make sure that your service is running before you make a call.

(more…)