私の Asp.Net プロジェクトでは、ILogger などにプロパティの自動配線を使用したいと考えています。基本的に、使用する必要があるクラスにプロパティとして配置しました。以下のように。
public class UserControlBase : UserControl
{
public ILogger CLogger { get; set; }
....
}
public partial class IPTracking : UserControlBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//it works
ILogger logger = ObjectFactory.GetInstance<ILogger>();
logger.LogInfo(string.Format("Client IP: {0}", ip));
//it does not work
CLogger.LogInfo(string.Format("Client IP: {0}", ip));
}
}
}
ただし、継承されたコントロールで呼び出す場合、logger は null です。コンテナーを確認したところ、上記の実装が示すように確実に設定されています。以下は Global.asax から呼び出す設定です。
public static void SetupForIoC()
{
Debug.WriteLine("SetupForIoC");
ObjectFactory.Initialize(x =>
{
x.FillAllPropertiesOfType<ILogger>().Use<EntLibLogger>();
});
Debug.WriteLine(ObjectFactory.WhatDoIHave());
}
アドバイスありがとう、ヒント?バツ。
更新:
- 前に言及しませんでしたが、その Asp.Net webforms 3.5.
- 欠けているものが見えません。インジェクションがプロセスの後半に関与し、要求されたクラスに設定されなかったことが原因である可能性があると思います。
説明へのリンク。使用法: http://structuremap.github.com/structuremap/ConstructorAndSetterInjection.htm#section7