确定 ASP.Net Core webapp 是否正在本地主机上运行

Wil*_*tes 8 c# .net-core asp.net-core

在我的公司,开发人员的机器位于代理后面。我有一个场景,我需要提供我的凭据,并且我通过 HttpClient.DefaultProxy 来完成它。但是,在临时服务器和生产服务器上没有代理。我想做的是确定应用程序是否针对本地主机运行,并且仅在该情况下设置默认代理。过去我查看过 HttpRequest 来确定该信息。为此,我需要应用启动文件中的逻辑。

到目前为止,我想出的唯一比较干净的解决方案是检查附加的调试器。但这并不理想,因为我们还在本地运行这些应用程序而没有附加调试器。另一种选择是使用中间件,然后检查“localhost”的 url,但这似乎过多。

任何想法,将不胜感激!

public void ConfigureServices(IServiceCollection services)
{
   if (/*Some clean way to determine IsLocal*/)
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}
Run Code Online (Sandbox Code Playgroud)

为了澄清以防止问题删除 - 我想确定 httprequest 之前的 localhost 是否存在。


更新我选择使用 lauchSetting.json ,它也并不完美,但最适合我的情况。我将概述所有选项及其优缺点,供未来处于同一位置的开发人员使用。

选项 1:添加本地环境
问题:如果您已经有了特定于环境的配置,如果您想从“本地”配置文件中定位“暂存”,则需要花时间复制粘贴配置。
代码

public void ConfigureServices(IServiceCollection services)
{
   if (HostingEnvironment.IsEnvironment("Local"))
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}
Run Code Online (Sandbox Code Playgroud)


选项 2:创建一个中间件来检查请求 url 中的 localhost
问题:根据其实现方式,它可能运行得太频繁。如果您的本地版本未在 url 中使用 localhost,它也不起作用。

选项 3:使用 launchSettings.json 来容纳环境变量
问题:根据您的 IDE,这可能不起作用。它假设您提交 launchSettings.json 文件。
代码

{
   "profiles": {
      "IIS Express": {
         "commandName": "IISExpress",
         "launchBrowser": true,
         "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "IsLocal": "True"
         }
      }
   }
}

Run Code Online (Sandbox Code Playgroud)
public void ConfigureServices(IServiceCollection services)
{
   if (Environment.GetEnvironmentVariable("IsLocal") == "True")
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}
Run Code Online (Sandbox Code Playgroud)

选项 3.5:使用实际环境变量
问题:除了检查代码和运行之外,还需要额外的开发人员设置

选项 4:使用计算机名称来确定是开发人员还是服务器
问题:假设所有开发人员计算机都使用某种标准。
代码

public void ConfigureServices(IServiceCollection services)
{
   if (Environment.MachineName.StartsWith("DEV", StringComparison.OrdinalIgnoreCase))
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}
Run Code Online (Sandbox Code Playgroud)

选项 5:检查附加的调试器
问题:当您在本地运行代码时,调试器并不总是附加的

public void ConfigureServices(IServiceCollection services)
{
   if (Debugger.IsAttached)
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}
Run Code Online (Sandbox Code Playgroud)

选项 6:使用编译时调试检查
问题:可能不希望在每个场景中都在调试中进行编译
代码

#if(DEBUG)
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
#endif
Run Code Online (Sandbox Code Playgroud)

小智 0

我会使用选项 4的一种变体,其中包含一个小的单行白名单

if("|PC1|PC2|PC3|".Contains("|" + Environment.MachineName + "|"))
{
    // proxy code here
}
Run Code Online (Sandbox Code Playgroud)

...然后就完成了。

我认为这将是一个很好的务实解决方案。

请记住:完美是优秀的敌人。

我不知道上下文,但万一您是在代码库的这个角落工作的主要开发人员,这将是一个非常好的解决方案,您只需通知您的同事是否/何时需要调试,他需要添加他的机器名称到列表中。