Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP.NET Core 3 support #1961

Closed
marinasundstrom opened this issue Feb 15, 2019 · 52 comments · Fixed by #2041
Closed

ASP.NET Core 3 support #1961

marinasundstrom opened this issue Feb 15, 2019 · 52 comments · Fixed by #2041

Comments

@marinasundstrom
Copy link

Where is the ASP.NET Core 3 support?

@RicoSuter
Copy link
Owner

Usually this is added when the new library is RTM.

As a ref, here are the PRs for adding 2.2 support:
https://github.com/RSuter/NSwag/pulls?utf8=✓&q=is%3Apr+is%3Aclosed+2.2

@martincostello
Copy link

You may want to start looking into this now using the previews, as changes to remove Newtonsoft.Json as a core dependency of MVC in ASP.NET Core 3.0 will likely break compatibility and cause a blocker for people from upgrading due to usage of MvcJsonOptions such as here:

https://github.com/RSuter/NSwag/blob/66095db418f5e6b50b670d3145feb63ada34550f/src/NSwag.AspNetCore/Middlewares/WebApiToSwaggerMiddleware.cs#L25

Related issues:

@RicoSuter
Copy link
Owner

as changes to remove Newtonsoft.Json as a core dependency

Yep, this will be a major change, mainly in the NJsonSchema repo. Until now, noone could tell me what the replacement of newtonsoft’s contract resolver is...

@martincostello
Copy link

So if you want to keep a dependency on Newtonsoft.Json, you'd use AddNewtonsoftJson() for configuring MVC and then the MvcNewtonsoftJsonOptions class. I'm not entirely sure what the new types are for the native solution.

Ultimately, the problematic reference for 3.0 is Microsoft.AspNetCore.Mvc.Formatters.Json. If you removed that and then built up from there for netcoreapp3.0 you should get towards something compatible.

@RicoSuter
Copy link
Owner

When using the newtonsoft.json serializer it should be no big issue - my problem is more with the new serializer and how to get the serialization contract there...

@martincostello
Copy link

Having had a quick look at the code, a "fast" way to do this (though still a breaking change) would be to remove all usage of IOptions<MvcJsonOptions> and replace it with IOptions<JsonSerializerSettings>, given that its sole purpose is to act as a holder for the serializer settings, and then register that into the services if it isn't already there, plus maybe an explicit dependency on Newtonsoft.Json for 3.0 apps as it won't necessarily be in the application's default dependencies anymore.

Otherwise you're looking at having to come up with some sort of abstraction on to get the serializer settings, abstract away JSON serialization completely like MVC is doing, or start forking the code with things like hash defines to handle different code and serialization paths for different TFMs.

@martincostello
Copy link

Have also opened dotnet/aspnetcore#8254 suggesting MvcJsonOptions is restored via a type-forward in ASP.NET Core 3.0 preview 4 or later to reduce the impact of the changes.

@RicoSuter
Copy link
Owner

I think we could just add the new target framework .net core 3 to nswag.aspnetcore and use the new MvcNewtonsoftJsonOptions with conditional compile... the downside is that you cannot use asp.net core 2.2 on .net core 3.0 - to allow that we’d need a new package - e.g. nswag.aspnetcore3 - i.e. decouple the aspnetcore version from the target framework version. But this means potentially a new package for every “major” aspnetcore version - a maintainability nightmare

@martincostello
Copy link

That approach also has the downside of forcing the Newtonsoft.Json version of MVC JSON formatting to be active even if the user isn't using it, rather than the new native implementation if someone wants to go with that instead.

Hopefully the ASP.NET Core team can come up with something to make this less painful to manage.

@RicoSuter
Copy link
Owner

Yeah, but we would only use these options if they are registered, ie the newtonsoft json serializer is active -otherwise we would use the new implementation. But i dont know at all how the contract reflection works there...

@RicoSuter
Copy link
Owner

RicoSuter commented Mar 7, 2019

