"Cannot find compilation library location for package "enc.dll"" 发生.net core 依赖注入错误
"Cannot find compilation library location for package "enc.dll"" error occur .net core dependency injection
我正在使用 asp.net 核心 mvc 构建网站,并且为了登录我添加了对 enc.dll 文件的依赖性,它只是 encrypt/decrypt 用户信息。
我用 enc.dll 文件制作了一个 Seeder class,它有一个键 属性 和 en/decrypt 键。然后我将它添加到我的服务中以使用依赖注入功能。
services.AddSingleton<ISeeder, Seeder>();
虽然当我调用 seeder class 的 enc、dec 函数时它运行良好,但它不会 return 任何错误。下面是示例代码。
private readonly ISeeder seed;
public AccountController(ISeeder seed)
{
this.seed = seed;
}
[HttpGet]
public IActionResult test()
{
string s = seed.Enc("testEncode");
return Json(s);
}
因此当我 return 由种子实例创建的字符串 s 时有效。
但是当我尝试 return 视图而不使用种子实例并抛出错误时它不起作用,其中 Enc 是我正在使用的 dll 库。
InvalidOperationException: Cannot find compilation library location for package 'Enc'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
下面是我的播种器代码。
private Enc enc;
private readonly EncKey key;
public Seeder(IOptions<EncKey> options)
{
enc = new Enc();
key = options.Value;
}
public string Dec(string toDec)
{
return enc.Dec(toDec, key.EncryptKey);
}
public string Enc(string toEnc)
{
return enc.Enc(toEnc, key.EncryptKey);
}
有人能帮忙吗?我正在开发 .net core 2.0 环境
更新
This issue was fixed in 2.0.3, for apply need to update VS(or manualy
dotnet SDK and Runtime) and project packages via nuget(in particular
Microsoft.AspNetCore.All to 2.0.3)
这是 .Net Core 2.0 的已知问题 https://github.com/dotnet/core-setup/issues/2981
Razor 视图预编译无法解析 lib 路径
这里的解决方法可以解决这个问题:
添加这个(它修复了发布发布错误)
using Microsoft.AspNetCore.Mvc;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace somenamespace
{
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(),
}));
}
}
}
和这个(修复编译错误)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.DependencyModel;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
namespace somenamespace
{
public class ReferencesMetadataReferenceFeatureProvider : IApplicationFeatureProvider<MetadataReferenceFeature>
{
public void PopulateFeature(IEnumerable<ApplicationPart> parts, MetadataReferenceFeature feature)
{
var libraryPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var assemblyPart in parts.OfType<AssemblyPart>())
{
var dependencyContext = DependencyContext.Load(assemblyPart.Assembly);
if (dependencyContext != null)
{
foreach (var library in dependencyContext.CompileLibraries)
{
if (string.Equals("reference", library.Type, StringComparison.OrdinalIgnoreCase))
{
foreach (var libraryAssembly in library.Assemblies)
{
libraryPaths.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, libraryAssembly));
}
}
else
{
foreach (var path in library.ResolveReferencePaths())
{
libraryPaths.Add(path);
}
}
}
}
else
{
libraryPaths.Add(assemblyPart.Assembly.Location);
}
}
foreach (var path in libraryPaths)
{
feature.MetadataReferences.Add(CreateMetadataReference(path));
}
}
private static MetadataReference CreateMetadataReference(string path)
{
using (var stream = File.OpenRead(path))
{
var moduleMetadata = ModuleMetadata.CreateFromStream(stream, PEStreamOptions.PrefetchMetadata);
var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);
return assemblyMetadata.GetReference(filePath: path);
}
}
}
}
也将 addMVC 改成这个
//workaround https://github.com/dotnet/core-setup/issues/2981 will be fixed in 2.0.1
services.AddMvc().ConfigureApplicationPartManager(manager =>
{
var oldMetadataReferenceFeatureProvider = manager.FeatureProviders.First(f => f is MetadataReferenceFeatureProvider);
manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
manager.FeatureProviders.Add(new ReferencesMetadataReferenceFeatureProvider());
});
您将能够在您的视图中使用 dll
还有第二种方法是在此处禁用剃须刀预编译示例
我正在使用 asp.net 核心 mvc 构建网站,并且为了登录我添加了对 enc.dll 文件的依赖性,它只是 encrypt/decrypt 用户信息。 我用 enc.dll 文件制作了一个 Seeder class,它有一个键 属性 和 en/decrypt 键。然后我将它添加到我的服务中以使用依赖注入功能。
services.AddSingleton<ISeeder, Seeder>();
虽然当我调用 seeder class 的 enc、dec 函数时它运行良好,但它不会 return 任何错误。下面是示例代码。
private readonly ISeeder seed;
public AccountController(ISeeder seed)
{
this.seed = seed;
}
[HttpGet]
public IActionResult test()
{
string s = seed.Enc("testEncode");
return Json(s);
}
因此当我 return 由种子实例创建的字符串 s 时有效。
但是当我尝试 return 视图而不使用种子实例并抛出错误时它不起作用,其中 Enc 是我正在使用的 dll 库。
InvalidOperationException: Cannot find compilation library location for package 'Enc'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
下面是我的播种器代码。
private Enc enc;
private readonly EncKey key;
public Seeder(IOptions<EncKey> options)
{
enc = new Enc();
key = options.Value;
}
public string Dec(string toDec)
{
return enc.Dec(toDec, key.EncryptKey);
}
public string Enc(string toEnc)
{
return enc.Enc(toEnc, key.EncryptKey);
}
有人能帮忙吗?我正在开发 .net core 2.0 环境
更新
This issue was fixed in 2.0.3, for apply need to update VS(or manualy dotnet SDK and Runtime) and project packages via nuget(in particular Microsoft.AspNetCore.All to 2.0.3)
这是 .Net Core 2.0 的已知问题 https://github.com/dotnet/core-setup/issues/2981
Razor 视图预编译无法解析 lib 路径
这里的解决方法可以解决这个问题:
添加这个(它修复了发布发布错误)
using Microsoft.AspNetCore.Mvc;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace somenamespace
{
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(),
}));
}
}
}
和这个(修复编译错误)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.DependencyModel;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
namespace somenamespace
{
public class ReferencesMetadataReferenceFeatureProvider : IApplicationFeatureProvider<MetadataReferenceFeature>
{
public void PopulateFeature(IEnumerable<ApplicationPart> parts, MetadataReferenceFeature feature)
{
var libraryPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var assemblyPart in parts.OfType<AssemblyPart>())
{
var dependencyContext = DependencyContext.Load(assemblyPart.Assembly);
if (dependencyContext != null)
{
foreach (var library in dependencyContext.CompileLibraries)
{
if (string.Equals("reference", library.Type, StringComparison.OrdinalIgnoreCase))
{
foreach (var libraryAssembly in library.Assemblies)
{
libraryPaths.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, libraryAssembly));
}
}
else
{
foreach (var path in library.ResolveReferencePaths())
{
libraryPaths.Add(path);
}
}
}
}
else
{
libraryPaths.Add(assemblyPart.Assembly.Location);
}
}
foreach (var path in libraryPaths)
{
feature.MetadataReferences.Add(CreateMetadataReference(path));
}
}
private static MetadataReference CreateMetadataReference(string path)
{
using (var stream = File.OpenRead(path))
{
var moduleMetadata = ModuleMetadata.CreateFromStream(stream, PEStreamOptions.PrefetchMetadata);
var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);
return assemblyMetadata.GetReference(filePath: path);
}
}
}
}
也将 addMVC 改成这个
//workaround https://github.com/dotnet/core-setup/issues/2981 will be fixed in 2.0.1
services.AddMvc().ConfigureApplicationPartManager(manager =>
{
var oldMetadataReferenceFeatureProvider = manager.FeatureProviders.First(f => f is MetadataReferenceFeatureProvider);
manager.FeatureProviders.Remove(oldMetadataReferenceFeatureProvider);
manager.FeatureProviders.Add(new ReferencesMetadataReferenceFeatureProvider());
});
您将能够在您的视图中使用 dll
还有第二种方法是在此处禁用剃须刀预编译示例