Unity IOC inject interface in ASP Control
Unity IOC inject interface in ASP Control
我想将接口注入 asp 用户控件。我的 类 看起来像:
public partial class PercentByState : CommonControlsData
{
[Dependency]
public IChartService ChartService { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = ReportService.DoSomething();
}
}
}
public class CommonControlsData : System.Web.UI.UserControl
{
[Dependency]
public IReportService ReportService { get; set; }
}
我使用 Unity 和 Unity.WebForms nuget 包。
RegisterDependencies 方法如下所示:
private static void RegisterDependencies( IUnityContainer container )
{
//services
container.RegisterType<IReportService, ReportService>();
}
ReportService 和 ChartService 在 Page_Load() 函数中为空。
知道我做错了什么吗?
编辑:
我正在使用以下代码动态加载控件:
TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
string controlName = feature.ControlName;
tableCell.Controls.Add(Page.LoadControl(controlName));
tableRow.Controls.Add(tableCell);
MainContentTable.Rows.Add(tableRow);
此外,我的控件未使用 Register 命令注册到页面。我觉得跟这种加载方式有关系。
由于您是动态加载控件,因此您需要手动构建它们:
TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
string controlName = feature.ControlName;
var control = Page.LoadControl(controlName);
Context.GetChildContainer().BuildUp(control.GetType().UnderlyingSystemType, control);
tableCell.Controls.Add(control);
tableRow.Controls.Add(tableCell);
MainContentTable.Rows.Add(tableRow);
为了让 Unity 构建控件,它必须知道要构建的控件的实际类型,这就是为什么在 BuildUp
调用中使用 UnderlyingSystemType 作为类型参数的原因。另一种方法是将控制转换为适当的类型,但在编译时可能不知道。
我想将接口注入 asp 用户控件。我的 类 看起来像:
public partial class PercentByState : CommonControlsData
{
[Dependency]
public IChartService ChartService { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = ReportService.DoSomething();
}
}
}
public class CommonControlsData : System.Web.UI.UserControl
{
[Dependency]
public IReportService ReportService { get; set; }
}
我使用 Unity 和 Unity.WebForms nuget 包。
RegisterDependencies 方法如下所示:
private static void RegisterDependencies( IUnityContainer container )
{
//services
container.RegisterType<IReportService, ReportService>();
}
ReportService 和 ChartService 在 Page_Load() 函数中为空。
知道我做错了什么吗?
编辑: 我正在使用以下代码动态加载控件:
TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
string controlName = feature.ControlName;
tableCell.Controls.Add(Page.LoadControl(controlName));
tableRow.Controls.Add(tableCell);
MainContentTable.Rows.Add(tableRow);
此外,我的控件未使用 Register 命令注册到页面。我觉得跟这种加载方式有关系。
由于您是动态加载控件,因此您需要手动构建它们:
TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
string controlName = feature.ControlName;
var control = Page.LoadControl(controlName);
Context.GetChildContainer().BuildUp(control.GetType().UnderlyingSystemType, control);
tableCell.Controls.Add(control);
tableRow.Controls.Add(tableCell);
MainContentTable.Rows.Add(tableRow);
为了让 Unity 构建控件,它必须知道要构建的控件的实际类型,这就是为什么在 BuildUp
调用中使用 UnderlyingSystemType 作为类型参数的原因。另一种方法是将控制转换为适当的类型,但在编译时可能不知道。