[英]IIS 7 how to load assembly from Bin folder
在.NET解決方案中,我使用自定義類進行翻譯。 翻譯框架的主要思想是將帶有翻譯的文件放置在靠近程序集的文件夾中。
從Windows窗體應用程序調用時,一切正常。 但是當我從Web服務中調用它時它不起作用...
我通過Visual Studio 2010和內置調試器調試Web服務。 而且我看到內置的ASP.NET開發會從C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ Temporary ASP.NET Files \\
也無法找到帶有翻譯的文件夾...
因此,建議在這種情況下該怎么辦?
我在IIS7下測試過,它也不起作用。
示例代碼如何加載程序集:
if (languageSettings == null)
{
TraceIt(assembly.Location);
string strPath = Path.Combine(Path.GetDirectoryName(assembly.Location), "Language.config");
TraceIt(strPath);
languageSettings = new LanguageSettings(strPath);
if (!languageSettings.LoadSettings())
{
languageSettings.CurrentLanguage = DefaultLocale;
languageSettings.SaveSettings();
}
}
在Web環境中,通常在web.config中使用您的語言數據文件夾的絕對路徑設置密鑰,而不是依賴執行文件夾中的查找:
<appSettings>
<add key="Languages" value="D:\Data\Languages" />
</appSettings>
並且,在代碼中
stirng path = ConfigurationManager.AppSettings["Languages"]
if (!String.IsNullOrEmplty(path))
{
string file = Path.Combine(path, filename);
// and so on...
}
在Web應用程序中,程序集將位於bin
文件夾中。 假設配置文件是上一級的,則在應用程序的根目錄下,您可以使用Server.MapPath
來獲取其路徑,就像這樣(您需要引用System.Web)。
string strPath = HttpContext.Current.Server.MapPath("~/Language.config");
您可以嘗試從類型中獲取位置。 例如:
string strPath =
Path.Combine(
Path.GetDirectoryName(typeof(LanguageSettings).Assembly.Location),
"Language.config");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.