C# · 12月 29, 2021

c# – 如何使XML文件成为vNext(ASP.NET 5)类库中的嵌入式资源?

我有一个MVC 6(vNext / ASP.NET 5)项目,一个类库用于DAL(数据访问层).现在我得到一个例外,因为NHibernate找不到我想要坚持的实体的映射文件.我已经看到严格的说明,将这个 XML映射文件标记为嵌入式资源,而不是复制到输出,但是在我为这个文件打开的三个属性页中没有一个,是否有任何规定.

我只想转向基于代码的流畅的映射,但这个问题并不是我的一个NHibernate映射文件所独有的.通过在解决方案资源管理器中右键单击的项目项目的旧属性页就可以了.我希望如果这样一个嵌入式资源的东西依然存在,那么就像project.json那样,我们必须指定这个.

解决方法@H_404_7@ UPDATE

我以前的答案不再有效(自RC2起),资源现在被标记为已弃用. (谢谢@Yossarian)

现在正确的方法是使用buildOptions / embed:

…”buildOptions”: { “emitEntryPoint”: true,”embed”: [ “9NLiZmx.png” ]},…

您必须使用project.json中的部分资源,就像这样

{ “compile”: “*.cs”,”resource”: [ “mapping.xml” ]}

By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.

Most sections of the project.json file that deal with files allow glob patterns,which are often called wildcards.

包含/排除属性列表

name default value===============================================compile compileExclude content **/* contentExclude preprocess compiler/preprocess/**/*.cs preprocessExclude resource compiler/preprocess/resources/**/* resourceExclude shared compiler/shared/**/*.cs sharedExclude publishExclude bin/**;obj/**;**/.*/** exclude

更多信息:http://docs.asp.net/en/latest/dnx/projects.html#including-excluding-files

您可以看到以下示例:

Program.cs中

using System;using System.Reflection;namespace ConsoleApp1{ public class Program { public static void Main(string[] args) { var assemblyName = new AssemblyName(“ConsoleApp1”); var resources = string.Join(Environment.NewLine,Assembly.Load(assemblyName).GetManifestResourceNames()); Console.WriteLine(“List of Manifest Resource Names”); Console.WriteLine(“======================”); Console.WriteLine(resources); } }}

project.json

{ “version”: “1.0.0-*”,”description”: “ConsoleApp1 Console Application”,”authors”: [ “Alberto Monteiro” ],”tags”: [ “” ],”projectUrl”: “”,”licenseUrl”: “”,”compilationOptions”: { “emitEntryPoint”: true },”resource”: “9NLiZmx.png”,”dependencies”: { },”commands”: { “ConsoleApp1”: “ConsoleApp1″ },”frameworks”: { “dnx451″: { },”dnxcore50”: { “dependencies”: { “Microsoft.CSharp”: “4.0.1-beta-23516″,”System.Collections”: “4.0.11-beta-23516″,”System.Console”: “4.0.0-beta-23516″,”System.Linq”: “4.0.1-beta-23516″,”System.Threading”: “4.0.11-beta-23516″,”System.IO”: “4.0.11-beta-23516″,”System.IO.FileSystem”: “4.0.1-beta-23225″,”System.Reflection”: “4.1.0-beta-23516” } } }}

产量

List of Manifest Resource Names======================ConsoleApp1.9NLiZmx.png