Đã đọc xung quanh trên IErrorHandler và muốn đi tuyến đường cấu hình. vì vậy, tôi đã đọc những điều sau đây trong một nỗ lực để thực hiện nó.IErrorHandler dường như không xử lý các lỗi của tôi trong WCF .. bất kỳ ý tưởng nào?
Keyvan Nayyeri blog about the type defintion
này về cơ bản là chỉ là ví dụ MSDN bọc trong một lớp kế thừa IErrorHandler và IServiceBehaviour ... thì đây được gói trong các yếu tố mở rộng được thừa kế từ BehaviourExtensionElement bị cáo buộc cho phép tôi thêm phần tử vào web.config. Tôi đã bỏ lỡ điều gì?
Tôi đã có nó để biên dịch và từ các lỗi khác nhau tôi đã cố định có vẻ như WCF thực sự đang tải trình xử lý lỗi. Vấn đề của tôi là ngoại lệ mà tôi đang ném để xử lý trong trình xử lý lỗi không nhận được ngoại lệ được truyền cho nó.
Việc triển khai dịch vụ của tôi chỉ đơn giản gọi một phương thức trên một lớp khác ném ArgumentOutOfRangeException - tuy nhiên ngoại lệ này không bao giờ được xử lý bởi trình xử lý.
web.config My
<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 binding="basicHttpBinding" bindingConfiguration="basic" contract="ErrorHandlerTest.IService1" />
</service>
</services>
Dịch vụ Hợp đồng
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(GeneralInternalFault))]
string GetData(int value);
}
Các ErrorHandler lớp
public class ErrorHandler : IErrorHandler , IServiceBehavior
{
public bool HandleError(Exception error)
{
Console.WriteLine("caught exception {0}:",error.Message);
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (fault!=null)
{
if (error is ArgumentOutOfRangeException)
{
var fe = new FaultException<GeneralInternalFault>(new GeneralInternalFault("general internal fault."));
MessageFault mf = fe.CreateMessageFault();
fault = Message.CreateMessage(version, mf, fe.Action);
}
else
{
var fe = new FaultException<GeneralInternalFault>(new GeneralInternalFault(" the other general internal fault."));
MessageFault mf = fe.CreateMessageFault();
fault = Message.CreateMessage(version, mf, fe.Action);
}
}
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandler = new ErrorHandler();
foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
{
channelDispatcher.ErrorHandlers.Add(errorHandler);
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
Và hành vi mở rộng phần tử
public class ErrorHandlerElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new ErrorHandler();
}
public override Type BehaviorType
{
get { return typeof(ErrorHandler); }
}
}
cảm ơn bạn rất nhiều, xin lỗi đã quá lâu để tôi trả lời thiết lập của tôi tại nơi làm việc là knackered. Điều này làm việc tốt trên thiết lập của tôi ở nhà mặc dù. –
Cảm ơn ví dụ đầy đủ. Điều này làm việc như một say mê! – Stewie
Làm việc cho tôi, mặc dù visual studio phàn nàn "hành vi của phần tử có errorhandler phần tử con không hợp lệ" - Tôi chỉ bỏ qua và trong thời gian chạy nó hoạt động. – BornToCode