使用单个标准响应对象创建 WCF Web 服务

发布于 2024-12-06 05:57:48 字数 1489 浏览 0 评论 0原文

我被要求使用 WCF 实现一个 Web 服务,该服务将返回一个标准响应对象,该对象指示 Web 服务调用是成功还是失败,以及如果发生错误消息。

在我看来,这不是 WCF 的预期用途,对吗?

我的问题是,我正在使用 IDispatchMessageInspector.AfterReceiveRequest() 在 WCF 反序列化请求并调用我的操作之前根据 XSD 验证请求。

如果验证失败,我如何返回我的标准响应对象? WCF 似乎希望我在这种情况下抛出一个FaultException,但我希望该接口纯粹返回一个包含任何失败信息的标准响应对象。

因此,这意味着您需要定义一个自定义错误异常,当您收到的请求在反序列化等之前验证失败时可以抛出该异常,以及当请求成功时发送的第二个标准响应。

这是正确的做法吗?

为了实现在发生验证错误等情况时简单地返回一个响应对象的目标,我一直在尝试作弊并将 AfterReceiveRequest() 中的请求对象设置为 null,以便它跳过请求的处理。

 object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            try
            {
                validateMessage(ref request);
            }
            catch (Exception e)
            {
                request = null;
                return new FaultException<string>(e.Message);
            }
            return null;

        }

然后也许我可以在 BeforeSendReply() 中创建自己的自定义响应对象。

 void IDispatchMessageInspector.BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            if (correlationState != null)
            {
                reply = CreateMyOwnResponse(((FaultException) correlationState));
            }
        }

但我知道这看起来很愚蠢,并且与 WCF 最有可能的使用方式背道而驰。有没有办法很好地实现我想要的,或者当事情变坏时我必须发送异常类型,而当事情成功时我必须发送不同的“正常”响应?

I've been asked to implement a webservice using WCF that will return a single standard response object that indicates whether the webservice call succeeds or fails and an error message if one occurs.

It looks to me like this is not how WCF is intended to be used, is that correct?

My problem is that I am using IDispatchMessageInspector.AfterReceiveRequest() to validate the request against an XSD prior to WCF deserializing it and calling my operation.

If it fails validation how do I return my standard response object? WCF seems to want me to throw a FaultException in this scenario but I want the interface to purely return one standard response object that contains any failure information.

So this would mean that you need to define both a custom Fault Exception that you can throw when the request you receive fails validation prior to deserialization etc, and the second standard response to send when the request succeeds.

Is that the right approach?

To achieve my goal of simply returning the one single response object if something like a validation error occurs, I've been trying to cheat and set the request object to null in AfterReceiveRequest() so it skips processing of the request.

 object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            try
            {
                validateMessage(ref request);
            }
            catch (Exception e)
            {
                request = null;
                return new FaultException<string>(e.Message);
            }
            return null;

        }

and then perhaps I can create my own custom response object in BeforeSendReply().

 void IDispatchMessageInspector.BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            if (correlationState != null)
            {
                reply = CreateMyOwnResponse(((FaultException) correlationState));
            }
        }

But I know that this seems silly and is working against how WCF is meant to be used most likely. Is there a way to nicely achieve what I want or do I have to send an exception type when things go bad and a different "normal" response when things succeed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-12-13 05:57:48

WCF 不会限制返回对象或强制您抛出异常来传达错误情况。这取决于您的需求以及您在架构中总体规模上达成的协议。

在我的应用程序中,我总是倾向于使用 int 类型的返回参数,当发生错误时该参数为负值,而当操作成功完成时该参数为零。您还可以拥有更复杂的返回参数,其中包含错误结构,例如:

[DataContract]
public class ReturnValue
{
  [DataMember]
  public int ReturnCode;

  [DataMember]
  public string ResultingDescription;

  // enum listing severities like Critical, Warning, etc.
  [DataMember]
  public Severity Severity;
}

如果您需要复杂的返回参数作为操作的返回,则可以具有以下签名:

[OperationContract]
ReturnValue MyOperation (int param1, string param2, out MyOperationResult result);

我的经验法则是:

  1. 仅在服务上抛出异常意外和未处理的异常的边界(您在开发时没有预料到的事情)

  2. 对于所有其他情况,使用返回错误代码您的操作的返回值(这不是这方面的例外)

WCF does not restrict the return object or force you to throw exceptions to communicate errorenous conditions. It depends on your needs and agreements you make on an overall scale in your architecture.

In my applications, I always tend to have an int type return argument that goes negative when an error occurs and is zero when the operation is completed successfully. You can also have more complex return arguments that have error structures in them, like:

[DataContract]
public class ReturnValue
{
  [DataMember]
  public int ReturnCode;

  [DataMember]
  public string ResultingDescription;

  // enum listing severities like Critical, Warning, etc.
  [DataMember]
  public Severity Severity;
}

If you need complex returned parameters as returns of your operation you could have the following signature:

[OperationContract]
ReturnValue MyOperation (int param1, string param2, out MyOperationResult result);

My rule of thumb is:

  1. Only throw an exception over service boundaries for unexpected and unhandled exceptions (things you didn't expect at time of development)

  2. For all other situations, return an error code using the return value of your operation (it is not an exception with that respect)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文