问题描述
我正在使用asp.net核心mvc构建一个网站,并为登录添加了enc.dll文件的依赖关系,该文件只是对用户信息进行加密/解密.我使用enc.dll文件制作了Seeder类,该文件具有密钥属性,并使用密钥进行加密/解密.然后,将其添加到服务中以使用依赖项注入功能.
I am building a website using asp.net core mvc, and for the login i added dependency for enc.dll file, which just encrypt/decrypt user information.I made a Seeder class with enc.dll file, which has a key property and en/decrypt with the key. Then I added it to my service to use dependency injection feature.
services.AddSingleton<ISeeder, Seeder>();
虽然当我调用种子程序类的enc,dec函数时效果很好,但它不返回任何错误.下面是示例代码.
While it works well when i call enc, dec function of seeder class, it does not return any error. Below is the example code.
private readonly ISeeder seed;
public AccountController(ISeeder seed)
{
this.seed = seed;
}
[HttpGet]
public IActionResult test()
{
string s = seed.Enc("testEncode");
return Json(s);
}
因此,当我返回由种子实例创建的字符串s时,它可以工作.
So it works when i return string s that is created by seed instance.
但是当我尝试不使用种子实例并抛出错误而返回视图时,它不起作用,其中Enc是我正在使用的dll库.
but it is not working when i try to return a view without using seed instance and throwing an error, where Enc is the dll library i am using.
InvalidOperationException: Cannot find compilation library location for package 'Enc'
Microsoft.Extensions.DependencyModel.CompilationLibrary.ResolveReferencePaths(ICompilationAssemblyResolver resolver, List<string> assemblies)
下面是我的Seeder代码.
And below is my Seeder code.
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环境中工作
Could anyone help? I am working on .net core 2.0 environment
推荐答案
更新
.Net Core 2.0的已知问题 https://github.com/dotnet/core-setup/issues/2981
It's known issue of .Net Core 2.0 https://github.com/dotnet/core-setup/issues/2981
Razor视图预编译无法解析lib路径
Razor view precompilation can not resolve lib path
这里有解决方法来完成这项工作:
Here workaround to get this work:
添加此内容(这是修复版本发布错误)
add this (it's fixing release publish error)
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(),
}));
}
}
}
和这个(正在修复编译错误)
and this (it's fixing compilation errors)
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更改为此
also change addMVC to this
//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
and you will able to use dll in your view
您还有第二种方法是在此处禁用剃须刀预编译示例从ASP.Net Core 2 API中删除PrecompiledViews.dll
also you have second way is disable razor precompilation here exampleDeleting PrecompiledViews.dll from ASP.Net Core 2 API
这篇关于“找不到包"enc.dll"的编译库位置. .net核心依赖项注入发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!