Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WcfClient<TServer> where TServer : class
- {
- private Binding _binding;
- private EndpointAddress _endpoint;
- public WcfClient(Binding binding, EndpointAddress endpoint)
- {
- this._binding = binding;
- this._endpoint = endpoint;
- }
- public void Call(Action<TServer> function)
- {
- TServer client = null;
- try
- {
- client = new ChannelFactory<TServer>(this._binding, this._endpoint).CreateChannel();
- function(client);
- }
- catch
- {
- throw;
- }
- finally
- {
- if (client != null)
- {
- IClientChannel channel = (IClientChannel)client;
- if (channel.State == CommunicationState.Faulted)
- {
- channel.Abort();
- }
- else
- {
- channel.Close();
- }
- }
- }
- }
- public T Call<T>(Func<TServer, T> function)
- {
- TServer client = null;
- try
- {
- client = new ChannelFactory<TServer>(this._binding, this._endpoint).CreateChannel();
- return (function(client));
- }
- catch
- {
- throw;
- }
- finally
- {
- if (client != null)
- {
- IClientChannel channel = (IClientChannel)client;
- if (channel.State == CommunicationState.Faulted)
- {
- channel.Abort();
- }
- else
- {
- channel.Close();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment