我正在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.");
}
}
}
答案 0 :(得分:0)
代码500是内部错误。
发生了一些错误,并且未在服务器端捕获。
在您的代码中,您只捕获OptimisticConcurrencyException,您还需要捕获Exception。
答案 1 :(得分:0)
我设法让它发挥作用。我想出我的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);
}
}
}