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

What is the WCF exception handling solution?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about what the WCF exception handling solution is. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Exception handling is indispensable in our program, exceptions can feedback our information, if you do not know the WCF exception friends, please see below for you to introduce. Exception messages are related to specific technologies, as well as .NET exceptions, so WCF does not support traditional exception handling. If the exception is handled in the traditional way in the WCF service, the client cannot receive the WCF exception thrown by the service because the exception message cannot be serialized, such as this service design:

[ServiceContract (SessionModeSessionMode = SessionMode.Allowed)] public interface IDocumentsExplorerService {[OperationContract] DocumentList FetchDocuments (string homeDir);} [ServiceBehavior (InstanceContextModeInstanceContextMode=InstanceContextMode.Single)] public class DocumentsExplorerService: IDocumentsExplorerService,IDisposable {public DocumentList FetchDocuments (string homeDir) {/ / Some Codes if (Directory.Exists (homeDir)) {/ / Fetch documents according to homedir} else {throw new DirectoryNotFoundException ("Directory {0} is not found.", homeDir)) }} public void Dispose () {Console.WriteLine ("The service had been disposed.");}}

When the client invokes the above service operation, the WCF exception cannot be obtained by using the following capture method:

Catch (DirectoryNotFoundException ex) {/ / handle the exception;}

To make up for this, unrecognized WCF exceptions are treated as FaultException exception objects, so the client can catch FaultException or Exception exceptions:

Catch (FaultException ex) {/ / handle the exception;} catch (Exception ex) {/ / handle the exception;}

Comprehensive description of the reliable transmission performance of WCF

A case study of orderly transfer of WCF by programming

Disclosure of WCF Service metadata Exchange programming

One-to-one connection of three WCF service modes

Talking about WCF transaction attributes

However, the exception caught in this way does not recognize the error message passed by DirectoryNotFoundException. What is particularly serious is that such exception handling will also lead to errors in the channel in which messages are delivered. When the client continues to invoke the service operation of the service proxy object, it will get a CommunicationObjectFaultedException exception and cannot continue to use the service. If the service is set to PerSession mode or Single mode, the exception will also cause the service object to be released to terminate the service. WCF does not recommend creating multiple ServiceHost instances in the application domain. If you want to host multiple services, you can expose multiple WCF services through multiple Endpoint in a single host. Because the application domain isolates security, it is necessary to create multiple ServiceHost instances if you need to provide different security contexts.

The above is what the editor shares with you about the WCF exception handling solution. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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