ASP.NET Core 2.0 + Swashbuckle(奇数路径截断)

时间:2018-06-19 17:43:50

标签: dependency-injection asp.net-core-2.0 swashbuckle system.xml

因此,我一直在为公司编写一些ASP.NET Core培训指南,并且在Windows上遇到了一个有趣的错误。

使用Swashbuckle实现Swagger时,由于Swagger应该定位到XML文档文件的截断路径,因此该应用程序在启动时会崩溃,这似乎是Swagger的自动DI注册。

Exception has occurred: CLR/System.IO.FileNotFoundException
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.AspNetCore.Hosting.dll: 'Could not find file 'C:\Temporary\Work\Swagger'.'
   at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
   at System.Xml.XmlTextReaderImpl.OpenUrl()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
   at System.Xml.XPath.XPathDocument..ctor(String uri, XmlSpace space)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.<>c__DisplayClass31_0.<IncludeXmlComments>b__0()
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenOptions.CreateSwaggerProvider(IServiceProvider serviceProvider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at SwaggerPathIssue.Program.BuildWebHost(String[] args) in C:\Temporary\Work\Swagger # Path Issue 2.0\Program.cs:line 18
   at SwaggerPathIssue.Program.Main(String[] args) in C:\Temporary\Work\Swagger # Path Issue 2.0\Program.cs:line 14

请注意,例外中列出的路径是:

'C:\Temporary\Work\Swagger'

但实际上应该是:

'C:\Temporary\Work\Swagger #1 Demo\doc\Swagger #1 Demo.xml'

在Startup.cs中将要定位的XML文件指定为:

public void ConfigureServices(IServiceCollection services) {
  services.AddMvc();
  services.AddSwaggerGen(c => {
    c.SwaggerDoc("v1", new Info {
      Version = "v1",
      Title = "Swagger #1 Demo",
    });
    String xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
    String xmlPath = Path.Combine("doc", xmlFile);
    c.IncludeXmlComments(xmlPath);
  });      
}

从所有适当的位置(路径,文件名,启动器等)中删除#,可使所有功能正常工作。

现在,如果我根本不指定自定义路径,而是在Swagger的默认位置生成XML文件,则默认情况下(使用我的.csproj文件,或仅将XML文件复制到该位置),问题消失了。

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
  <DocumentationFile>bin\Debug\$(TargetFramework)\$(MSBuildProjectName).xml</DocumentationFile>
  <NoWarn>1701;1702;1705;1591</NoWarn>
</PropertyGroup>

但是...

如果我在Ubuntu上编译并运行原始的ASP.NET Core 2.0项目(带有#),则不会出现此问题。

如果我在Windows上编译并运行项目的ASP.NET Core 2.1版本(带有#),则不会出现此问题。

因此,这似乎是ASP.NET Core 2.0的一个特定问题,可能是使用参数的DI注册,或者可能是System.Xml如何处理路径?我还没有深入研究,但是我想在深入研究时出于知识的考虑而将问题解决。

如果有人知道此问题的根本原因,请随时回答。

0 个答案:

没有答案