Thông thường dịch vụ WCF được lưu trữ trong ServiceHost, nếu WCF-Service không thành công thì tùy chọn duy nhất là hủy dịch vụ WCF và bắt đầu dịch vụ WCF mới.
Các ServiceHost có kích hoạt sự kiện "đứt gãy" được kích hoạt khi các dịch vụ WCF thất bại:
ServiceHost host = new ServiceHost(new Service.MyService());
host.Faulted += new EventHandler(host_faulted);
host.Open();
Có thể để có được ngoại lệ gây ra lỗi, nhưng nó đòi hỏi một chút công việc nhiều hơn:
public class ErrorHandler : IErrorHandler
{
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
}
public bool HandleError(Exception error)
{
Console.WriteLine("exception");
return false;
}
}
public class ErrorServiceBehavior : IServiceBehavior
{
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
ErrorHandler handler = new ErrorHandler();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(handler);
}
}
}
ServiceHost host = new ServiceHost(new Service.MyService());
host.Faulted += new EventHandler(host_faulted);
host.Description.Behaviors.Add(new ErrorServiceBehavior());
host.Open();
Tín http://www.haveyougotwoods.ca/2009/06/24/creating-a-global-error-handler-in-wcf
Nguồn
2009-12-02 15:12:01
ở đây bạn có thể nhận được tất cả các lý thuyết http://msdn.microsoft.com/en-us/library/ms789041(v=vs.110).aspx –