Loading

创建WCF链接

 

不要在using语句中调用WCF服务

 

这样如何?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public sealed class WcfWrapper<T> : IDisposable
    {
        private readonly dynamic _client;
 
        public WcfWrapper(T wcf)
        {
            WcfClient = wcf;
            _client = wcf;
        }
 
        public T WcfClient { get; private set; }
 
        #region IDisposable Members
 
        public void Dispose()
        {
            try
            {
                _client.Close();
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception.Message);
                _client.Abort();
            }
             
        }
 
        #endregion
    }



如此使用

1
2
3
4
using (var client = new WcfWrapper<ServiceClient>(_service))
                            {
                                var callresult = client.WcfClient.Service(message);
}
posted @ 2018-06-06 16:05  Sam Xiao  阅读(44)  评论(0)    收藏  举报