简体   繁体   中英

IIS 7 how to load assembly from Bin folder

In .NET solutions I use custom classes for translations. 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. But it does not work when I call it from web service...

I debug web service via Visual Studio 2010 and via bult-in debugger. And I see that buit-in ASP.NET Developpment loades assemply from 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.

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:

<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. 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).

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 :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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