1

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();
   }
}
ohmantics
  • 1,799
  • 14
  • 16
TheNIK
  • 67
  • 1
  • 8

4 Answers4

1

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...
}
BertuPG
  • 653
  • 4
  • 6
0

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");
wsanville
  • 37,158
  • 8
  • 76
  • 101
  • it is ok, but I have to run the same code under windows service also.. so question exists: how to determine that code was called from iis ? – TheNIK Feb 08 '11 at 17:49
  • Check if `HttpContext.Current` is `null`, if so use your existing code. If not, run this. – wsanville Feb 08 '11 at 17:54
0

You could try and get the location from the type. For example:

string strPath = 
   Path.Combine(
      Path.GetDirectoryName(typeof(LanguageSettings).Assembly.Location),
      "Language.config");
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • it is possible , but the same code has to work under IIS and windows form and windows service, how to determine whe code works under iis? – TheNIK Feb 08 '11 at 17:47
0

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)

Community
  • 1
  • 1