简体   繁体   English

是否有可能从来自webjob的webapp获取环境变量

[英]Is it possible to get environment variable from webapp coming from webjob

I have a problem with reading environment variables. 我在读取环境变量时遇到问题。 I have 2 projects under same solution. 我在相同的解决方案下有2个项目。 Basic i need to be able to read my environment Variables when i come from my webjob. 基本我需要从网络工作中读取环境变量。

The two project are a asp.net-core-mvc project and a .net core console project. 这两个项目是asp.net-core-mvc项目和.net核心控制台项目。

My console project is a "Webjob" with a reference to my web app project. 我的控制台项目是“ Webjob”,引用了我的Web应用程序项目。

When my "Webjob" is running, it hits a method in my web app. 当我的“ Webjob”运行时,它在我的Web应用程序中命中了一个方法。 Then i tries to get the environment variables from my web app. 然后,我尝试从Web应用程序获取环境变量。 Which returns null, becouse it is not set. 返回空值,因为未设置。

Is there any way to run it so i can get the environment varaible from my web app when i come from my webjob? 有什么方法可以运行它,以便当我来自Webjob时,我可以从Web应用程序获取环境变量吗?

the code i run to get the variables when my web app is running alone is 我的Web应用程序单独运行时我运行以获取变量的代码是

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

When you say "it hits a method in my web app", I assume you mean you run a method in your WebApp project, and not call a WebAPI endpoint. 当您说“它在我的Web应用程序中命中某个方法”时,我假设您的意思是您在WebApp项目中运行一个方法,而不是调用WebAPI端点。

The thing is that the debug configuration of VS is applicable only to the project running the actual code, just like the web.config or app.config can be found in that particular project's bin/debug folder. 事实是,VS的调试配置仅适用于运行实际代码的项目,就像可以在特定项目的bin / debug文件夹中找到web.config或app.config一样。 This means you would need to set the environment variables on the WebJob project as well when running that project. 这意味着在运行该项目时,还需要在WebJob项目上设置环境变量。

However: when deploying to Azure for example, you will typically host both projects in the same App Service, which share configuration, and so can read from the same values. 但是:例如,当部署到Azure时,通常会将两个项目托管在同一App Service中,它们共享配置,因此可以从相同的值读取。

You cannot, nor should you, reference an ASP.NET Core project from another project and directly hit actions as if they were methods. 您不能也不应引用另一个项目中的ASP.NET Core项目并直接将操作视为方法。 You need to start the ASP.NET Core project and have it running, and then make requests to it from your console app using HttpClient . 您需要启动ASP.NET Core项目并使其运行,然后使用HttpClient从控制台应用程序对其进行请求。 Otherwise, none of the stuff in Startup happens (because it's not running), and as a result, you have no access to services such as configuration. 否则,“ Startup中的所有内容均不会发生(因为它没有运行),因此,您无权访问配置等服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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