简体   繁体   English

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

[英]Unity and ASP.NET WebForms - No parameterless constructor defined for this object

Does anyone have any good examples of how to make Unity 1.2 or 2.0 work with ASP.NET WebForms? 有没有人有任何关于如何使Unity 1.2或2.0与ASP.NET WebForms一起工作的好例子?

I thought I had this figured out, but evidently I'm missing something. 我以为我弄明白了,但显然我错过了一些东西。 Now I'm getting the error; 现在我收到了错误; "No parameterless constructor defined for this object". “没有为此对象定义无参数构造函数”。 I remember getting this error a couple years ago, I and just don't remember what I did. 我记得几年前收到这个错误,我只是不记得我做了什么。

Obviously Unity isn't working as it should because somewhere along the way I've forgotten something. 显然Unity并没有正常工作,因为我忘记了某些事情。 Any help would be appreciated. 任何帮助,将不胜感激。

Here's some of my code: 这是我的一些代码:

Global.asax Global.asax中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using Microsoft.Practices.Unity;

using PIA35.Unity;

namespace PIA35.Web
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            IUnityContainer container = Application.GetContainer();
            PIA35.Web.IoC.Bootstrapper.Configure(container);
        }
    }
}

Here's my httpModules section of the web.config file: 这是web.config文件的httpModules部分:

<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add name="UnityHttpModule" type="PIA35.Unity.UnityHttpModule, PIA35.Unity"/>
</httpModules>

Here's the code for my IoC bootstrapper class. 这是我的IoC bootstrapper类的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;

using PIA35.Services.Interfaces;
using PIA35.Services;
using PIA35.DataObjects.Interfaces;
using PIA35.DataObjects.SqlServer;

namespace PIA35.Web.IoC
{
    public static class Bootstrapper
    {
        public static void Configure(IUnityContainer container)
        {
            container
                .RegisterType<ICategoryService, CategoryService>()
                .RegisterType<ICustomerService, CustomerService>()
                .RegisterType<IOrderService, OrderService>()
                .RegisterType<IOrderDetailService, OrderDetailService>()
                .RegisterType<IProductService, ProductService>()
                .RegisterType<ICategoryDao, SqlServerCategoryDao>()
                .RegisterType<ICustomerDao, SqlServerCustomerDao>()
                .RegisterType<IOrderDao, SqlServerOrderDao>()
                .RegisterType<IOrderDetailDao, SqlServerOrderDetailDao>()
                .RegisterType<IProductDao, SqlServerProductDao>();
        }
    }
}

Here's the HttpApplicationStateExtensions.cs file. 这是HttpApplicationStateExtensions.cs文件。

using System.Web;

using Microsoft.Practices.Unity;

namespace PIA35.Unity
{
    public static class HttpApplicationStateExtensions
    {
        private const string GlobalContainerKey = "GlobalUnityContainerKey";

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

Here's my UnityHttpModule.cs file. 这是我的UnityHttpModule.cs文件。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Microsoft.Practices.Unity;

namespace PIA35.Unity
{
    public class UnityHttpModule : IHttpModule
    {
        #region IHttpModule Members

        ///
        ///Initializes a module and prepares it to handle requests.
        ///
        ///
        ///An  
        ///that provides access to the methods, properties, 
        ///and events common to all application objects within an ASP.NET application 
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
        }

        ///
        ///Disposes of the resources (other than memory) 
        ///used by the module that implements .
        ///
        ///
        public void Dispose()
        {
        }

        #endregion

        private void OnPreRequestHandlerExecute(object sender, EventArgs e)
        {
            IHttpHandler handler = HttpContext.Current.Handler;
            HttpContext.Current.Application.GetContainer().BuildUp(handler.GetType(), handler);

            // User Controls are ready to be built up after the page initialization is complete
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                page.InitComplete += OnPageInitComplete;
            }
        }

        // Get the controls in the page's control tree excluding the page itself
        private IEnumerable GetControlTree(Control root)
        {
            foreach (Control child in root.Controls)
            {
                yield return child;
                foreach (Control c in GetControlTree(child))
                {
                    yield return c;
                }
            }
        }

        // Build up each control in the page's control tree
        private void OnPageInitComplete(object sender, EventArgs e)
        {
            Page page = (Page)sender;
            IUnityContainer container = HttpContext.Current.Application.GetContainer();
            foreach (Control c in GetControlTree(page))
            {
                container.BuildUp(c.GetType(), c);
            }
        }
    }
}

