问题描述
在创建ASP.NET Core应用程序时,将为您设置一个名为ASPNETCORE_ENVIRONMENT=Development
的环境变量,在调试时,您会看到IHostingEnvironment
设置为Development
.
When creating an ASP.NET Core app an environment variable called ASPNETCORE_ENVIRONMENT=Development
will be set for you and when debugging you will see that the IHostingEnvironment
is set to Development
.
问题是,当我在为Service Fabric设置的解决方案中使用同一项目时,似乎没有注入环境变量,并且IHostingEnvironment
只是返回了生产".
The problem is that when I use the same project in a solution set up for Service Fabric the environment variables don't seem to get injected and IHostingEnvironment
just returns "Production".
我该如何解决?
注意:我已经在启动类中设置了一个断点,以观察IHostingEnvironment变量.
Note: I've set a breakpoint in the startup class to observe the IHostingEnvironment variable.
推荐答案
我在默认模板上遇到了同样的问题.以下内容与Duncan的答案类似,但有两个重要的区别:1)您不必在服务中更改任何模板代码,并且2)IHostingEnvironment
将被正确设置.
I ran into the same issue with the default template. The following is similar to Duncan's answer but with two important differences: 1) You will not have to change any template code within the service, and 2) the IHostingEnvironment
will be properly set.
首先,将ASPNETCORE_ENVIRONMENT
变量添加到应用程序服务的PackageRoot\ServiceManifest.xml
文件的<CodePackage>
元素中:
First, add the ASPNETCORE_ENVIRONMENT
variable to the <CodePackage>
element of the PackageRoot\ServiceManifest.xml
file of the application service:
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>MyService.exe</Program>
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</EntryPoint>
<EnvironmentVariables>
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value=""/>
</EnvironmentVariables>
</CodePackage>
在Duncan的回复中,您将对Service Fabric Application项目的ApplicationManifest.xml
进行两项更改.首先,设置一个参数(变量),以便根据部署项目的方式替换ApplicationParameters
文件时可以对其进行修改.然后,在ServiceManifestImport
元素中添加一个EnvironmentalOverrides
部分.两次相加的结果将如下所示:
As in Duncan's response, there are two changes you'll make to the ApplicationManifest.xml
of your Service Fabric Application project. First, setup a parameter (variable) so that it can be modified when the ApplicationParameters
files are substituted based on the way you deploy the project. Then, add an EnvironmentalOverrides
section to your ServiceManifestImport
element. The results of the two additions will look something like this:
<Parameters>
<Parameter Name="MyService_InstanceCount" DefaultValue="-1" />
<Parameter Name="AspNetCoreEnvironment" DefaultValue="" />
</Parameters>
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
<EnvironmentOverrides CodePackageRef="Code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[AspNetCoreEnvironment]" />
</EnvironmentOverrides>
</ServiceManifestImport>
最后,您可以在各个ApplicationParameters
文件中添加适当的值:
Finally, you can add in the proper values in the individual ApplicationParameters
files:
<Parameters>
<Parameter Name="MyService_InstanceCount" Value="-1" />
<Parameter Name="AspNetCoreEnvironment" Value="Development" />
</Parameters>
这时,您可以从服务的属性-调试"环境变量中删除该变量.
At this point, you can remove the variable from your service's Properties - Debug environmental variables.
这篇关于通过Service Fabric项目进行调试时未使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!