FaultException和Details(FaultException and Details)

编程入门 行业动态 更新时间:2024-10-27 17:16:29
FaultException和Details(FaultException and Details)

我正在从几天的WCF服务工作。 我写了一个带有异常的服务

public String Login(string Vendorname, string VendorAccessCode) { try { if() { } else { UserIdentityToken = string.Empty; this.ErrorMessage = "Authentication failed. Please contact administrator"; throw new FaultException(this.ErrorMessage); } } catch (FaultException ex) { logger.Error(ex.Message); } catch (Exception ex) { logger.Error(ex.Message); } return UserIdentityToken; }

在此之后,我在Messgae Inspector中处理客户端的异常

public class MessageInspector : IClientMessageInspector { public void AfterReceiveReply(ref Message reply, object correlationState) { if (reply.IsFault) { MessageFault fault = MessageFault.CreateFault(new FaultCode("Receiver"), new FaultReason(reply.ToString())); throw new FaultException(fault); } } }

我正在处理我的客户端代码

try { objVendorServiceClient.Login(txtuserName.Text.Trim(), txtAccessCode.Text.Trim()); } catch (FaultException ex) { lblAuthenticationMessage.Text = ex.Message; throw ex; }

但是,如果Service中的身份验证失败,则Reply.IsFault仅返回false。 任何人都可以向我解释什么是回复。这个错误以及它对我有用的程度如何?

I am working on WCF Services from Couple of Days. I had a written a service with an Exception as

public String Login(string Vendorname, string VendorAccessCode) { try { if() { } else { UserIdentityToken = string.Empty; this.ErrorMessage = "Authentication failed. Please contact administrator"; throw new FaultException(this.ErrorMessage); } } catch (FaultException ex) { logger.Error(ex.Message); } catch (Exception ex) { logger.Error(ex.Message); } return UserIdentityToken; }

After this i am handling the exceptions in client side in a Messgae Inspector as

public class MessageInspector : IClientMessageInspector { public void AfterReceiveReply(ref Message reply, object correlationState) { if (reply.IsFault) { MessageFault fault = MessageFault.CreateFault(new FaultCode("Receiver"), new FaultReason(reply.ToString())); throw new FaultException(fault); } } }

I am handling my Client Side Code as

try { objVendorServiceClient.Login(txtuserName.Text.Trim(), txtAccessCode.Text.Trim()); } catch (FaultException ex) { lblAuthenticationMessage.Text = ex.Message; throw ex; }

But when ever the authentication fails in Service, the Reply.IsFault is returning false only. could any one explain me what is reply.Isfault and how exactly it is useful to me?

最满意答案

Message.IsFault属性

获取一个值,该值指示此消息是否生成任何SOAP错误。

你说:

当Service中的身份验证失败时,Reply.IsFault返回false

那是因为你自己捕获了错误,记录它,然后返回一个空字符串。 您必须抛出FaultException,以便WCF捕获它并从中创建SOAP错误。

Message.IsFault Property:

Gets a value that indicates whether this message generates any SOAP faults.

You say:

when ever the authentication fails in Service, the Reply.IsFault is returning false

That is because you catch the fault yourself, log it, and then return an empty string. You'll have to throw the FaultException in order for WCF to catch it and create a SOAP fault from it.

更多推荐

本文发布于:2023-07-20 12:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:FaultException   Details

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!