Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not call the WCF service in the using statement

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces why do not call the WCF service in the using statement, the article is very detailed, has a certain reference value, interested friends must read it!

If you call a WCF service in a using statement like the following code, you need to be aware of one problem.

Using (CnblogsWcfClient client = new CnblogsWcfClient ()) {client.Say ("Hello, cnblogs.com!");

The above code looks fine. CnblogsWcfClient is an automatically generated WCF client agent that inherits from System.ServiceModel.ClientBase. At the end of the using statement, the System.IDisposable.Dispose interface of the ClientBase implementation is called, which is actually the Close () method of ClientBase. Open C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ System.ServiceModel.dll with .NET Refector, and you can see this code, as shown below:

Not only does it look fine, it just seems to be fine. But. The problem lies in ClientBase.Close (), where Close () closes a network connection. If there is a problem with the network connection, an exception will be thrown if the network connection cannot be closed properly (this is how ClientBase's Close method is designed to throw an exception instead of forcing it to close). Originally, the purpose of using using is that no matter what happens, even if the sky falls, it will be closed for me; as a result, it is closed, but it is not closed, and the sky still falls.

Maybe we can use "force majeure" to avoid this problem, but it is the nature of programmers to solve problems. Any small problem in the code can not be ignored, because it is difficult to predict whether this small problem will cause a big problem.

So how to solve this problem? there is an answer in MSDN (go to MSDN). The code is as follows:

CnblogsWcfClient client = new CnblogsWcfClient (); try {client.Say ("Hello, cnblogs.com!"); client.Close ();} catch (CommunicationException e) {... Client.Abort ();} catch (TimeoutException e) {... Client.Abort ();} catch (Exception e) {... Client.Abort (); throw;}

The above code looks a little shivering, and if you don't care what the exception is and turn it off whenever an exception occurs, you can use the following code (code from TIP: Closing your WCF Connections properly):

CnblogsWcfClient client = new CnblogsWcfClient (); client.Say ("Hello, cnblogs.com!"); try {if (client.State! = System.ServiceModel.CommunicationState.Faulted) {client.Close ();}} catch (Exception ex) {client.Abort ();}

Simplified version of the code:

CnblogsWcfClient client = new CnblogsWcfClient (); client.Say ("Hello, cnblogs.com!"); try {client.Close ();} catch {client.Abort ();} above is all the content of the article "Why not invoke the WCF service in the using statement". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report