简体   繁体   English

在行为中引发错误并将其返回给客户端

[英]Throwing a fault inside a behavior and returning it to client

I'm writing some services in WCF to be called by a Silverlight client. 我正在WCF中编写一些服务,以供Silverlight客户端调用。 I change status code to 200 every time a fault is to be returned, via a IDispatchMessageInspector. 每当通过IDispatchMessageInspector返回故障时,我都会将状态代码更改为200。

It works almost perfect, but sometimes it keeps returning error 500: NotFound. 它的工作原理几乎是完美的,但有时仍会返回错误500:NotFound。

I have just written another IDispatchMessageInspector to commit changes in a ObjectContext. 我刚刚编写了另一个IDispatchMessageInspector来提交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. 我认为,到UnitOfWorkMessageInspector运行时,该消息已被设置为非故障响应。 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.");
                }
            }
        }

Code 500 is Internal Error. 代码500是内部错误。

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. 在您的代码中,您仅捕获OptimisticConcurrencyException,还需要捕获Exception。

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. 我发现我的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);
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WCF 服务中抛出故障异常并在 Silverlight 中处理 - throwing Fault Exceptions in WCF service and handling in Silverlight MatrixTransform行为内的MatrixTransform不是我期望的 - MatrixTransform inside MatrixTransform behavior is not what I was expecting DomainService Include()方法不会将子级返回到Silverlight客户端 - DomainService Include() Method Not Returning Children To Silverlight Client 使用服务器实体上定义的方法,在具有WCF服务引用的客户端实体内部 - Using methods defined on server entity, inside client entity with WCF Service Reference Silverlight故障传播和UserNamePasswordValidator - Silverlight fault propagation and UserNamePasswordValidator Silverlight 4 HttpWebRequest引发ProtocolViolationException - Silverlight 4 HttpWebRequest throwing ProtocolViolationException WCF抛出随机ServiceActivationException - WCF throwing random ServiceActivationException 在Silverlight中抛出ReflectionTypeLoadException的继承WeakReference - Inherited WeakReference throwing ReflectionTypeLoadException in Silverlight DelegateCommand是否与“附加行为”相同? - Is a DelegateCommand the same as an “Attached Behavior”? BinaryReader的怪异行为 - Weird behavior with a BinaryReader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM