简体   繁体   English

swagger-ui 部署后返回 500

[英]swagger-ui returns 500 after deployment

Out of the box configuration works perfectly on my machine, no problems at all.开箱即用的配置在我的机器上完美运行,完全没有问题。

But when I deploy to our test environment - I get the following message但是当我部署到我们的测试环境时 - 我收到以下消息

500 : { "Message": "An error has occurred." 500 : { "Message": "发生错误。" } /api/swagger/docs/v1 /api/swagger/docs/v1

在此处输入图片说明 The deployment is to default web site/api部署到default web site/api

Im guessing it has something to do with the baseUrl or something like that, but I have no idea of even where to begin.我猜它与 baseUrl 或类似的东西有关,但我什至不知道从哪里开始。

My routes work fine within the project - I can call all my webapi endpoints and they respond correctly.我的路由在项目中运行良好 - 我可以调用我所有的 webapi 端点并且它们正确响应。

any help would be much appreciated任何帮助将非常感激

When debugging I was using the debug config (Which I had generated XmlComments for: Properties -> build tab -> Output -> XML Documentation File)调试时,我使用了调试配置(我为以下项目生成了 XmlComments:属性 -> 构建选项卡 -> 输出 -> XML 文档文件)

I had not done this for my release configuration (duh...) - now everything works我没有为我的发布配置做过这个(废话......) - 现在一切正常

thank you @VisualBean.谢谢@VisualBean。

As it was not so obvious for me .... how to... a simple image.因为它对我来说不是那么明显......如何......一个简单的图像。

In Project > Your Project properties > Build Tab在“项目”>“您的项目属性”>“构建选项卡”中

在此处输入图片说明

Swashbuckle is hiding the real error message due to your customErrors setting in web.config.由于 web.config 中的 customErrors 设置,Swashbuckle 隐藏了真正的错误消息。 If you set customErrors to off you should get a better error message.如果您将 customErrors 设置为 off,您应该会得到更好的错误消息。

<system.web>
    <customErrors mode="Off"/>
</system.web>

As stated in the accepted answer you have to make sure the XML documentation file output is in bin and not bin\\Debug or bin\\Release (verify this for all build configurations).如已接受的答案中所述,您必须确保 XML 文档文件输出在bin 中,而不是bin\\Debugbin\\Release (验证所有构建配置)。

I still got the 500 response because I use multiple XML documentation files.我仍然收到 500 响应,因为我使用了多个 XML 文档文件。 In my SwaggerConfig implementation I include XML documentation files from two projects (the WebApi project itself and a class library that is referenced by the WebApi project):在我的SwaggerConfig实现中,我包含来自两个项目(WebApi 项目本身和 WebApi 项目引用的类库)的 XML 文档文件:

c.IncludeXmlComments(string.Format(@"{0}\bin\MyWebApiProject.xml", System.AppDomain.CurrentDomain.BaseDirectory));
c.IncludeXmlComments(string.Format(@"{0}\bin\ReferencedProject.xml", System.AppDomain.CurrentDomain.BaseDirectory));

The XML documentation file of the WebApi project was published correctly to the bin folder of the site, however the XML documentation file of the referenced project was not (even though it appears in the bin folder of the compiled project). WebApi 项目的 XML 文档文件已正确发布到站点的 bin 文件夹,但引用项目的 XML 文档文件未正确发布(即使它出现在已编译项目的 bin 文件夹中)。

So you need to modify the WebApi project file (.csproj) in a text editor and add the following sections at the bottom (replace ReferencedProject ):所以需要在文本编辑器中修改 WebApi 项目文件(.csproj),并在底部添加以下部分(替换ReferencedProject ):

<PropertyGroup>
  <CopyAllFilesToSingleFolderForPackageDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForPackageDependsOn);
  </CopyAllFilesToSingleFolderForPackageDependsOn>
  <CopyAllFilesToSingleFolderForMsdeployDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForMsdeployDependsOn);
  </CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
  <ItemGroup>
    <_CustomFiles Include="..\ReferencedProject\bin\ReferencedProject.xml" />
    <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
      <DestinationRelativePath>bin\%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

See How do you include additional files using VS2010 web deployment packages?请参阅如何使用 VS2010 Web 部署包包含其他文件? for a full explanation.以获得完整的解释。

The issue is that running dotnet publish with -r Release does not produce XML file.问题是使用-r Release运行dotnet publish不会生成 XML 文件。 However, dotnet publish with -r Debug does in fact produce the file.但是,使用-r Debug dotnet publish实际上确实会生成该文件。 This explains why people are only getting this issue when they are deploying to environments OTHER than locally, then kicking themselves when the find the exception only occurs on prod.(ITS THE RELEASE) In order to reporoduce, simply run those command locally and view output directory and you should see the issue.这解释了为什么人们只在部署到本地以外的环境时才会遇到这个问题,然后在发现异常只发生在产品上时踢自己。(它的发布)为了 reporoduce,只需在本地运行这些命令并查看输出目录,您应该会看到问题。

(UPDATE) The fix for me was to actually go into .csproj file and add a line to ensure that the file was copied over always. (更新)对我来说,修复实际上是进入 .csproj 文件并添加一行以确保始终复制该文件。 Diff shown below差异如下所示在此处输入图片说明

The accepted answer should be the first thing you try.接受的答案应该是您尝试的第一件事。

However, I have my XML output set to go to App_Data\\ and have my Swashbuckle configured to read from that directory, so, it doesn't matter which way it gets built: the xml files are going to 'be there'.但是,我将 XML 输出设置为转到 App_Data\\ 并将我的 Swashbuckle 配置为从该目录读取,因此,构建它的方式并不重要:xml 文件将“存在”。 Nevertheless, I was still getting the error...尽管如此,我仍然收到错误...

I found over at MSDN's forums @enough2012's answer:我在MSDN 的论坛上找到了@enough2012 的回答:

select "Remove additional files at destination" in the "File Publish Options" within the "Settings" pane of the Publish dialog.在“发布”对话框的“设置”窗格中的“文件发布选项”中选择“在目标位置删除其他文件”。

Worked like a charm!像魅力一样工作!

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

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