如何使XML文件成为vNext(ASP.NET 5)类库中的嵌入式资源? [英] How do I make an XML file an embedded resource in a vNext (ASP.NET 5) class library?

查看:82
本文介绍了如何使XML文件成为vNext(ASP.NET 5)类库中的嵌入式资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 6(vNext/ASP.NET 5)项目,其中一个类库用于DAL(数据访问层).现在,我遇到了一个异常,因为NHibernate找不到我要保留的实体的映射文件.我已经看到了严格的说明,将这个XML映射文件标记为嵌入式资源,并且不能复制到输出,但是在我设法为此文件打开的三个属性页面中,没有一个可以规定这个.

I have an MVC 6 (vNext/ASP.NET 5) project, with one class library for the DAL(Data Access Layer). Now I am getting an exception because NHibernate can't find the mapping file for an entity I am trying to persist. 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.

我只是要转向基于代码的流利的映射,但是这个问题并不是我的一个NHibernate映射文件所独有的.通过在解决方案资源管理器中右键单击可获得的项目项的旧属性页已不复存在.我希望,如果仍然存在诸如嵌入式资源之类的东西,那么我们必须指定它在其他地方,例如project.json.

I am simply going to move to code-based fluent mapping, but this issue isn't unique to my one NHibernate mapping file. 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.

推荐答案

更新

我以前的答案不再有效(因为RC2),resource现在被标记为已弃用. (感谢 @Yossarian )

My previous answer is no longer valid (since RC2), the resource is now marked as deprecated. (Thanks @Yossarian)

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

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


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

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

默认情况下,项目中包含目录中包含project.json的所有代码文件.您可以使用project.json的include/exclude部分控制此操作.

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.

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/zh-CN/latest/dnx/projects.html#include-exception-files

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

这篇关于如何使XML文件成为vNext(ASP.NET 5)类库中的嵌入式资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