简体   繁体   English

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

[英]Determine if ASP.Net Core webapp is running on localhost

At my company developer machines are behind a proxy.在我公司,开发人员的机器在代理后面。 I have a scenario where I need to provide my credentials for it and I am accomplishing it via the HttpClient.DefaultProxy.我有一个场景,我需要为它提供我的凭据,我正在通过 HttpClient.DefaultProxy 完成它。 However, on Staging and Production servers there is no proxy.但是,在登台和生产服务器上没有代理。 What I would like to do is determine if the application is running against local host and only set the default proxy in that scenario.我想要做的是确定应用程序是否针对本地主机运行,并仅在该场景中设置默认代理。 In the past I've looked at the HttpRequest to determine that information.过去,我查看了 HttpRequest 以确定该信息。 For this I need to apply the logic in the Startup file.为此,我需要在启动文件中应用逻辑。

So far the only solution I've come up with that is somewhat clean is checking for an attached debugger.到目前为止,我想出的唯一有点干净的解决方案是检查附加的调试器。 That isn't ideal though since we also run these applications locally without the debugger attached.但这并不理想,因为我们也在本地运行这些应用程序而没有附加调试器。 Another option would be to use a middleware then check the url for "localhost" but that seems excessive.另一种选择是使用中间件,然后检查“localhost”的 url,但这似乎过多。

Any ideas would be appreciated!任何想法,将不胜感激!

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);
   }
}

To clarify to prevent question removal - I would like to determine if localhost before an httprequest exists.为了澄清以防止问题删除 - 我想在 httprequest 存在之前确定 localhost 是否存在。


Update I've chosen to go with using lauchSetting.json which also isn't perfect but fits the best for my case.更新我选择使用 lauchSetting.json,它也不完美,但最适合我的情况。 I'll go a head and outline all the options and their pros/cons for any future dev in the same position.对于未来处于同一位置的任何开发人员,我将一一概述所有选项及其优缺点。

Option 1: Add Local Environment选项 1:添加本地环境
Problems: If you have Environment specific configs already you'll spend time copy pasting configuration if you want to target "Staging" from your "Local" config file.问题:如果您已经有特定于环境的配置,如果您想从“本地”配置文件中定位“暂存”,您将花费时间复制粘贴配置。
Code代码

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);
   }
}


Option 2: Create a Middleware to check for localhost in request url选项 2:创建一个中间件来检查请求 url 中的 localhost
Problems: Depending on how its implemented it might be run too often.问题:根据它的实现方式,它可能会运行得太频繁。 It also doesn't work in the event you're local version isn't using localhost in the url.如果您的本地版本未在 url 中使用 localhost,它也不起作用。

Option 3: Use launchSettings.json to house an environmentVariable选项 3:使用 launchSettings.json 来容纳 environmentVariable
Problems: Depending on your IDE this may not work.问题:根据您的 IDE,这可能不起作用。 It assumes you commit your launchSettings.json file.它假定您提交了 launchSettings.json 文件。
Code代码

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

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);
   }
}

Option 3.5: Use actual environment variable选项 3.5:使用实际环境变量
Problems: Requires additional developer setup beyond checking out code and running问题:除了检查代码和运行之外,还需要额外的开发人员设置

Option 4: Use Machine Names to determine if developer or server选项 4:使用机器名称来确定是开发人员还是服务器
Problems: Assumes all developer machines are using some kind of standard.问题:假设所有开发机器都使用某种标准。
Code代码

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);
   }
}

Option 5: Check for an attached debugger选项 5:检查附加的调试器
Problems: Debugger isn't always attached when you're running locally问题:当您在本地运行时,并不总是附加调试器
Code代码

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);
   }
}

Option 6: Use compile time debug check选项 6:使用编译时调试检查
Problems: Might not want to compile in debug in every scenario问题:可能不想在每种情况下都在调试中编译
Code代码

#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

I would use a variant of your Option 4 with a small one-line whitelisting我会使用您的选项 4的变体和一个小的单行白名单

if("|PC1|PC2|PC3|".Contains("|" + Environment.MachineName + "|"))
{
    // proxy code here
}

... and then be done with it. ......然后完成它。

I think that would be a good pragmatic solution.我认为这将是一个很好的务实解决方案。

Remember: perfect is the enemy of good.记住:完美是好的敌人。

I don't know the context but in case maybe you are the primary developer working on this corner of the codebase this would be perfectly good solution and you just inform your colleague if/when he needs to debug, that he needs to add his machine name to the list.我不知道上下文,但如果您是在代码库的这个角落工作的主要开发人员,这将是一个非常好的解决方案,您只需通知您的同事是否/何时需要调试,他需要添加他的机器名称到列表中。

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

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