我有一個第三方庫,由我的Core項目中引用的兩個程序集組成。 其中一個程序集是從我的Core項目中的代碼靜態引用的,另一個僅在web項目的web.config中使用。核心項目反過來從Web項目引用。在構建中缺少程序集
問題是,當我啓動web應用程序時,應用程序崩潰,因爲第二個程序集丟失後無法分析web.config。我猜編譯器不能理解Core項目應該包含第二個程序集,因爲它通過web.config鬆散地鏈接在一起。
我不想將這些程序集添加到Web項目中,因爲這將是緊密的耦合,它不是依賴於程序集的Web項目,而是核心項目。
我這樣做解決了它(在使用第三方組件的核心項目類):
static MyClass()
{
var dummy = typeof(ThirdPartyClass);
if (dummy == null)
throw new Exception("Do not remove reference to 'ThirdPartyClass'");
}
它的一個黑客位,但好處是,它僅影響核心項目這是唯一應該瞭解這個第三方lib的項目。 throw new Exception
是爲其他開發者提供的,以便他們明白他們不能刪除它,並且還會處理未使用的警告。
我的問題是,這是一個很好的有效解決方案,還是有更好的?
編輯:做了一個小的輔助類
public static class Util
{
public static void EnsureStaticReference<T>()
{
var dummy = typeof(T);
if(dummy == null)
throw new Exception(string.Format("This code is used to ensure that the compiler will include assembly"));
}
}
postbuildevents? – giammin
這是連接。 [Status:Closed as Will not Fix](http://connect.microsoft.com/VisualStudio/feedback/details/652785/visual-studio-does-not-copy-referenced-assemblies-through-the-reference-hierarchy ),解決方法:後期構建步驟。 – CodeCaster
如果構建事件意味着用魔法字符串編寫複製代碼,我的上述解決方案必須考慮更清潔嗎? – Anders