在行为中抛出错误并将其返回给客户端

发布于 2024-10-19 09:57:38 字数 848 浏览 9 评论 0原文

我正在 WCF 中编写一些由 Silverlight 客户端调用的服务。每次返回错误时,我都会通过 IDispatchMessageInspector 将状态代码更改为 200。

它工作得几乎完美,但有时它会不断返回错误 500:NotFound。

我刚刚编写了另一个 IDispatchMessageInspector 来提交 ObjectContext 中的更改。但是当失败时,不会调用错误处理程序。

我认为在 UnitOfWorkMessageInspector 运行时,该消息已被设置为非故障响应。我怎样才能让这两件事都发挥作用?

    public class UnitOfWorkMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!reply.IsFault)
            {
                try
                {
                    UnitOfWorkBase.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    throw new FaultException("It was changed by another user. Try again.");
                }
            }
        }

I'm writing some services in WCF to be called by a Silverlight client. I change status code to 200 every time a fault is to be returned, via a IDispatchMessageInspector.

It works almost perfect, but sometimes it keeps returning error 500: NotFound.

I have just written another IDispatchMessageInspector to commit changes in a ObjectContext. But when this fails, the error handler is not called.

I think by the time UnitOfWorkMessageInspector runs, the message was already been set up as a non-fault-response. How can I do both things work?

    public class UnitOfWorkMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!reply.IsFault)
            {
                try
                {
                    UnitOfWorkBase.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    throw new FaultException("It was changed by another user. Try again.");
                }
            }
        }

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

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

发布评论

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

评论(2

青春有你 2024-10-26 09:57:38

代码 500 是内部错误。

发生了一些错误,但服务器端未捕获该错误。

在您的代码中,您只捕获 OptimisticConcurrencyException,您还需要捕获 Exception。

Code 500 is Internal Error.

Some error is occuring and it is not caught on the server side.

In your code you only catch OptimisticConcurrencyException you need to also catch Exception.

我纯我任性 2024-10-26 09:57:38

我设法让它发挥作用。我发现我的 IDispatchMessageInspector 从状态代码 500 切换到 200 不起作用,因为消息已经构建,然后我替换了该消息。

    public class UnitOfWorkMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!reply.IsFault)
            {
                try
                {
                    UnitOfWorkBase.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    FaultException fe = new FaultException("It was changed by another user. Try again.");
                    reply = Message.CreateMessage(reply.Version, fe.CreateMessageFault(), fe.Action);
                }
            }
        }

I managed to make it work. I figured out my IDispatchMessageInspector for switching from status code 500 to 200 wouldn't work because the message was already built, then I replaced the message.

    public class UnitOfWorkMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!reply.IsFault)
            {
                try
                {
                    UnitOfWorkBase.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    FaultException fe = new FaultException("It was changed by another user. Try again.");
                    reply = Message.CreateMessage(reply.Version, fe.CreateMessageFault(), fe.Action);
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文