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

Solutions to Common abnormal problems in WCF

2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "solutions to common abnormal problems in WCF". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the solutions to common abnormal problems in WCF.

WCF is still quite commonly used, so I studied WCF and took it out here to share with you. I hope it will be useful to you. 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, WCF treats unrecognized exceptions 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;}

However, the WCF 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 and terminate the service.

[ServiceContract (SessionModeSessionMode = SessionMode.Allowed)] public interface IDocumentsExplorerService {[OperationContract] [FaultContract (typeof (DirectoryNotFoundException))] DocumentList FetchDocuments (string homeDir);} Thank you for reading. The above is the content of "Solutions to Common abnormal problems in WCF". After the study of this article, I believe you have a deeper understanding of the solutions to common abnormal problems in WCF, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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