WCF _ example
作者:互联网
private static ChannelFactory<InterDataServiceChannel> _factory; private static object _lock = new object(); private InterDataServiceClient _client = null; ILogBase _logBase; public ClientHelper(ILogBase logBase) { _logBase = logBase; } public virtual void Dispose() { CloseChannel(); CloseFactory(); } protected void CloseChannel() { lock (_lock) { if (_client != null) { try { _client.Close(); } catch (TimeoutException tmEx/* timeout */) { _logBase.Error($"CloseChannel TimeoutException :{tmEx.Message}", tmEx); // Handle the timeout exception _client.Abort(); } catch (CommunicationException ccEx /* communicationException */) { _logBase.Error($"CloseChannel CommunicationException :{ccEx.Message}", ccEx); _client.Abort(); } catch (Exception e) { _logBase.Error($"CloseChannel Exception :{e.Message}", e); _client.Abort(); throw ; } _client = null; } } } protected void CloseFactory() { lock (_lock) { if (_factory!=null) { try { _factory.Close(); } catch (TimeoutException tmEx/* timeout */) { _logBase.Error($"CloseFactory TimeoutException :{tmEx.Message}", tmEx); // Handle the timeout exception _factory.Abort(); } catch (CommunicationException ccEx/* communicationException */) { _logBase.Error($"CloseFactory TimeoutException :{ccEx.Message}", ccEx); // Handle the communication exception _factory.Abort(); } catch(Exception e) { _logBase.Error($"CloseFactory Exception :{e.Message}", e); throw; } } _factory = null; } } private void CreateFactory() { // ... set up custom bindings here and get some config values var endpoint = GetEndpointAddr(); var binding = GetBindingForEndpoint(); _factory = new ChannelFactory<InterDataServiceChannel>(binding, endpoint); } protected ChannelFactory<InterDataServiceChannel> Factory { get { if (_factory == null ) { // ... set up custom bindings here and get some config values CreateFactory(); } else if(_factory.State != CommunicationState.Opening) { CloseFactory(); CreateFactory(); } return _factory; } } private void CreateClient() { _client = (InterDataServiceClient)Factory.CreateChannel(); SetClientCredential(_client.ClientCredentials.Windows.ClientCredential); } protected InterDataServiceClient Client { get { if (_client == null) { CreateClient(); } else { if (_client.State != CommunicationState.Opening) { CloseChannel(); } CreateClient(); } return _client; } } public InterDataServiceClient GetDefaultOAClient() { return Client; }
标签:logBase,Error,factory,ccEx,client,WCF,null,example 来源: https://www.cnblogs.com/panpanwelcome/p/15813458.html