Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何避免Autofac中的隐式集合解析?_C#_Asp.net_Collections_Autofac_Resolve - Fatal编程技术网

C# 如何避免Autofac中的隐式集合解析?

C# 如何避免Autofac中的隐式集合解析?,c#,asp.net,collections,autofac,resolve,C#,Asp.net,Collections,Autofac,Resolve,我正在ASP.NET中实现MVP模式,并定义了如下视图: public interface IMyView { IList<User> Users { get; set; } } public partial class MyPage : Page, IMyView { public IList<User> Users { set {

我正在ASP.NET中实现MVP模式,并定义了如下视图:

public interface IMyView
    {
         IList<User> Users { get; set; }
    }

    public partial class MyPage : Page, IMyView
    {
        public IList<User> Users
        {
            set
            {
                this.grid.DataSource = value;
                this.grid.DataBound();
            }
        }
公共界面IMyView
{
IList用户{get;set;}
}
公共部分类MyPage:Page,IMyView
{
公共IList用户
{
设置
{
this.grid.DataSource=值;
这个是.grid.DataBound();
}
}
我的示例页面继承自视图,但每当我访问页面时,Autofac会将默认空值分配给Users属性,DataBound会引发异常,因为我的页面尚未完全构建。 如何将Autofac配置为不自动解析?
现在我唯一能做的就是将IList更改为List。

最简单的解决方案是修改您的注册,而不是对此类型使用属性注入,这是我推荐的解决方案

顺便说一下,如果您不能更改注册模式,我可以看到3种解决方案

  • 通过使用状态更改属性的工作方式

    public MyPage: Page, IMyView
    {
    
      private Boolean _isInitialized; 
    
    
      public void Page_Init(Object sender, EventArgs e)
      {
        this._isInitialized = true; 
      }
    
    
      public IList<User> Users
      {
        get { ... }
        set
        {
          if (this._isInitialized) 
          {
            this.grid.DataSource = value;
            this.grid.DataBound();
          }
        }
      }
    }
    
    不要使用此代码,它只是为了让您获得iead。请查看此代码的良好实现

  • 删除Autofac中的收集支持。收集支持由管理。很遗憾,无法告诉Autofac仅删除收集支持,您只能删除所有默认适配器

    IContainer container = builder.Build(ContainerBuildOptions.ExcludeDefaultModules);
    
    这将删除所有默认模块,即:

    private void RegisterDefaultAdapters(IComponentRegistry componentRegistry)
    {
      this.RegisterGeneric(typeof(KeyedServiceIndex<, >)).As(new Type[]
      {
        typeof(IIndex<, >)
      }).InstancePerLifetimeScope();
      componentRegistry.AddRegistrationSource(new CollectionRegistrationSource());
      componentRegistry.AddRegistrationSource(new OwnedInstanceRegistrationSource());
      componentRegistry.AddRegistrationSource(new MetaRegistrationSource());
      componentRegistry.AddRegistrationSource(new LazyRegistrationSource());
      componentRegistry.AddRegistrationSource(new LazyWithMetadataRegistrationSource());
      componentRegistry.AddRegistrationSource(new StronglyTypedMetaRegistrationSource());
      componentRegistry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
    }
    
    专用无效注册表DefaultAdapters(IComponentRegistry componentRegistry)
    {
    this.RegisterGeneric(typeof(KeyedServiceIndex)).As(新类型[]
    {
    类型(IIndex)
    }).InstancePerLifetimeScope();
    AddRegistrationSource(新的CollectionRegistrationSource());
    AddRegistrationSource(新的OwnedInstanceRegistrationSource());
    AddRegistrationSource(新的MetaRegistrationSource());
    AddRegistrationSource(新LazyRegistrationSource());
    AddRegistrationSource(新的LazyWithMetadataRegistrationSource());
    componentRegistry.AddRegistrationSource(新的StronglyTypedMetaRegistrationSource());
    AddRegistrationSource(新生成的FactoryRegistrationSource());
    }
    
  • 不幸的是,当使用
    属性autowired()
    时,无法告诉Autofac不要注入特定属性。

    我发现了问题。 多亏了你的回答,我才找到了原因。 升级到Autofac版本3后,我在web配置中有以下模块:

    <add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web" preCondition="managedHandler" />
    
    
    
    因此,我需要将其移除并替换为以下内容:

     <add
            name="AttributedInjection"
            type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web"
            preCondition="managedHandler" />
    
    
    
    谢谢您的回答。我没有使用PropertiesAutoWired,甚至我也没有在Autofac中注册我的视图。@alirezam Autofac尝试在您的类型中设置属性,因此,有人正在用
    PropertiesAutoWired
    注册您的类型。编辑您的问题,并包括如何配置Autofac。那里可能有一个解决方案供您使用。我双重检查我没有使用所有属性Autowiring属性。在以前版本的Autofac中,我在升级到Autofac v3后没有遇到此问题。@alirezam您如何配置Autofac?其他一些东西可能在内部使用
    属性Autowiring
    。您能使用断点并查看调用堆栈以查看谁是cal吗l setter?有关信息,请介绍对IList的支持
     <add
            name="AttributedInjection"
            type="Autofac.Integration.Web.Forms.AttributedInjectionModule, Autofac.Integration.Web"
            preCondition="managedHandler" />