And of course the new serializer requires a lot of work because ATM everything is newtonsoft.json based - ie at the beginning nswag will only work with aspnetcore 3 WITH the newtonsoft serializer (or some [automatic] newtonsoft settings which more or less resemble the new serializer behavior)

@Zoxive
Copy link

Zoxive commented Mar 13, 2019

Does anyone know a workaround/hack that i can use today while we wait for a proper fix?

@martincostello
Copy link

Using preview 3.0, there is currently no workaround.

@RicoSuter
Copy link
Owner

So the problem is only that MvcJsonOptions has been renamed to MvcNewtonsoftJsonOptions?

@martincostello
Copy link

It’s not just been renamed, but also moved to a different assembly, so it’s a complete breaking change as the types this library are compiled against aren’t there at runtime, effectively making everything unusable.

The only current solution is to fork this repo, swap to the new type from 3.0, compile it yourself and use that.

@RicoSuter
Copy link
Owner

RicoSuter commented Mar 13, 2019

Yes, but assuming they to this type forward thing, will it work even if NSwag targets the "old" ASP.NET Core 2.2 version?

@martincostello
Copy link

If they come up with a solution then this issue can just be closed as it’ll no longer be an issue and it’ll “just work”, but looking at their reply it doesn’t look like a simple fix, but they seem to be treating it as high priority to resolve.

@RicoSuter
Copy link
Owner

RicoSuter commented Mar 22, 2019

PR for supporting ASP.NET Core 3: #2041

@RicoSuter
Copy link
Owner

Ok, the NSwag.AspNetCore package should now (v12.0.20) work with the ASP.NET Core 3 preview...

Keeping the PR open as this (conditionals & Core 3 TFM) is probably the way to go - also we need many more changes to support CLI etc...

For now probably only the ASP.NET Core middleware works. Please report any problems.

@pranavkm Does this dynamic fallback look good (for now)?
19ecd16#diff-2698934139b3148923e9785185649f08R118
(I need this "func hack" in order to be able to catch the type load exception)

@pranavkm
Copy link
Contributor

Yeah, that seems reasonable. If you have builds available with the change, I'd be happy to check the swagger gen works with the most recent preview4 bits of AspNetCore.

@RicoSuter
Copy link
Owner

@pranavkm this is available in v12.0.20 - I tested it with preview3

@marinasundstrom
Copy link
Author

marinasundstrom commented Apr 14, 2019

NSwag.MSBuild needs to be updated to support .NET Core 3.0. Generic Host support, and and adding NSwagExe_Core30.

@RicoSuter
Copy link
Owner

Yes, but i wanted to wait until core 3 is released :-) not an option?

@marinasundstrom
Copy link
Author

Aah. I understand.
Not for the progress of my project 🙂

I don’t know how hard it is to add, but a preview would be nice.

@RicoSuter
Copy link
Owner

RicoSuter commented Apr 14, 2019

I agree that it would be nice...

I saw the following problems:

  • As soon as i install core 3 preview i’m no longer able to build the solution even if there are no changes for core 3 (solvable)
  • On the ci server i need to build with vs 2019 msbuild but there is no chocolatey wix extension package for it yet (solvable)
  • My biggest concern: As soon as we add core 3 preview target framework to the projects, all people contributing to nswag need to also install it which will make it even harder for them to compile everything (it’s already hard with all the required sdks etc)

But ill see again if its worth the effort now...

@marinasundstrom
Copy link
Author

What is the progress?

@RicoSuter
Copy link
Owner

NSwag.AspNetCore (the middlewares) should work with ASP.NET Core 3.0, the CLI binaries (e.g. NSwag.MSbuild, NSwag.ConsoleCore) are currently not released with .NET Core 3.0 target framework and ASP.NET Core 3.0 dependencies because it would force all maintainers to install .NET Core 3.0 previews, etc. - waiting for a RC for now.

@Oposow
Copy link

Oposow commented Jul 25, 2019

Hi @RicoSuter,

You have written, that the middlewares should work with ASP.NET Core 3.0.
I've just installed NSwag.AspNetCore and tried to use middlewares same way, as I have used in ASP.NET Core 2.2.

