zoukankan      html  css  js  c++  java
  • C# VS2017的.net Core1.0项目在版本升级为2.0后找不到程序集的处理办法

    最近不小心升级了VS2017,然后原来的.net web core1.0的项目是引用了DataBaseLib的程序集,如图  ,升级之后安装了2.0的框架,发现项目就报错了,,这个是还是之后报的错误,第一个错误是Unable to find the library 'DataBase.dll',也就是找不到所引用的程序集的位置。借助同事以及stackoverflow等问答网站,整理的解决办法如下:

    修改startup.cs中的代码文件:

    原来的代码:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Builder;
     6 using Microsoft.AspNetCore.Hosting;
     7 using Microsoft.Extensions.Configuration;
     8 using Microsoft.Extensions.DependencyInjection;
     9 using Microsoft.Extensions.Logging;
    10 
    11 namespace OneStop
    12 {
    13     public class Startup
    14     {
    15         public Startup(IHostingEnvironment env)
    16         {
    17             var builder = new ConfigurationBuilder()
    18                 .SetBasePath(env.ContentRootPath)
    19                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    20                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    21                 .AddEnvironmentVariables();
    22             Configuration = builder.Build();
    23         }
    24 
    25         public IConfigurationRoot Configuration { get; }
    26 
    27         // This method gets called by the runtime. Use this method to add services to the container.
    28         public void ConfigureServices(IServiceCollection services)
    29         {
    30             // Add framework services.
    31             services.AddMvc();
    32         }
    33 
    34         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    35         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    36         {
    37             loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    38             loggerFactory.AddDebug();
    39 
    40             if (env.IsDevelopment())
    41             {
    42                 app.UseDeveloperExceptionPage();
    43                 app.UseBrowserLink();
    44             }
    45             else
    46             {
    47                 app.UseExceptionHandler("/Home/Error");
    48             }
    49 
    50             app.UseStaticFiles();
    51 
    52             app.UseMvc(routes =>
    53             {
    54                 routes.MapRoute(
    55                     name: "default",
    56                     template: "{controller=Home}/{action=Index}/{id?}");
    57             });
    58         }
    59     }
    60 }
    View Code

    处理之后的代码:

      1 using DataBaseLib;
      2 using Microsoft.AspNetCore.Builder;
      3 using Microsoft.AspNetCore.Hosting;
      4 using Microsoft.AspNetCore.Http;
      5 using Microsoft.AspNetCore.Http.Features;
      6 using Microsoft.AspNetCore.Mvc;
      7 using Microsoft.DotNet.PlatformAbstractions;
      8 using Microsoft.Extensions.Configuration;
      9 using Microsoft.Extensions.DependencyInjection;
     10 using Microsoft.Extensions.DependencyModel;
     11 using Microsoft.Extensions.DependencyModel.Resolution;
     12 using Microsoft.Extensions.Logging;
     13 using Newtonsoft.Json;
     14 using Newtonsoft.Json.Serialization;
     15 using System;
     16 using System.Collections.Generic;
     17 using System.IO;
     18 using System.Reflection;
     19 
     20 namespace Assemble
     21 {
     22     public class Startup
     23     {
     24         public Startup(IHostingEnvironment env)
     25         {
     26             var builder = new ConfigurationBuilder()
     27                 .SetBasePath(env.ContentRootPath)
     28                 .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
     29                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
     30                 .AddEnvironmentVariables();
     31             Configuration = builder.Build();
     32         }
     33 
     34         public IConfigurationRoot Configuration { get; }
     35 
     36         // This method gets called by the runtime. Use this method to add services to the container.
     37         public void ConfigureServices(IServiceCollection services)
     38         {
     39 
     40 
     41             Initialize.ConfigureServices(services);
     42             // Add framework services.
     43 
     44             var mvcBuilder = services.AddMvc();
     45             new MvcConfiguration().ConfigureMvc(mvcBuilder);
     46 
     47         }
     48 
     49         public class MvcConfiguration : IDesignTimeMvcBuilderConfiguration
     50         {
     51             private class DirectReferenceAssemblyResolver : ICompilationAssemblyResolver
     52             {
     53                 public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
     54                 {
     55                     if (!string.Equals(library.Type, "reference", StringComparison.OrdinalIgnoreCase))
     56                     {
     57                         return false;
     58                     }
     59 
     60                     var paths = new List<string>();
     61 
     62                     foreach (var assembly in library.Assemblies)
     63                     {
     64                         var path = Path.Combine(ApplicationEnvironment.ApplicationBasePath, assembly);
     65 
     66                         if (!File.Exists(path))
     67                         {
     68                             return false;
     69                         }
     70 
     71                         paths.Add(path);
     72                     }
     73 
     74                     assemblies.AddRange(paths);
     75 
     76                     return true;
     77                 }
     78             }
     79 
     80             public void ConfigureMvc(IMvcBuilder builder)
     81             {
     82                 // .NET Core SDK v1 does not pick up reference assemblies so
     83                 // they have to be added for Razor manually. Resolved for
     84                 // SDK v2 by https://github.com/dotnet/sdk/pull/876 OR SO WE THOUGHT
     85                 /*builder.AddRazorOptions(razor =>
     86                 {
     87                 razor.AdditionalCompilationReferences.Add(
     88                 MetadataReference.CreateFromFile(
     89                 typeof(PdfHttpHandler).Assembly.Location));
     90                 });*/
     91 
     92                 // .NET Core SDK v2 does not resolve reference assemblies‘ paths
     93                 // at all, so we have to hack around with reflection
     94                 typeof(CompilationLibrary)
     95                 .GetTypeInfo()
     96                 .GetDeclaredField("<DefaultResolver>k__BackingField")
     97                 .SetValue(null, new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
     98                 {
     99                     new DirectReferenceAssemblyResolver(),
    100                     new AppBaseCompilationAssemblyResolver(),
    101                     new ReferenceAssemblyPathResolver(),
    102                     new PackageCompilationAssemblyResolver(),
    103                 }));
    104             }
    105         }
    106 
    107         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    108         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    109         {
    110             loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    111             loggerFactory.AddDebug();
    112 
    113             if (env.IsDevelopment())
    114             {
    115                 app.UseDeveloperExceptionPage();
    116                 app.UseBrowserLink();
    117             }
    118             else
    119             {
    120                 app.UseExceptionHandler("/Home/Error");
    121             }
    122 
    123             app.UseStaticFiles();
    124 
    125             app.UseMvc(routes =>
    126             {
    127                 routes.MapRoute(
    128                     name: "default",
    129                     template: "{controller=Solution}/{action=Index}/{id?}");
    130             });
    131         }
    132     }
    133 
    134 }
    View Code

    希望能对大家有所帮助!

  • 相关阅读:
    应用程序池溢出问题
    弹窗上传图片
    第三方监测
    服务器架设方案
    python随笔录入月份的值,输出对应的季节
    用python计算直角三角形斜边长
    返回(统计)一个列表中出现次数最多的元素
    使用random函数实现randint函数的功能
    Spring
    ng build prod basehref /javaweb/angular/
  • 原文地址:https://www.cnblogs.com/xinjianheyi/p/8509294.html
Copyright © 2011-2022 走看看