launchsettings.json配置中包含了多个环境所致。以下是默认生成的配置,有两个环境一个是IIS Express,另外一个是Project(通过CommanName可知),默认运行dotnet run会执行project类型,如果只有一个回作为默认值使用,就不会有问题。如果有多个的话会获取不到导致出错。

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:58970",
      "sslPort": 44395
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
	 "codes": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:99000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

  

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

查看官方文档

解决办法:

使用帮助命令-h,可知需要明确指定启动配置文件: dotnet run --launch-profile  xxx

 dotnet run 提示System.Net.Sockets.SocketException (10049): 在其上下文中,该请求的地址无效。 随笔

例如下面的配置文件,假如我们要使用codes-test的配置,可以这样:dotnet run --launch-profile  codes-test

学会看官方文档可以少走很多弯路。

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:58970",
      "sslPort": 44395
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
	 "codes": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:99000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
	"codes-test": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