2016-07-05 87 views
3

我收到此錯誤:System.TypeLoadException未能加載類型 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions'

Error when call "services.AddSession()" in ConfigureServices after migrate from RC2 to ASP.NET Core 1.0

enter image description here

下面是詳細:

Error Message: System.TypeLoadException Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

項目.json:

{ 
    "dependencies": { 
    "ADMA.EWRS.Web.Security": "1.0.0-*", 
    "Microsoft.AspNetCore.Diagnostics": "1.0.0", 
    "Microsoft.AspNetCore.Mvc": "1.0.0", 
    "Microsoft.AspNetCore.Razor.Tools": { 
     "version": "1.0.0-preview2-final", 
     "type": "build" 
    }, 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", 
    "Microsoft.AspNetCore.StaticFiles": "1.0.0", 
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0", 
    "Microsoft.Extensions.Logging": "1.0.0", 
    "Microsoft.Extensions.Logging.Console": "1.0.0", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0", 
    //========================================================================= 
    //Murad Added 
    //========================================================================= 
    "Microsoft.AspNetCore.Authorization": "1.0.0", 
    "Microsoft.AspNetCore.Authentication": "1.0.0", 
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0", 
    "Microsoft.AspNetCore.Session": "1.0.0", 
    "Autofac": "4.0.0-rc3-286", 
    "Autofac.Extensions.DependencyInjection": "4.0.0-rc3-280", 
    "Microsoft.AspNetCore.DataProtection": "1.0.0", 
    "Microsoft.AspNetCore.Http.Abstractions": "1.0.0", 
    "Microsoft.Extensions.Caching.Abstractions": "1.0.0", 
    "Microsoft.Extensions.Logging.Abstractions": "1.0.0", 
    "Microsoft.Extensions.Options": "1.0.0", 
    "System.Security.Cryptography.Algorithms": "4.2.0", 
    //========================================================================= 
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", 
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0" 
    }, 

    "tools": { 
    "BundlerMinifier.Core": "2.0.238", 
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final", 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" 
    }, 

    "frameworks": { 
    "net461": { 
     "dependencies": { 
     "ADMA.EWRS.BizDomain": { 
      "target": "project" 
     }, 
     "ADMA.EWRS.Data.Models": { 
      "target": "project" 
     }, 
     "ADMA.EWRS.Security": { 
      "target": "project" 
     } 
     } 
    } 
    }, 

    "buildOptions": { 
    "emitEntryPoint": true, 
    "preserveCompilationContext": true 
    }, 

    "publishOptions": { 
    "include": [ 
     "wwwroot", 
     "Views", 
     "Areas/**/Views", 
     "appsettings.json", 
     "web.config" 
    ] 
    }, 

    "scripts": { 
    "prepublish": [ "bower install", "dotnet bundle" ], 
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] 
    } 
} 

Startup.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using Microsoft.AspNetCore.Http; 
using ADMA.EWRS.Web.Security.Claims; 
using ADMA.EWRS.Web.Security; 
using ADMA.EWRS.Security.Policy; 
using Microsoft.AspNetCore.Authorization; 
using Microsoft.AspNetCore.Mvc.Authorization; 
using ADMA.EWRS.Web.Security.Policy; 
using ADMA.EWRS.Data.Models.Security; 
using Autofac; 
using Autofac.Extensions.DependencyInjection; 

namespace ADMA.EWRS.Web.Core 
{ 
    public partial 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(); 