Here's an example of one of my service classes. 这是我的一个服务类的示例。

namespace PIA35.Services
{
    public class CategoryService : ICategoryService
    {

        #region Dependency Injection

        private ICategoryDao categoryDao;

        public CategoryService(ICategoryDao CategoryDao)
        {
            this.categoryDao = CategoryDao;
        }

        #endregion


        #region ICategoryService Members

        public List GetAll()
        {
            return categoryDao.GetAll().ToList();
        }

        public Category GetById(int CategoryId)
        {
            return categoryDao.GetById(CategoryId);
        }

        public void Add(Category model)
        {
            categoryDao.Insert(model);
        }

        public void Update(Category model)
        {
            categoryDao.Update(model);
        }

        public void Delete(Category model)
        {
            categoryDao.Delete(model);
        }

        #endregion
    }
}

I see it has already been answered but just thought I would point out that you are synchronising all the calls to GetContainer with your locking pattern. 我看到它已经得到了回答,但我想我会指出你正在使用锁定模式同步所有对GetContainer的调用。 A call to Application.Lock() actually takes out a write lock on the applicationState which is a singleton object in your web application and you will see issues if you want to scale this. 对Application.Lock()的调用实际上在applicationState上取出了一个写锁定,它是Web应用程序中的单例对象,如果你想扩展它,你会看到问题。

To tidy this up you could do a double checked lock. 为了整理这个,你可以做一个双重检查锁。 like this: 像这样:

    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;
    }

I would also like to point out a neat pattern that we have used to ensure Controls and Pages have their Dependencies built up. 我还想指出一个简洁的模式,我们已经用它来确保控件和页面的依赖关系建立起来。 We basically have a Generic PageBase and Generic ControlBase that all our pages and controls inherit from. 我们基本上有一个Generic PageBase和Generic ControlBase,我们所有的页面和控件都继承自。 I will just go into the pagebase as an example: 我将以页面库为例进行说明:

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 );
    }
}

Then in our Pages we can simply derive from Generic base and it will look after the build up. 然后在我们的页面中,我们可以简单地从Generic base派生,它将负责构建。

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

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

The ObjectDataSource can take an interface, but not with the wizard. ObjectDataSource可以使用接口,但不能使用向导。 You can use the wizard to create the ObjectDataSource tag then edit it and turn the TypeName attribute value into your interface name. 您可以使用向导创建ObjectDataSource标记,然后编辑它并将TypeName属性值转换为您的接口名称。

Then, you need to instruct the ObjectDataSource how to create the object. 然后,您需要指示ObjectDataSource如何创建对象。 The way i'm using is to handle the OnObjectCreating event, so in the code behind I have: 我正在使用的方法是处理OnObjectCreating事件,所以在后面的代码我有:

[Dependency]
public IMyService Service { get; set; }

protected void OnObjectCreating(...)
{
   e.ObjectInstance = Service;
}

I had a working project a while back and I started a new project and getting the same problem. 我有一个工作项目,我开始一个新的项目,并得到同样的问题。 Make some comparison and took me a while. 做一些比较并带我一段时间。 But I remembered that you need to initialise it in the global.asax. 但我记得你需要在global.asax中初始化它。

Bootstrapper.Initialise(); // Missing in the global.asax

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 asp.net web 服务中没有为此对象错误定义无参数构造函数 - No parameterless constructor defined for this object error in asp.net web service ASP.NET MVC:没有为此定义无参数构造函数 object - ASP.NET MVC: No parameterless constructor defined for this object 在ASP.NET MVC中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in ASP.NET MVC ASP.NET MVC 4 + Ninject MVC 3 = 没有为此对象定义无参数构造函数 - ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object 没有为此对象定义无参数构造函数ASP.NET网站 - No parameterless constructor defined for this object ASP.NET Website ASP.NET Core 标识用户管理器<ApplicationUser> -Error 没有为此对象定义无参数构造函数 - ASP.NET Core Identity UserManager<ApplicationUser> -Error No parameterless constructor defined for this object Unity:没有为此对象定义无参数构造函数。 - Unity : No parameterless constructor defined for this object. 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object 没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时 - No parameterless constructor defined for this object. while i try for Unity Dependency ASP Net Core MVC创建实例控制器System.MissingMethodException:&#39;为此对象未定义无参数构造函数。 - asp net core mvc create instance controller System.MissingMethodException: 'No parameterless constructor defined for this object.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM