02 | using System.Collections.ObjectModel; |
03 | using System.Diagnostics; |
04 | using System.ServiceModel; |
05 | using System.ServiceModel.Channels; |
06 | using System.ServiceModel.Description; |
07 | using System.ServiceModel.Dispatcher; |
08 | |
09 | namespace WcfServiceLibrary1 |
10 | { |
11 | public class ErrorHandler : IErrorHandler, IServiceBehavior |
12 | { |
13 | public void AddBindingParameters( |
14 | ServiceDescription serviceDescription, |
15 | ServiceHostBase serviceHostBase, |
16 | Collection<ServiceEndpoint> endpoints, |
17 | BindingParameterCollection bindingParameters) |
18 | { |
19 | } |
20 | |
21 | public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) |
22 | { |
23 | IErrorHandler errorHandler = new ErrorHandler(); |
24 | |
25 | foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers) |
26 | { |
27 | ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher; |
28 | |
29 | if (channelDispatcher != null ) |
30 | { |
31 | channelDispatcher.ErrorHandlers.Add(errorHandler); |
32 | } |
33 | } |
34 | } |
35 | |
36 | public bool HandleError(Exception error) |
37 | { |
38 | Trace.TraceError(error.ToString()); |
39 | |
40 | // Returning true indicates you performed your behavior. |
41 | return true ; |
42 | } |
43 | |
44 | public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) |
45 | { |
46 | } |
47 | |
48 | public void ProvideFault(Exception error, MessageVersion version, ref Message fault) |
49 | { |
50 | // Shield the unknown exception |
51 | FaultException faultException = new FaultException( |
52 | "Server error encountered. All details have been logged." ); |
53 | MessageFault messageFault = faultException.CreateMessageFault(); |
54 | |
55 | fault = Message.CreateMessage(version, messageFault, faultException.Action); |
56 | } |
57 | } |
58 | } |
No comments:
Post a Comment