      if (env.IsDevelopment()) 
      { 
       // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. 
       //Murad : TODO : Add it later 
       //builder.AddApplicationInsightsSettings(developerMode: true); 
      } 
      Configuration = builder.Build(); 
     } 

     public IConfigurationRoot Configuration { get; } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     //Murad : When using a third-party DI container, you must change ConfigureServices so that it returns IServiceProvider instead of void. 
     public IServiceProvider ConfigureServices(IServiceCollection services) 
     { 
      // Add framework services. 
      //Murad :: Add Security 
      services.AddAuthorization(options => 
      { 
       PoliciesManager.BuildSystemPolicies(options); 
      }); 

      services.AddSession(); 

      // Murad Add this for RC2, remove it if release 1.0 after June :: AddRazorOptions 
      services.AddMvc(config => 
      { 
       var policy = new AuthorizationPolicyBuilder() 
           .RequireAuthenticatedUser() 
           .RequireRole(Groups.ADMAUserGroupName) 
           .Build(); 
       config.Filters.Add(new AuthorizeFilter(policy)); 

       //Murad :: Info : https://damienbod.com/2015/09/15/asp-net-5-action-filters/ 
       config.Filters.Add(new Filters.AppFilter()); 


      }); 
      /* 
      * Murad :: BUG Fixed 
      ).AddRazorOptions(options => 
      { 
       var previous = options.CompilationCallback; 
       options.CompilationCallback = context => 
       { 
        previous?.Invoke(context); 
        context.Compilation = context.Compilation.AddReferences(Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ADMA.EWRS.Data.Models.Murad).Assembly.Location)); 
       }; 
      }); 
      */ 

      //var myAssemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(x.Location)).ToList(); 
      //services.Configure((Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions options) => 
      //{ 
      // var previous = options.CompilationCallback; 
      // options.CompilationCallback = (context) => 
      // { 
      //  previous?.Invoke(context); 

      //  context.Compilation = context.Compilation.AddReferences(myAssemblies); 
      // }; 
      //}); 


      //Murad : Replace ASP.NET Core DI with better AutoFac 
      //services.AddScoped<IClaimsSecurityManager, ClaimsSecurityManager>(); 

      //Murad :: Add IoC i used AutoFac 
      //Check : http://docs.autofac.org/en/latest/integration/aspnetcore.html 

      // Add Autofac 

      // Create the container builder. 
      var containerBuilder = new Autofac.ContainerBuilder(); 
      containerBuilder.RegisterModule<IoC.DefaultModule>(); 

      //using Autofac.Extensions.DependencyInjection; it is extension method 
      containerBuilder.Populate(services); 

      //build the container 
      var container = containerBuilder.Build(); 

      // Return the IServiceProvider resolved from the container. 
      return container.Resolve<IServiceProvider>(); 
     } 

     // 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(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseBrowserLink(); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseStaticFiles(); 

      //Murad :: Replace in future with Cache Manager 
      app.UseSession(
       new SessionOptions() 
       { 
        CookieName = Cookies.SessionsCookieName, 
        IdleTimeout = TimeSpan.FromMinutes(60) 
       }); 

      //app.Map("/session", subApp => 
      //{ 
      // subApp.Run(async context => 
      // { 
      //  int visits = 0; 
      //  visits = context.Session.GetInt32("visits") ?? 0; 
      //  context.Session.SetInt32("visits", ++visits); 
      //  await context.Response.WriteAsync("Counting: You have visited our page this many times: " + visits); 
      // }); 
      //}); 

      //HttpContext.Session.SetString("Name", "Mike"); 

      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationScheme = Cookies.AuthenticationCookieName, 
       LoginPath = new PathString("/Account/Login/"), 
       AccessDeniedPath = new PathString("/Account/Forbidden/"), 
       AutomaticAuthenticate = true, 
       AutomaticChallenge = true, 
       SessionStore = new MemoryCacheTicketStore() 
      }); 

      //Murad: Build Custom ADMA Claim Provider - If Allah give us more time in those FA people 
      //app.UseClaimsTransformation(new ClaimsTransformationOptions 
      //{ 
      // Transformer = new ADMAClaimsTransformer() 
      //}); 

      app.UseMvc(routes => 
      { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 
} 
} 
+0

是否「Autofac.Extensions.DependencyInjection」:「4.0.0-rc3-280」支持RTM呢?如果不是,它可能會提取過時的依賴關係或取決於過時的依賴關係。查看公告:https://github.com/aspnet/Announcements/issues/187 – Tseng

+0

是的,它確實檢查了這一點:https://www.nuget.org/packages/Autofac.Extensions.DependencyInjection –

+3

任何從Google結束的人。我有這個問題,因爲我使用的是1.0.0,對於會話包中的大多數情況,然後是1.0.0-rc2-final。將其更改爲1.0.0修復了錯誤。可能版本不匹配。 – douggard

回答

0

這是一個版本的問題。檢查project.json文件。我已經給出了下面的代碼:

"Microsoft.ApplicationInsights.AspNetCore": "1.0.0", 
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0", 
"Microsoft.AspNetCore.Diagnostics": "1.0.0", 
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0", 
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0", 
"Microsoft.AspNetCore.Mvc": "1.0.1", 
相關問題