WCF - 故障/异常与消息 [英] WCF - Faults / Exceptions versus Messages

查看:121
本文介绍了WCF - 故障/异常与消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在争论是否通过WCF频道发送故障,而不是传递一条消息,指出服务的状态或响应。



故障来自WCF的内置支持,您可以使用内置的错误处理程序并作出相应的反应。但是,这样会带来开销,因为在.NET中抛出异常可能相当昂贵。



消息可以包含必要的信息,以确定您的服务调用发生了什么,而没有开销抛出异常然而,它需要几行重复代码来分析消息并根据其内容确定操作。



我们在创建一个可以在我们的服务,这是我们想出来的:

  public class ReturnItemDTO< T> 
{
[DataMember]
public bool Success {get;组; }

[DataMember]
public string ErrorMessage {get;组; }

[DataMember]
public T Item {get;组; }
}

如果我所有的服务调用返回这个项目,我可以一直检查成功财产,以确定一切顺利。那么我在事件中出现错误消息字符串,如果需要,则包含Dto的通用项目。



异常信息必须被记录到一个中央记录服务,而不是退出服务。



想法?注释?想法?建议?



有关我的问题的进一步说明



有错误的合同是沟通业务规则。



喜欢,如果有人登录,他们的帐户被锁定,我该如何沟通?他们的登录显然失败了,但是由于帐户锁定的原因,它失败了。



我也是这样:





B)返回AuthenticatedDTO与相关信息

解决方案


然而,这带来了开销,因为在.NET中抛出异常可能相当昂贵。


您将序列化和反序列化对象到XML并通过缓慢的网络发送它们。相比之下,抛出异常的开销是可忽略的。



<我通常坚持抛出异常,因为他们清楚地传达了一些错误的东西,而且所有的 webservice工具包都有一个很好的处理方式。



在您的示例中,我将抛出一个UnauthorizedAccessException消息Account Locked。



澄清: .NET wcf服务将异常转换为FaultCont默认情况下,您可以更改此行为。 MSDN:指定和处理合同和服务中的错误


We're currently having a debate whether it's better to throw faults over a WCF channel, versus passing a message indicating the status or the response from a service.

Faults come with built-in support from WCF where by you can use the built-in error handlers and react accordingly. This, however, carries overhead as throwing exceptions in .NET can be quite costly.

Messages can contain the necessary information to determine what happened with your service call without the overhead of throwing an exception. It does however need several lines of repetitive code to analyze the message and determine actions following its contents.

We took a stab at creating a generic message object we could utilize in our services, and this is what we came up with:

public class ReturnItemDTO<T>
{
    [DataMember]
    public bool Success { get; set; }

    [DataMember]
    public string ErrorMessage { get; set; }

    [DataMember]
    public T Item { get; set; }
}

If all my service calls return this item, I can consistently check the "Success" property to determine if all went well. I then have an error message string in the event indicating something went wrong, and a generic item containing a Dto if needed.

The exception information will have to be logged away to a central logging service and not passed back from the service.

Thoughts? Comments? Ideas? Suggestions?

Some further clarification on my question

An issue I'm having with fault contracts is communicating business rules.

Like, if someone logs in, and their account is locked, how do I communicate that? Their login obviously fails, but it fails due to the reason "Account Locked".

So do I:

A) use a boolean, throw Fault with message account locked

B) return AuthenticatedDTO with relevant information

解决方案

This however carries overhead as throwing exceptions in .NET can be quite costly.

You're serializing and de-serializing objects to XML and sending them over a slow network.. the overhead from throwing an exception is negligable compared to that.

I usually stick to throwing exceptions, since they clearly communicate something went wrong and all webservice toolkits have a good way of handling them.

In your sample I would throw an UnauthorizedAccessException with the message "Account Locked".

Clarification: The .NET wcf services translate exceptions to FaultContracts by default, but you can change this behaviour. MSDN:Specifying and Handling Faults in Contracts and Services

这篇关于WCF - 故障/异常与消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