![](/img/trans.png)
[英]How do I inject ASP.NET 5 (vNext) user-secrets into my own utility class?
[英]How do I make an XML file an embedded resource in a vNext (ASP.NET 5) class library?
我有一個MVC 6(vNext / ASP.NET 5)項目,有一個用於DAL(數據訪問層)的類庫。 現在我得到一個例外,因為NHibernate找不到我試圖堅持的實體的映射文件。 我已經看到嚴格的指令將此XML映射文件標記為嵌入式資源而不是復制到輸出,但是在我設法為此文件打開的三個屬性頁中,沒有任何一個可以規定這一點。
我只是轉向基於代碼的流暢映射,但這個問題並不是我的一個NHibernate映射文件所特有的。 在解決方案資源管理器中右鍵單擊的項目項的舊屬性頁面就消失了。 我希望如果嵌入式資源這樣的東西仍然存在,那么它就像project.json
一樣,我們必須指定它。
UPDATE
我以前的答案不再有效(自RC2起), resource
現已標記為已棄用。 (謝謝@Yossarian )
正確的方法是使用buildOptions/embed
:
...
"buildOptions": {
"emitEntryPoint": true,
"embed": [ "9NLiZmx.png" ]
},
...
你必須使用project.json中的section 資源 ,就像這樣
{
"compile": "*.cs",
"resource": [
"mapping.xml"
]
}
默認情況下,包含project.json的目錄中的所有代碼文件都包含在項目中。 您可以使用project.json的include / exclude部分來控制它。
處理文件的project.json文件的大多數部分都允許使用通常稱為通配符的glob模式。
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
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);
}
}
}
{
"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
Alberto的答案不再有效(自RC2起), resource
現已標記為已棄用。
正確的方法是使用buildOptions/embed
:
...
"buildOptions": {
"emitEntryPoint": true,
"embed": [ "9NLiZmx.png" ]
},
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.