简体   繁体   English

IIS 7如何从Bin文件夹加载程序集

[英]IIS 7 how to load assembly from Bin folder

In .NET solutions I use custom classes for translations. 在.NET解决方案中,我使用自定义类进行翻译。 Main idea of translation framework that files with translations are placed in folder near assembly. 翻译框架的主要思想是将带有翻译的文件放置在靠近程序集的文件夹中。

All work fine when it calles from windows forms application. 从Windows窗体应用程序调用时,一切正常。 But it does not work when I call it from web service... 但是当我从Web服务中调用它时它不起作用...

I debug web service via Visual Studio 2010 and via bult-in debugger. 我通过Visual Studio 2010和内置调试器调试Web服务。 And I see that buit-in ASP.NET Developpment loades assemply from C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\ 而且我看到内置的ASP.NET开发会从C:\\ Windows \\ Microsoft.NET \\ Framework \\ v4.0.30319 \\ Temporary ASP.NET Files \\

and there is no possibility to find my folder with translations... 也无法找到带有翻译的文件夹...

So suggest please what to do in this case? 因此,建议在这种情况下该怎么办?

I tested under IIS7 it does not work also. 我在IIS7下测试过,它也不起作用。

sample code how I load assembly: 示例代码如何加载程序集:

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();
   }
}

In web environment, its usual to setup a key in web.config with the absolute path to your language data folder, instead of rely on lookups in execution folder: 在Web环境中,通常在web.config中使用您的语言数据文件夹的绝对路径设置密钥,而不是依赖执行文件夹中的查找:

<appSettings>
  <add key="Languages" value="D:\Data\Languages" />
</appSettings>

and, in code 并且,在代码中

stirng path = ConfigurationManager.AppSettings["Languages"]
if (!String.IsNullOrEmplty(path))
{
    string file = Path.Combine(path, filename);
    // and so on...
}

In a web application, your assemblies will be located in a bin folder. 在Web应用程序中,程序集将位于bin文件夹中。 Assuming your config file is one level up, at the root of your application, you can get its path using Server.MapPath , like this (You'll need to reference System.Web). 假设配置文件是上一级的,则在应用程序的根目录下,您可以使用Server.MapPath来获取其路径,就像这样(您需要引用System.Web)。

string strPath = HttpContext.Current.Server.MapPath("~/Language.config");

You could try and get the location from the type. 您可以尝试从类型中获取位置。 For example: 例如:

string strPath = 
   Path.Combine(
      Path.GetDirectoryName(typeof(LanguageSettings).Assembly.Location),
      "Language.config");

Take a look at the solution given by John Sibly : 看一看John Sible提供的解决方案:

How do I get the path of the assembly the code is in? 如何获取代码所在的程序集的路径?

i think this is more what you are looking for ;) as it work in both cases (win and web) 我认为这是您所寻找的更多;),因为它在两种情况下(win和web)都有效

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM