.NetCore2.0引用DLL报System.InvalidOperationException: Can not find compilation library location for pac...

.NET CORE 2.0 MVC项目引用类库出现:System.InvalidOperationException: Can not find compilation library location for package 'XXX'

临时解决如下(据说2.0.1会修复):
在Startup.cs文件中:
修改:
public void ConfigureServices(IServiceCollection services)
{
var mvcBuilder = services.AddMvc();
new MvcConfiguration().ConfigureMvc(mvcBuilder);
}
添加:
public class MvcConfiguration : IDesignTimeMvcBuilderConfiguration
{
private class DirectReferenceAssemblyResolver : ICompilationAssemblyResolver
{
public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
{
if (!string.Equals(library.Type, "reference", StringComparison.OrdinalIgnoreCase))
{
return false;
}

var paths = new List<string>();

foreach (var assembly in library.Assemblies)
{
var path = Path.Combine(ApplicationEnvironment.ApplicationBasePath, assembly);

if (!File.Exists(path))
{
return false;
}

paths.Add(path);
}

assemblies.AddRange(paths);

return true;
}
}

public void ConfigureMvc(IMvcBuilder builder)
{
// .NET Core SDK v1 does not pick up reference assemblies so
// they have to be added for Razor manually. Resolved for
// SDK v2 by https://github.com/dotnet/sdk/pull/876 OR SO WE THOUGHT
/*builder.AddRazorOptions(razor =>
{
razor.AdditionalCompilationReferences.Add(
MetadataReference.CreateFromFile(
typeof(PdfHttpHandler).Assembly.Location));
});*/

// .NET Core SDK v2 does not resolve reference assemblies' paths
// at all, so we have to hack around with reflection
typeof(CompilationLibrary)
.GetTypeInfo()
.GetDeclaredField("<DefaultResolver>k__BackingField")
.SetValue(null, new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
{
new DirectReferenceAssemblyResolver(),
new AppBaseCompilationAssemblyResolver(),
new ReferenceAssemblyPathResolver(),
new PackageCompilationAssemblyResolver(),
}));
}
}

.csproj中配置(如果有,确定是true,没有则不用管):
<PreserveCompilationContext>true</PreserveCompilationContext>

转载于:https://www.cnblogs.com/sun51586/p/7545046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值