gpt4 book ai didi

c# - Unity和ASP.NET WebForms-没有为此对象定义无参数构造函数

转载 作者:行者123 更新时间:2023-12-03 17:46:17 28 4
gpt4 key购买 nike

有没有人有任何好的示例说明如何使Unity 1.2或2.0与ASP.NET WebForms一起使用?

我以为我已经弄清楚了,但是显然我缺少了一些东西。现在我得到了错误; “没有为此对象定义无参数构造函数”。我记得几年前收到此错误,但我不记得自己做了什么。

显然,Unity无法正常工作,因为在此过程中我忘记了某些东西。任何帮助,将不胜感激。

这是我的一些代码:

Global.asax

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.SessionState;

使用Microsoft.Practices.Unity;

使用PIA35.Unity;

命名空间PIA35.Web
{
公共类Global:System.Web.HttpApplication
{

受保护的void Application_Start(对象发送者,EventArgs e)
{
IUnityContainer容器= Application.GetContainer();
PIA35.Web.IoC.Bootstrapper.Configure(容器);
}
}
}


这是web.config文件的httpModules部分:








这是我的IoC引导程序类的代码。

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用Microsoft.Practices.Unity;

使用PIA35.Services.Interfaces;
使用PIA35.Services;
使用PIA35.DataObjects.Interfaces;
使用PIA35.DataObjects.SqlServer;

命名空间PIA35.Web.IoC
{
公共静态类Bootstrapper
{
公共静态无效的Configure(IUnityContainer容器)
{
容器
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ()
.RegisterType ();
}
}
}



这是HttpApplicationStateExtensions.cs文件。

使用System.Web;

使用Microsoft.Practices.Unity;

命名空间PIA35.Unity
{
公共静态类HttpApplicationStateExtensions
{
私有常量字符串GlobalContainerKey =“ GlobalUnityContainerKey”;

公共静态IUnityContainer GetContainer(此HttpApplicationState应用程序)
{
application.Lock();
尝试
{
IUnityContainer容器= application [GlobalContainerKey]作为IUnityContainer;
如果(容器==空)
{
container = new UnityContainer();
application [GlobalContainerKey] =容器;
}
返回容器;
}
最后
{
application.UnLock();
}
}
}
}



这是我的UnityHttpModule.cs文件。

使用系统;
使用System.Collections.Generic;
使用System.Web;
使用System.Web.UI;
使用Microsoft.Practices.Unity;

命名空间PIA35.Unity
{
公共类UnityHttpModule:IHttpModule
{
#region IHttpModule成员

///
///初始化模块并准备处理请求。
///
///
///一个
///可以访问方法,属性,
///和ASP.NET应用程序中所有应用程序对象共有的事件
公共无效的初始化(HttpApplication上下文)
{
context.PreRequestHandlerExecute + = OnPreRequestHandlerExecute;
}

///
///处置资源(内存除外)
///由实现的模块使用。
///
///
公共无效Dispose()
{
}

#endregion

私有无效OnPreRequestHandlerExecute(对象发送者,EventArgs e)
{
IHttpHandler处理程序= HttpContext.Current.Handler;
HttpContext.Current.Application.GetContainer()。BuildUp(handler.GetType(),处理程序);

//在页面初始化完成后即可构建用户控件
页面page = HttpContext.Current.Handler作为Page;
如果(页面!=空)
{
page.InitComplete + = OnPageInitComplete;
}
}

//在页面的控件树中获取控件,但不包括页面本身
私有IEnumerable GetControlTree(控件根)
{
foreach(root.Controls中的控制子项)
{
退还孩子;
foreach(GetControlTree(子级)中的控件c)
{
收益率c;
}
}
}

//在页面的控件树中建立每个控件
私有无效OnPageInitComplete(对象发送者,EventArgs e)
{
页面page =(Page)sender;
IUnityContainer容器= HttpContext.Current.Application.GetContainer();
foreach(GetControlTree(页面)中的控件c)
{
container.BuildUp(c.GetType(),c);
}
}
}
}


这是我的一个服务类别的示例。

命名空间PIA35.Services
{
公共类CategoryService:ICategoryService
{

#region依赖注入

私人ICategoryDao类Dao;

公共类别服务(ICategoryDao CategoryDao)
{
this.categoryDao = CategoryDao;
}

#endregion


#region ICategoryService成员

公开列表GetAll()
{
返回categoryDao.GetAll()。ToList();
}

公共类别GetById(int CategoryId)
{
返回categoryDao.GetById(CategoryId);
}

public void Add(类别模型)
{
categoryDao.Insert(model);
}

公共无效更新(类别模型)
{
categoryDao.Update(model);
}

public void Delete(类别模型)
{
categoryDao.Delete(model);
}

#endregion
}
}

最佳答案

我看到它已经得到了回答,但我想我要指出的是,您正在将对GetContainer的所有调用与您的锁定模式同步。对Application.Lock()的调用实际上是对applicationState上的写锁,而applicationState是Web应用程序中的单例对象,如果要扩展它,将会看到问题。

要整理一下,您可以进行双重检查锁定。像这样:

    public static IUnityContainer GetContainer(this HttpApplicationState application)
{
IUnityContainer container = application[GlobalContainerKey] as IUnityContainer;
if (container == null)
{
application.Lock();
try
{
container = application[GlobalContainerKey] as IUnityContainer;
if (container == null)
{
container = new UnityContainer();
application[GlobalContainerKey] = container;
}
}
finally
{
application.UnLock();
}
}
return container;
}


我还想指出一个整齐的模式,我们已经使用它来确保控件和页面具有依赖关系。基本上,我们的所有页面和控件都继承自Generic PageBase和Generic ControlBase。我将以页面库为例:

public abstract class SitePageBase<T> : SitePageBase where T : SitePageBase<T>
{
protected override void OnInit( EventArgs e )
{
BuildUpDerived();
base.OnInit( e );
}

protected void BuildUpDerived()
{
ContainerProvider.Container.BuildUp( this as T );
}
}


然后,在我们的页面中,我们可以简单地从泛型基础中派生出来,它将照会构建。

public partial class Default : SitePageBase<Default>
{
[Dependency]
public IContentService ContentService { get; set; }

protected override void OnPreRender( EventArgs e )
{
this.label.Text = ContentService.GetContent("labelText");
}
}

关于c# - Unity和ASP.NET WebForms-没有为此对象定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3663380/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com