Unfortunatelly, I can't download the swagger document. I got error 404 (Not Found). I tried on preview7 and preview5.
I've generated project from the VS Web Api template and added swagger middleware.

Here is my Startup.cs

`public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddOpenApiDocument();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseOpenApi();
        app.UseSwaggerUi3();


        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

`

Is there any chance to use NSwag in ASP.NET Core 3.0, or currently I have to move back to 2.2?
I'm starting new project and wanted to write it from the begining in ASP.NET Core 3 to avoid migrating it in near future. :-)

@RicoSuter
Copy link
Owner

Have you tried to add it before UseRouting()?

@vfrz
Copy link

vfrz commented Jul 26, 2019

@Oposow You probably need to add Newtonsoft Json support.
Include the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package in preview version and add .AddNewtonsoftJson() after services.AddControllers()

It is how I solved it on my project and I get it working with preview 7.

Source: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio#jsonnet-support

@Oposow
Copy link

Oposow commented Jul 27, 2019 via email

@Oposow
Copy link

Oposow commented Jul 29, 2019

@RicoSuter unfortunatelly changing the methods calling order didn't help.

The solution provided by @vfrz worked perfectly, thank you very much!

@acds
Copy link

acds commented Aug 19, 2019

Word on the street is there it may go straight from preview 8 to release, no RC's version. September 23rd at .Net Conf.

As an aside for those switching their implementation to the new native JSON support, and not planning to use the old Newtonsoft JSON libraries, how will this work with Nswag?

@martincostello
Copy link

Unless things have changed very recently, last I was aware, there was going to be a preview 9 before the final version ships during .NET Conf: https://github.com/dotnet/corefx/issues/39769#issuecomment-518766445

@RicoSuter
Copy link
Owner

As an aside for those switching their implementation to the new native JSON support, and not planning to use the old Newtonsoft JSON libraries, how will this work with Nswag?

I think it will be possible to only use newtonsoft contract resolver internally and use the new json stuff for serialization... then obly the internal resolver needs to be aligned with the actual serializer

@RicoSuter
Copy link
Owner

RicoSuter commented Aug 22, 2019

Added fallback to JsonSerializerSettings from OpenAPI document settings if settings are not provided by calling AddNewtonsoftJson().

v13.0.6

@marinasundstrom
Copy link
Author

So we wont get any MSBuild task for 3.0 before the actual release?

@RicoSuter
Copy link
Owner

RicoSuter commented Aug 23, 2019

Maybe you can already use this instead of CLI?
https://github.com/RicoSuter/NSwag/wiki/ServiceProjectReference

@marinasundstrom
Copy link
Author

@RicoSuter

Severity | Code | Description | Project | File | Line | Suppression State
-- | -- | -- | -- | -- | -- | --
Error |   | ServiceProjectReference items are no longer supported. | BlazorApp2.ApiClient | C:\Users\rober\.nuget\packages\microsoft.extensions.apidescription.client\0.3.0-preview7.19365.7\build\Microsoft.Extensions.ApiDescription.Client.targets | 139 |  

@SamazoOo
Copy link
Contributor

SamazoOo commented Sep 9, 2019

Hi @RicoSuter
When will it be available Nswag.MsBuild for .net core 3.0? How can we test preview versions?

@RicoSuter
Copy link
Owner

I hope to find time to look into this this week...

@RicoSuter
Copy link
Owner

RicoSuter commented Sep 26, 2019

Release v13.1.0: #2427

Also supports NRT (provided by Namotion.Reflection/):

image

.NET Core 3.0 support docs: https://github.com/RicoSuter/NSwag/wiki/AspNetCoreOpenApiDocumentGenerator#net-core-3-and-c-8

Please report if something is not working as expected.

image

@jasontaylordev
Copy link

Works great, thank you. Implemented here;
https://github.com/JasonGT/NorthwindTraders/tree/master/Src/WebUI

@SeppPenner
Copy link

This is the solution: #1961 (comment).

@statler
Copy link

statler commented Nov 4, 2020

Does not work for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.