我用這兩篇文章作爲參考,以實現IErrorHandler行爲擴展:錯誤而實施IErrorHandler - 不能添加
(我使用.NET Framework 3.5 。)
不過,我有一點麻煩 - 每當我試圖啓動服務,我得到以下錯誤:
System.Configuration.ConfigurationErrorsException:由於基礎行爲類型未實現IServiceBehavior接口,因此無法將行爲擴展名'errorHandlerExtension'添加到名爲'Test.TestService.Service1Behavior'的服務行爲。
這裏是我的代碼和配置文件:
ErrorhandlerExtension.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Configuration;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;
namespace Test.TestService
{
class ErrorHandlerExtension:BehaviorExtensionElement,IServiceBehavior
{
public override Type BehaviorType
{
get { return typeof(ErrorHandler); }
}
protected override object CreateBehavior()
{
return new ErrorHandler();
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
private IErrorHandler GetInstance()
{
return new ErrorHandler();
}
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)
{
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
{
if (endpoint.Contract.Name.Equals("IMetadataExchange") &&
endpoint.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
continue;
foreach (OperationDescription description in endpoint.Contract.Operations)
{
if (description.Faults.Count == 0)
{
throw new InvalidOperationException("FaultContractAttribute not found on this method");
}
}
}
}
}
}
的web.config:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="errorHandlerExtension"
type="Test.TestService.ErrorHandlerExtension, Test.TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="Test.TestService.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<errorHandlerExtension />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
每當我擺脫errorHandlerExtension /元素,該服務工作正常,但它無法啓動,只要我包含errorHandlerExtension元素(與錯誤)。我是WCF新手,很難過。有任何想法嗎?
編輯:添加ErrorHandler.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;
namespace Test.TestService
{
public class ErrorHandler:IErrorHandler
{
public bool HandleError(Exception error)
{
LogError("UnhandledError: "+ error);
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
FaultException faultException = new FaultException("Exception message:ProvideFault");
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);
}
}
}
這個ErrorHandler類在哪裏?它應該實現IServiceBehavior –
我在這裏添加了ErrorHandler類代碼 - 我在ErrorHandlerExtension類中實現了IServiceBehavior,類似於[Error Handler Does not seem ...](http://stackoverflow.com/questions/3036692/ierrorhandler-似乎並沒有處理我的錯誤在任何想法)後。 –
您應該在ErrorHandler中實現IServiceBehavior。你可以在你鏈接的帖子中看到這個。 –