Unable to resolve service for type ‘Swashbuckle.AspNetCore.Swagger.ISwaggerProvider‘

.net core项目集成Swagger后,可以在本地运行查看接口,但是部署到测试机的IIS之后,无法使用,导致应用程序启动不起来,一直报错:

ERROR|Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer|Connection ID "16212958664439370925", Request ID "800014ae-0001-e100-b63f-84710c7967bb": An unhandled exception was thrown by the application. System.InvalidOperationException: Unable to resolve service for type 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' while attempting to Invoke middleware 'Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware'.
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.GetService(IServiceProvider sp, Type type, Type middleware)
   at lambda_method(Closure , Object , HttpContext , IServiceProvider )
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_1.<UseMiddleware>b__2(HttpContext context)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Abp.AspNetCore.Security.AbpSecurityHeadersMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

即:Unable to resolve service for type 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' while attempting to Invoke middleware 'Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware'.

 

如果不注册Swagger,应用程序即可正常显示,但是为了让前端人员查看接口文档,还是需要进行配置Swagger,那如何解决?

原因:

IIS未配置全局的ASPNETCORE_ENVIRONMENT值,比如Development、Production环境变量。

 

解决方案一:

1、C:\Windows\System32\inetsrv\config\applicationHost.config  到部署的电脑,打开这个文件。

2、找到你部署的站点名称以及节点,比如: <add name="TC.Host" managedRuntimeVersion="" managedPipelineMode="Integrated">

 

3、添加节点,指明环境:

<environmentVariables>
         <add name="ASPNETCORE_ENVIRONMENT" value="Development" />
 </environmentVariables>

 

4、重启站点即可。

解决方案二:

通过配置编辑器,进行添加环境变量,可以参考如下:

参考资料:https://stackoverflow.com/questions/31049152/publish-to-iis-setting-environment-variable

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值