3

我刚刚将我的一个 ASP.NET Core 项目更新到 1.1 版,因为我收到以下错误(第一次调用时工作正常,但在以下情况下失败)

无法访问已处置的对象。

在以下行:

    private ILogger logger;
    public ValuesController(ILoggerFactory loggingFactory)
    {
        logger = loggingFactory.CreateLogger<ValuesController>();
    }

该项目使用 DryIoc 作为容器,我的启动看起来像这样:

    using System;
using DryIoc;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using DryIoc.Microsoft.DependencyInjection;

namespace WebApplication2
{
    public class CompositionRoot
    {
        public CompositionRoot(IRegistrator r){}
    }

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc().AddControllersAsServices();
            return new Container()
            // setup DI adapter
            .WithDependencyInjectionAdapter(services,
                // optional: propagate exception if specified types are not resolved, and prevent fallback to default Asp resolution
                throwIfUnresolved: type => type.Name.EndsWith("Controller"))
            // add registrations from CompositionRoot classs
            .ConfigureServiceProvider<CompositionRoot>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }
}

我的 project.json 看起来像这样:

  {
"dependencies": {
  "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
  "Microsoft.Extensions.Logging": "1.1.0",
  "Microsoft.Extensions.Configuration.Json": "1.1.0",
  "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
  "Microsoft.AspNetCore.Mvc": "1.1.0",
  "Microsoft.AspNetCore.Routing": "1.1.0",
  "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
  "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
  "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
  "Microsoft.Extensions.Logging.Console": "1.1.0",
  "Microsoft.Extensions.Logging.Debug": "1.1.0",
  "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
  "DryIoc.Microsoft.DependencyInjection": "1.0.2"
},
  "tools": {
  },
  "frameworks": {
      "netcoreapp1.1": {
          "dependencies": {
              "Microsoft.NETCore.App": {
                  "type": "platform",
                  "version": "1.1.0"
              }
          }
      }
  },
  "buildOptions": {
      "emitEntryPoint": true
  },
  "runtimeOptions": {
      "configProperties": {
          "System.GC.Server": true
      }
  },
  "scripts": {
      "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }

}

任何帮助将不胜感激

4

2 回答 2

1

将您的 CompositionRoot 替换为

    public class CompositionRoot
    {
        public CompositionRoot(IRegistrator r)
        {
            r.Unregister<ILoggerFactory>();
            r.Register<ILoggerFactory, LoggerFactory>(Reuse.Singleton);
        }
    }

Ps 找到了你的票https://bitbucket.org/dadhi/dryioc/issues/432/loggingfactory-disposed-on-second-call

于 2017-01-16T20:40:22.490 回答
0

该问题已在v1.1.1中修复。

问题是由于外部实例注册为瞬态。一次性实例由容器跟踪,但将与第一个范围(请求)一起处置,因此将在第二个范围(请求)中抛出异常。现在它被注册为单例。

顺便提一句。要在 DryIoc 容器中找出问题或自定义框架注册,您可以将您的registerDescriptor委托提供给WithDependencyInjectionAdapter方法。DryIoc repo 中的示例使用示例用法进行了更新

于 2017-01-21T18:39:50.853 回答