问题
I was using these two articles as a reference to implement IErrorHandler:
ErrorHandler doesn't seem...
Roy Primrose's blog post
(I'm using .net Framework 3.5.)
However, I am having a bit trouble - whenever I try to start the service, I get following error:
System.Configuration.ConfigurationErrorsException: Cannot add the behavior extension 'errorHandlerExtension' to the service behavior named 'Test.TestService.Service1Behavior' because the underlying behavior type does not implement the IServiceBehavior interface.
Here are my code and config files:
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>
Whenever I get rid of the errorHandlerExtension / element, the service works fine, but it fails to start whenever I include errorHandlerExtension element (with the error). I'm new to WCF and quite stumped. Any ideas?
edit: added 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);
}
}
}
回答1:
Like I stated above, Implementing the IServiceBehavior in the ErrorHandler instead of ErrorHandlerExtension solved this problem.
回答2:
I ran into the exact same issue. Turned out that I made a copy+paste error. Your problem - just as mine - can be also solved if BehaviorType
would return typeof(ErrorHandlerExtension)
and CreateBehavior
would return with new ErrorHandlerExtension()
来源:https://stackoverflow.com/questions/13123958/error-while-implementing-ierrorhandler-cannot-add-the-behavior-extension