WCF 常见逻辑和代码 1.错误处理和配置
这些东西都很多人写过了.....
放在这里只是做一个备份,要用的时候找的到 - -#
需要的人直接拷贝代码就可以运行了
注意只能运行在WCF 4.0中...因为为了少些点.....
以下是c#代码用于定义错误处理
View Code
public class ErrorHandler : IErrorHandler
{
#if DEBUG
public static ConcurrentDictionary<DateTime, Exception> ExceptionList = new ConcurrentDictionary<DateTime, Exception>();
#endif
private static WebBodyFormatMessageProperty bodyFormat = new WebBodyFormatMessageProperty(WebContentFormat.Xml);
private static HttpResponseMessageProperty responseMessage = new HttpResponseMessageProperty() { StatusCode = System.Net.HttpStatusCode.InternalServerError };
public bool HandleError(Exception error)
{
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
#if DEBUG
ExceptionList[DateTime.Now] = error;//just debug, don't care time conflict
#endif
Logger.Current.WriteEntry(error);
var exception = error as OperationException;//custom exception type
if (exception != null)
{
//要修改返回内容注意这里
fault = Message.CreateMessage(version, "", new OperationResult(Convert.ToInt32(exception.ErrorCode), exception.ToString()), new DataContractJsonSerializer(typeof(OperationResult)));
}
else
{
fault = Message.CreateMessage(version, "", new OperationResult(-1, error.ToString()), new DataContractJsonSerializer(typeof(OperationResult)));
}
//要修改返回类型注意这里
fault.Properties.Add(WebBodyFormatMessageProperty.Name, bodyFormat);//use xml format
fault.Properties.Add(HttpResponseMessageProperty.Name, responseMessage);//error code
}
}
public class ErrorHandlerExtension : BehaviorExtensionElement, IServiceBehavior
{
public override Type BehaviorType
{
get { return GetType(); }
}
protected override object CreateBehavior()
{
return this;
}
private IErrorHandler GetInstance()
{
return new ErrorHandler();
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandlerInstance = GetInstance();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(errorHandlerInstance);
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
/// <summary>
/// just wrap errorcode and errormessage
/// </summary>
public class OperationException : System.ApplicationException
{
public ErrorCode ErrorCode { get; set; }
public OperationException(ErrorCode errorCode)
{
this.ErrorCode = errorCode;
}
public OperationException(ErrorCode errorCode, string message)
: base(message)
{
this.ErrorCode = errorCode;
}
}
以下是在web.config中的配置
<extensions>
<behaviorExtensions>
<add name="errorHandler" type="{命名空间}.ValidationBehaviorSection, {dll名字}"/>
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior>
<errorHandler/>
</behavior>
</serviceBehaviors>
</behaviors>
要注意的方法就是ProvideFault 用于处理异常 (在没有执行完这个方法前不返回)
HandleError方法不阻止消息返回..用来写日志会好点