1

I have an ASP.NET WebApi application running in IIS. From a non-Request background worker thread I need to find out the base Uri of the application (e.g., https://machineName:port/applicationName/).

The usual suggestions do not work since the Request object is null in the case of a worker thread that was created in the Application_Start() method.

Update: I am adding the port to the environment-specific web.config file which gets merged at deployment time (as suggested by @Kenneth).

asked Jan 13, 2016 at 21:49

2 Answers 2

3

You can use the HostingEnvironment.ApplicationVirtualPath property.

It's located in the System.Web.Hosting-namespace of the System.Web assembly

This is the MSDN reference: https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment(v=vs.110).aspx

answered Jan 13, 2016 at 21:52

2 Comments

Unfortunately, that only provides /applicationName/ but not the port number (in case the application is using a non-default port). Is there a way to get the port number separately to reconstruct the entire application Uri?
afaik that's not possible. There are a few workarounds, which require you to wait until the first request has come in and then capture the info: mvolo.com/… However, you should probably rethink why you need this. I'd say you can probably keep that info inside your web.config and change it (preferably automated) when you deploy
0

I would suggest the same solution as @Kenneth.

Otherwise, I guess you could grab the value on App Start and capture it in memory for later usage.

answered Jan 14, 2016 at 10:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.