Usage of the WCF ServiceProxyHelper

I received a question through the contact form about the usage of my WCF ServiceProxyHelper. That blog post shows the code for the LogicaCMG.ServiceAccess.ServiceProxyHelper<TProxy, TChannel> class. This class helps you in creating a disposable wrapper around a generated Windows Communication Foundation proxy class for a service.

Suppose you have a standard generated WCF proxy class with the name TeamServiceClient which implements the service contract ITeamService. Both are generated by the svcutil.exe utility from the Windows SDK or by using "Add Service Reference" in Visual Studio. For clarity I have left out most namespaces and attributes because they are not important for this discussion:

using System.ServiceModel;
public interface ITeamService { GetTeamWithMembersResponse GetTeamWithMembers(GetTeamWithMembersRequest request); } public partial class TeamServiceClient: ClientBase<ITeamService>, ITeamService {
... GetTeamWithMembersResponse GetTeamWithMembers(GetTeamWithMembersRequest request); }

You can use my ServiceProxyHelper class by deriving from it:

public sealed TeamServiceHelper: LogicaCMG.ServiceAccess.ServiceProxyHelper<TeamServiceClient, ITeamService>
    public TeamServiceHelper()

    public TeamWithMembers GetTeamWithMembers(Guid teamGuid)
       GetTeamWithMembersRequest = new GetTeamWithMembersRequest(); 
       request.TeamGuid = teamGuid;
       GetTeamWithMembersResponse response = Proxy.GetTeamWithMembers(request); 
       return response.Team;

As you can see I have added code for the construction and unwrapping of the DataContract classes GetTeamWithMembersRequest and GetTeamWithMembersResponse.

And in your code you can call the service through this helper by using code like this:

   Guid teamGuid = new Guid(...);
   TeamWithMembers team;  
   using (TeamServiceHelper helper = new TeamServiceHelper()) 
       team = helper.GetTeamWithMembers(teamGuid);

This will give you guaranteed clean up of any WCF channels and such that are used behind the covers in the face of exceptions. Only expected exceptions during the clean up and closing of the channel will be ignored. Exceptions that occur when calling the service operation are not swallowed and will still be passed on to your code.

I hope this helps explain how you can use my WCF ServiceProxyHelper.

Leave a Reply

Your email address will not be published. Required fields are marked *