How to get host URL in the background service in asp .net core web api?
Asked Answered
H

1

6

I'm having an Asp .Net core web api which will run a background service to schedule some task daily and will send some mail. In the mail I need to have the url of the api. Is there any way to get the host URL in the background services other than controllers.

Hensley answered 8/7, 2019 at 9:58 Comment(2)
Why do you need that URL? The background service itself doesn't need that ULR to call the Web API service, it is part of the service. You can't use the service's URL in an email either, as it's almost certainly not same as the public URL exposed by the firewall, load balancers or IIS. There may be many different host names/URLs configured for the same service too.Metts
@PanagiotisKanavos I want the URL hosted by the web api where the background service is runningHensley
I
7

Background services run outside the scope of a web request, hence background service. This means that there is no HTTP context available where you can grab the host of the application from. I suggest you create an application setting where you configure the host for the current environment, e.g. appsettings.Staging.json, appsettings.Production.json, etc and use that in your background service by injecting IConfiguration or a specific options class.

Isomeric answered 8/7, 2019 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.