简体   繁体   English

如何将XML文件作为vNext(ASP.NET 5)类库中的嵌入式资源?

[英]How do I make an XML file an embedded resource in a vNext (ASP.NET 5) class library?

I have an MVC 6 (vNext/ASP.NET 5) project, with one class library for the DAL(Data Access Layer). 我有一个MVC 6(vNext / ASP.NET 5)项目,有一个用于DAL(数据访问层)的类库。 Now I am getting an exception because NHibernate can't find the mapping file for an entity I am trying to persist. 现在我得到一个例外,因为NHibernate找不到我试图坚持的实体的映射文件。 I have seen strict instructions to mark this XML mapping file as an embedded resource and not to copy to output, but in none of the three property pages I manage to open for this file, is there anywhere to stipulate this. 我已经看到严格的指令将此XML映射文件标记为嵌入式资源而不是复制到输出,但是在我设法为此文件打开的三个属性页中,没有任何一个可以规定这一点。

I am simply going to move to code-based fluent mapping, but this issue isn't unique to my one NHibernate mapping file. 我只是转向基于代码的流畅映射,但这个问题并不是我的一个NHibernate映射文件所特有的。 The old property page for project items, available by right click in Solution Explorer, is simply gone. 在解决方案资源管理器中右键单击的项目项的旧属性页面就消失了。 I am hoping that if such a thing as an embedded resource still exists, it is somewhere else, like project.json , that we must specify this. 我希望如果嵌入式资源这样的东西仍然存在,那么它就像project.json一样,我们必须指定它。

UPDATE UPDATE

My previous answer is no longer valid (since RC2), the resource is now marked as deprecated. 我以前的答案不再有效(自RC2起), resource现已标记为已弃用。 (Thanks @Yossarian ) (谢谢@Yossarian

Proper way how to do this is now to use buildOptions/embed : 正确的方法是使用buildOptions/embed

...
"buildOptions": {
  "emitEntryPoint": true,
  "embed": [ "9NLiZmx.png" ]
},
...

You must use the section resource in project.json, like this 你必须使用project.json中的section 资源 ,就像这样

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

By default all code files in a directory containing a project.json are included in the project. 默认情况下,包含project.json的目录中的所有代码文件都包含在项目中。 You can control this with the include/exclude sections of the project.json. 您可以使用project.json的include / exclude部分来控制它。

Most sections of the project.json file that deal with files allow glob patterns, which are often called wildcards. 处理文件的project.json文件的大多数部分都允许使用通常称为通配符的glob模式。

List of include/exclude properties 包含/排除属性列表

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              

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


You can see a sample below: 您可以在下面看到示例:

Program.cs 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 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"
      }
    }
  }
}

Output 产量

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

Alberto's answer is no longer valid (since RC2), the resource is now marked as deprecated. Alberto的答案不再有效(自RC2起), resource现已标记为已弃用。

Proper way how to do this is now to use buildOptions/embed : 正确的方法是使用buildOptions/embed

...
"buildOptions": {
  "emitEntryPoint": true,
  "embed": [ "9NLiZmx.png" ]
},
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ASP.NET 5(vNext)用户机密注入我自己的实用程序类? - How do I inject ASP.NET 5 (vNext) user-secrets into my own utility class? 如何创建一个在ASP.NET和非ASP.NET应用程序中工作的类库? - How do I make a class library that works in ASP.NET and non-ASP.NET applications? ASP.NET 5类库(vNext)中的编译器无法识别的类 - Class not recognized by compiler in ASP.NET 5 Class Library (vNext) 将常规类库转换为ASP.NET 5(vnext) - Convert regular class library to ASP.NET 5 (vnext) Asp.net将类库中的js编译为内嵌c#代码的嵌入式资源 - Asp.net compile js in class library as embedded resource with c# code inside 如何只发布一次文件? ASP.NET vNext - How to publish a file only once? ASP.NET vNext 如何根据ASP.NET VNEXT MVC6中给出的路径进行虚拟路由/重定向? - How can I do a virtual route/redirect based on the path given in ASP.NET VNEXT MVC6? 如何在 ASP.NET 5 MVC 6 (vNext) 中定义 Identity 的密码规则? - How do I define the password rules for Identity in ASP.NET 5 MVC 6 (vNext)? 是否可以直接在ASP.NET 5(vNext)的类库中访问强类型的配置设置? - Accessing strongly typed configuration settings directly into class library in ASP.NET 5 (vNext)? 无法在asp.net vnext类库中使用必需的属性 - Can't use required attribute in the asp.net vnext class library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM