Skip to content

Instantly share code, notes, and snippets.

@brandedoutcast
Last active January 10, 2019 10:45
Show Gist options
  • Save brandedoutcast/a359e2c655f0bcf17e8546e5a9c5c86d to your computer and use it in GitHub Desktop.
Save brandedoutcast/a359e2c655f0bcf17e8546e5a9c5c86d to your computer and use it in GitHub Desktop.
Generic Service Util Client Proxy to consume WCF services
using System;
using System.ServiceModel;
public class Proxy<T>
{
public ChannelFactory<T> Factory { get; set; }
public Proxy()
{
Factory = new ChannelFactory<T>("endpoint");
}
public T CreateChannel()
{
return Factory.CreateChannel();
}
public void Execute(Action<T> action)
{
T proxy = CreateChannel();
action(proxy);
((ICommunicationObject)proxy).Close();
}
public TResult Execute<TResult>(Func<T, TResult> function)
{
T proxy = CreateChannel();
var result = function(proxy);
((ICommunicationObject)proxy).Close();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment