Wcf サービス プロジェクトで Castle windsor 3.0 を使用しています。Topshelf を使用して Windows サービス プロジェクトとしてホストしています。私の wcf サービス構成はすべて app.config ファイルにあります。私は城の wcffacility を使用しており、このようなサービスを登録しています -
Container.AddFacility<WcfFacility>();
Container.AddFacility<LoggingFacility>(f => f.UseLog4Net());
container.Register(
Classes
.FromThisAssembly()
.Pick()
.If(x => x.IsClass
&& HasServiceContract(x))
.WithServiceDefaultInterfaces()
.Configure(c => c.AsWcfService().LifeStyle.HybridPerWcfOperationTransient()));
これにより、ILog (log4net から) が問題なくサービスに挿入されますが、IErrorHandler への挿入に失敗します。
IErrorHandler を使用して ServiceBehaviour を追加したので、ユーザーの未処理の例外をキャッチし、以下のコードを使用してエラーをログに記録できます。
#region IErrorHandler Members
public ILog Logger { get; set; }
public bool HandleError(Exception error)
{
if (error is FaultException)
return false; // Let WCF do normal processing
return true; // Fault message is already generated
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
var uniqueKey = UniqueKeyGenerator.Generate();
//create the custom wcfexception before passing that to client
var wcfException = new CustomServerException("Unknown Error has occured. Please contact your administrator!", uniqueKey);
//log the exception
Logger.Error(uniqueKey, error);
MessageFault messageFault = MessageFault.CreateFault(
new FaultCode("Sender"),
new FaultReason(wcfException.Message),
wcfException,
new NetDataContractSerializer());
fault = Message.CreateMessage(version, messageFault, null);
}
このstackoverflowの投稿を見ましたが、古すぎて回答が投稿されていなかったため、この新しい質問を投稿しています。
アップデート
私はgreyAlienによって提供された答えでこれを解決しました(今のところ部分的に)。私がしなければならなかったのは
カスタム サービス動作クラスをウィンザー城に登録します。
Container.Register( Component.For<IServiceBehavior>() .ImplementedBy<PassThroughExceptionHandlingBehaviour>() .Named("IServiceBehavior")
app.config ファイルから serviceBehaviour 拡張機能を削除します。構成ファイルに動作拡張を追加すると、何らかの理由で城が依存関係を注入できず、代わりに Wcf が新しいインスタンスを作成していて、ロガーのパブリック プロパティが null になっていると思います。
現在は機能していますが、behaviourextensions を使用して依存関係を注入する方法についても (将来) 理解する必要があります。