How to retrieve on which site the web application is currently running?

208 views Asked by At

I am currently running a WCF service on an AppFabric server and my application needs to load a the web.config file dynamically to retrieve custom configuration sections.

On my development machine I can just load the configuration like this:

WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath);

But on the test machine (AppFabric server) I am getting an exception and it seems that I need to specify a third parameter which is actually the site the web application is running on:

WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath, "MySite");

So I tried to hard code it and it worked. Anyway this is not acceptable, so I need to dynamically provide the site to the WebConfigurationManager because I do not on which site the service will be running in the future. Do anybody knows how to achieve that?

Thanks.

2

There are 2 answers

1
Shiraz Bhaiji On BEST ANSWER

If you are running this code as part of handling a request you could use:

Request.ServerVariables("server_name")

see: http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx

Edit based on your comment

The parameter that you need is the Site Name, not the machine name, your code be running on many machines. If the code is running somewhere where it no longer knows that it is on a web site, then it is difficult for it to get the name of the web site that it is running on.

You then have two options:

  • Send the name as a parameter from a layer that has httpconext
  • Not sure if this will work: but you could try adding a reference to system.web to your project. It may compile, but you could get a null reference exception when you run it. Probably worth a try.
0
Kamran Khan On

How about Server.MachineName