一直在浏览IErrorHandler并想要配置路由.所以,我已经阅读了以下内容,试图实现它.
这基本上只是包含在继承IErrorHandler和IServiceBehaviour的类中的msdn示例...然后它包含在继承自BehaviourExtensionElement的Extension元素中,据称允许我将该元素添加到web.config中.我错过了什么?
我已经得到它编译,并从我已修复的各种错误,似乎WCF实际上加载错误处理程序.我的问题是我在错误处理程序中处理的异常不会导致传递给它的异常.
我的服务实现只是在另一个抛出ArgumentOutOfRangeException的类上调用一个方法 - 但是这个异常永远不会被处理程序处理.
我的web.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basic">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="customHttpBehavior"
type="ErrorHandlerTest.ErrorHandlerElement, ErrorHandlerTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="exceptionHandlerBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<customHttpBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="exceptionHandlerBehaviour" name="ErrorHandlerTest.Service1">
<endpoint …Run Code Online (Sandbox Code Playgroud) 无法使用正确的web.config将IErrorHandler集成到我的项目中
我有一个成功运行的WCF,正在被.net 4中的webclients使用,但是当我尝试将IErrorhandler设置为全局错误记录器作为所有我的所有服务方法的捕获时,事情正在快速失败 - 主要与web.config部分有关!请帮忙.
这三项服务是:IReport,IServiceCustomer,IServiceUser
在一个名为MyErrorClass.cs的单独类中实现了IErrorHandler,如下所示:
namespace CustomerWcfService
{
public class WcfErrorHandler : IErrorHandler
{
/// <summary>
/// Enables the creation of a custom <see cref="T:System.ServiceModel.FaultException`1"/> that is returned from an exception in the course of a service method.
/// </summary>
/// <param name="error">The <see cref="T:System.Exception"/> object thrown in the course of the service operation.</param><param name="version">The SOAP version of the message.</param><param name="fault">The <see cref="T:System.ServiceModel.Channels.Message"/> object that is returned to the client, or service, in the duplex case.</param>
public void …Run Code Online (Sandbox Code Playgroud) 我正在实现IErrorHandler,以便在一个地方集中我的WCF服务的所有错误处理.这很好用:
public class ServiceErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
// ..Log..
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// ..Provide fault..
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我们使用Ninject在服务的其余部分注入依赖项,我想在这里做同样的事情.由于WCF正在根据我的配置构建对象,并且我认为我没有任何钩子进入此过程,我需要使用属性注入:
[Inject]
public ILoggingService Logger { get; set; }
Run Code Online (Sandbox Code Playgroud)
然而,这似乎永远不会被注入.我尝试使用Ninject的MVC扩展来设置ServiceErrorHandler允许像过滤器一样注入,但这似乎没有成功.有没有办法让这种情况发生?
我已经实现了IErrorHandler来处理在我的restful WCF服务的构造函数中抛出的授权异常.捕获常规异常时,我的自定义类型按预期返回,但ContentType标头不正确.
HTTP/1.1 500 Internal Server Error
Content-Type: application/xml;
...
{"ErrorMessage":"Error!"}
Run Code Online (Sandbox Code Playgroud)
但是,当错误处理程序尝试返回401 Unauthorized http状态代码时,消息正文将被覆盖为默认类型,但ContentType标头应该是它应该的样本.
HTTP/1.1 401 Unauthorized
Content-Type: application/json;
...
{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
Run Code Online (Sandbox Code Playgroud)
显然这里有些不对劲,但我不确定是什么.
如何实现IErrorHandler,使其在json中使用正确的标头返回我的自定义类型?
BaseDataResponseContract对象:
[Serializable]
[DataContract( Name = "BaseDataResponseContract" )]
public class BaseDataResponseContract
{
[DataMember]
public string ErrorMessage { get; set; }
} // end
Run Code Online (Sandbox Code Playgroud)
这是我想要返回的对象.我的应用程序中的所有其他对象都继承自此对象.当抛出异常时,我们真正关心的是http状态代码和错误消息.
IErrorHandler实现(为简洁起见,未显示日志记录):
namespace WebServices.BehaviorsAndInspectors
{
public class ErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
} // end
public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
{
// Create a …Run Code Online (Sandbox Code Playgroud) 我基本上实现了IErrorHandler接口,以从WCF服务捕获各种异常,并通过实现该ProvideFault方法将其发送到客户端.
然而,我面临一个关键问题.所有异常都作为a发送到客户端,FaultException但这会禁用客户端来处理他可能在服务中定义的特定异常.
考虑:SomeException已经在其中一个OperationContract实现中定义并抛出了它.抛出异常时,使用以下代码将其转换为错误:
var faultException = new FaultException(error.Message);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);
Run Code Online (Sandbox Code Playgroud)
这会将错误作为字符串发送,但客户端必须捕获一般异常,例如:
try{...}
catch(Exception e){...}
Run Code Online (Sandbox Code Playgroud)
并不是:
try{...}
catch(SomeException e){...}
Run Code Online (Sandbox Code Playgroud)
不仅像SomeException这样的自定义异常,而且使用上述过程也无法捕获像InvalidOperationException这样的系统异常.
关于如何实现这种行为的任何想法?
一些上下文:我们有一个自定义XSD,并使用WSCF.blue生成WSDL和C#代码.客户端使用ChannelFactory<T>并共享接口,该接口包括WSCF.blue添加的所有属性以匹配XSD中的内容.
我正在尝试实现IErrorHandler.ProvideFault它提供泛型的地方FaultException<T>,但在客户端我得到的是非泛型的FaultContract.这是我的ProvideFault方法的样子:
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (!(error is FaultException))
{
FaultException faultException = FaultExceptionFactory.CreateFaultException(error);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);
}
}
Run Code Online (Sandbox Code Playgroud)
在每个服务方法中,如果我使用try/catch,throw FaultExceptionFactory.CreateFaultException(ex)它按预期工作,所以我认为[FaultContract],工厂,绑定等都是正确的.以防万一,这就是工厂的工作原理:
BusinessRuleFaultExceptionType businessRuleFaultException = new BusinessRuleFaultExceptionType();
BusinessRuleFaultException.Code = exception.Code.ToString();
BusinessRuleFaultException.Reason = exception.Message;
return new FaultException<BusinessRuleFaultExceptionType>(
businessRuleFaultException,
exception.Message,
new FaultCode(exception.Code.ToString())
);
Run Code Online (Sandbox Code Playgroud)
我认为问题在于如何创建消息IErrorHandler,也许是在CreateMessageFault().我已经读过这个动作应该faultException.Action代替null,但实际上faultException.Action是null.也许这导致了这个问题.我可以在工厂设置一个动作,但是动作应该是什么,为什么不会出现手动抛出? …
可能是微不足道的问题..我想实现我自己的错误处理程序来记录错误并监视正在发生的事情.此时我不想向客户提供自己的错误.我希望它是透明的 - 就像默认的WCF行为一样.我应该如何ProvideFault实现这一目标?
namespace IDATT.Web.Services
{
using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
public class MyServiceErrorHandler : IErrorHandler
{
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// ????
}
public bool HandleError(Exception error)
{
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在为WCF服务配置ServiceBehavior时遇到问题.
一些背景.基本上我正在开发一个应该在IIS上运行的REST服务WCF.我需要能够记录服务抛出的异常(我正在使用log4net)并根据异常类型返回HTTP状态代码.我希望我的服务实现对WCF相关的东西知之甚少,所以我不想在服务的每个地方将异常转换为FaultException.所以我发现将自己的IErrorHandler添加到服务主机是最好的方法.
我的问题是,无论我尝试什么,我似乎无法在Web.config中获得我的自定义ServiceBehavior的配置.这是相关的代码.
网络配置.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="UsingErrorLogBehavior">
<errorLogBehavior/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="errorLogBehavior"
type="MyNameSpace.Web.ErrorExtensionElement, MyNameSpace.Web"/>
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="false"
defaultOutgoingResponseFormat="Json"
maxReceivedMessageSize="4194304" transferMode="Buffered" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
ErrorExtensionElement.
namespace MyNameSpace.Web
{
public class ErrorExtensionElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(ErrorServiceBehavior); }
}
protected override object CreateBehavior()
{ …Run Code Online (Sandbox Code Playgroud) 我已经采用了LuaJSON来解析JSON.解析调用似乎是这样的:
-- file.lua
local res = json.decode.decode(json_str)
if res == nil then
throw('invalid JSON')
end
...
Run Code Online (Sandbox Code Playgroud)
但如果json_str是严重格式化时,decode()将在LuaJSON停止和中断的执行file.lua.我希望控制流返回到我的函数,所以我可以提供自定义错误通知.
我浏览了LuaJSON API,并且没有类似回调的错误处理.我想知道是否有任何Lua机制允许我从file.lua中处理LuaJSON中发生的错误?
我有这种情况:我有WCF服务.我正在通过MyErrorHandler实现的接口处理所有异常IErrorHandler.整个工作代码如下.
我想做什么,但不知道如何:我想将一个对象(例如ILogger)注入到MyErrorHandler类中.它基本上意味着我必须在这里注入一个对象:[GlobalErrorHandlerBehaviour(typeof(MyErrorHandler))].你能帮我解决一下这个问题吗?
[ServiceContract]
public interface ITestService
{
[OperationContract]
int GetTest();
}
[GlobalErrorHandlerBehaviour(typeof(MyErrorHandler))]
public class TestService : ITestService
{
public TestService(ILogger logger)
{
// Here, I'm already injecting logger.
// It's not imported for my question so I removed it for now
}
public int GetTest()
{
throw new Exception("Test");
}
}
// This is attribute added to TestService class
// How can I inject (via constructor) ILogger, or any other class??
public …Run Code Online (Sandbox Code Playgroud)