RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.78k stars 1.29k forks source link

NSwag 13.13.2 can not use Microsoft.Extensions.DependencyInjection.Abstractions 5.0.0 #3643

Open husilan opened 3 years ago

husilan commented 3 years ago

Previous I am using Microsoft.Extensions.DependencyInjection.Abstractions 3.1 and it succeeded.

And I am trying to upgrade Microsoft.Extensions.DependencyInjection.Abstractions to 5.0.0, but it failed

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Method not found: 'Microsoft.AspNetCore.Hosting.IWebHostBuilder Microsoft.AspNetCore.Hosting.IWebHostBuilder.ConfigureServices(System.Action`1)'. at Microsoft.HDInsight.ApiSvc.SwaggerHostBuilderProvider.CreateSwaggerWebHostBuilder() --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

husilan commented 3 years ago

Is there anyone who can help with this?

Here is my command: dotnet "C:\Users\silahu.nuget\packages\nswag.msbuild\13.13.2\build../tools/NetCore31/dotnet-nswag.dll" aspnetcore2openapi /variables:NoBuild=true /assembly:D:\HiloTest\HDInsight-Hilo\src\services\Api\Microsoft.HDInsight.ApiSvc\bin\x64\Debug\netcoreapp3.1\win7-x64\Microsoft.HDInsight.ApiSvc.dll /CreateWebHostBuilderMethod:Microsoft.HDInsight.ApiSvc:Microsoft.HDInsight.ApiSvc.SwaggerHostBuilderProvider.CreateSwaggerWebHostBuilder /Startup:Microsoft.HDInsight.ApiSvc.SwaggerStartup /output:Swagger/hilo_swagger.json

robv8r commented 2 years ago

Did you happen to find a solution for this?

[Edit]

I posted a potential solution below. My guess is that the $(NSwagExe_Core31) version is incompatible with Microsoft's 5.0.x and 6.0.x NuGet packages. I'm uncertain why, but $(NSwagExe_Core31) fails to find the proper entry point.

Using $(NSwagExe_Net60) worked for me. Again, see below for a more complete solution using NSwag.MSBuild.

robv8r commented 2 years ago

I wasn't able to get aspnetcore2openapi working. After reviewing the documentation, I found a reference to webapi2openapi.

My command was very similar to the one posted by @husilan. I replaced the aspnetcore2openapi command with webapi2openapi and it worked.

Please note the following:

Important: This reflection based generator is deprecated for ASP.NET Core projects and replaced by the new APi Explorer based generator AspNetCoreOpenApiDocumentGenerator!

Also, please read the Assembly Loading wiki document to ensure NSwag can find your assemblies.

If I stumble on a way to make aspnetcore2openapi work properly, I'll post it here.

robv8r commented 2 years ago

I found that aspnetcore2openapi doesn't work as expected when using $(NSwagExe_Core31) from the MSBuild NuGet package. Like @husilan, I'm working on an aspnetcore 3.1 app.

Using $(NSwagExe_Net60) worked on the first try.

For reference:

global.json

{
  "sdk": {
    "version": "3.1.102",
    "rollForward": "latestFeature"
  }
}

Project file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <!-- omitted for brevity -->
  </PropertyGroup>

  <Target Name="NswagGen" BeforeTargets="AfterBuild">
    <PropertyGroup>
      <_NSwagOutputDirectory>$(MSBuildProjectDirectory)/ClientApp/src/services</_NSwagOutputDirectory>
      <_NSwagOpenApiJsonPath>$(_NSwagOutputDirectory)/openapi.json</_NSwagOpenApiJsonPath>
    </PropertyGroup>
    <Exec Command="$(NSwagExe_Net60) aspnetcore2openapi /assembly:$(TargetPath) /documentName:v1 /output:$(_NSwagOpenApiJsonPath) /outputtype:OpenApi3">
      <Output TaskParameter="ExitCode" PropertyName="_NSwagExitCode" />
      <Output TaskParameter="ConsoleOutput" PropertyName="_NSwagOutput" />
    </Exec>
    <Message Text="$(_NSwagOutput)" Condition="'$(_NSwagExitCode)' == '0'" Importance="high" />
    <Error Text="$(_NSwagOutput)" Condition="'$(_NSwagExitCode)' != '0'" />
 </Target>

</Project>