title: How to re-write WCF client to use ChannelFactory instead of service reference
body: <p>I have a WCF service created using the <a href="http://www.codeplex.com/servicefactory" rel="nofollow">web service software factory</a>. All my incoming requests and returned responses are implemented as message contracts. I had been accessing the service by adding a service reference in VS which worked fine. However I've now been told that this isn't the standard company way and that I need to call the service using an in-house WCF proxy helper class based on the System.ServiceModel.ChannelFactory. </p>
  
  <p>I've never used the ChannelFactory before and I'm struggling to figure out the best way to re-write the client access. The service contract is generated by the service factory and I need to provide the service interface to the proxy helper class. Here's part of the service contract - there's about 25 operations in total on the service:</p>
  
  <pre><code>namespace CAEWCFService.ServiceContracts
  {
    /// &lt;summary&gt;
    /// Service Contract Class - CAEServiceContract
    /// &lt;/summary&gt;
    [WCF::ServiceContract(Namespace = "urn:CAEServiceServiceContracts", Name = "CAEServiceContract", SessionMode = WCF::SessionMode.Allowed, ProtectionLevel = ProtectionLevel.None )]
    public partial interface ICAEServiceContract 
    {
      [WCF::OperationContract(IsTerminating = false, IsInitiating = true, IsOneWay = false, AsyncPattern = false, Action = "GetAllIncomes", ProtectionLevel = ProtectionLevel.None)]
      CAEService.MessageContracts.GetAllIncomesReponse GetIncomeRecords(CAEService.MessageContracts.GetAllIncomesRequest request);
  
      [WCF::OperationContract(IsTerminating = false, IsInitiating = true, IsOneWay = false, AsyncPattern = false, Action = "urn:CAEServiceServiceContracts.CAEServiceContract.GetSecurityRecords", ProtectionLevel = ProtectionLevel.None)]
      CAEService.MessageContracts.GetAllSecuritiesResponse GetSecurityRecords(CAEService.MessageContracts.GetAllSecuritiesRequest request);
  
      //... the rest
    }
  }
  </code></pre>
  
  <p>And here's the call from the client side calling the GetIncomeRecords operation:</p>
  
  <pre><code>CAEServiceContractClient client = new CAEServiceContractClient();    
  
  IncomeSearchItem item = new IncomeSearchItem()
  {
      aCode = aCode.Text,
      bCode = bCode.Text,
      sCode = sCode.Text,
      secDesc = tbSecurityName.Text,
      status = statusValue != 0 ? statusValue : 0,
      fromDate = fromDate,
      toDate = toDate,
      IncomeID = 0,
      userBank = userBank,
      userId = userId,
      currentRate = currentSourceVal != 0 ? currentSourceVal : 0,
      dateConfirmed = ddDate.SelectedValue,
      pageIndex = pageIdx,
      pageSize = recordsPerPage
  };
  
  GetAllIncomesReponse response = new GetAllIncomesReponse();
  
  try
  {
      response = client.GetIncomeRecords(item);
      totalRecords = response.TotalRecords;
  
      incomelist.DataSource = response.IncomeItemColl;
      incomelist.DataBind();
  
      //.. the rest
  }
  </code></pre>
  
  <p>Here's some sample code from another project showing how to use the proxy helper. The problem with my service is that it expects and returns message contracts whereas the sample one passes in primitive types and returns primitives or datasets:</p>
  
  <pre><code>public static class ServiceHelper
  {
    /// &lt;summary&gt;
    /// Services the verification.
    /// &lt;/summary&gt;
    /// &lt;param name="user"&gt;The user.&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string ServiceVerification(string user)
    {
      return WCFProxyHelper&lt;IFCAServices&gt;.Use(client =&gt;
        client.ServiceVerification(user));
    } 
  }
  </code></pre>
  
  <p>So what's the quickest / easiest way to do this? How do I make the ChannelFactory aware of my IncomeSearchItem and several other objects that I need to pass to the service? Can / should I use svcutil to generate all the objects and use that in the ChannelFactory? </p>