IErrorHandler を実装するためのリファレンスとして、次の 2 つの記事を使用していました。
(私は .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);
}
}
}