cake-contrib / Cake.NSwag

A simple Cake addin powered by NSwag for compiling client code and API definitions from a variety of sources
https://cakebuild.net/extensions/cake-nswag
MIT License
6 stars 9 forks source link

Support .NET core assemblies #5

Open fleed opened 8 years ago

fleed commented 8 years ago

I'd like to generate swagger file and client code from .NET core assemblies. Any plan to support it?

Currently I'm getting the following error running it against a .NET core app targeting net452:

Error: The 'src/DevApp/bin/Debug/net452/win7-x64/DevApp.exe' does not appear to be an assembly!

Thanks

agc93 commented 8 years ago

@fleed I haven't tried using this library with .NET Core before so can you please share more detailed example of how to reproduce what you're describing? Is the script and source you're using on a public repo anywhere?

Any information would help, thanks! 😄

fleed commented 8 years ago

The project is in a private repo, but I'll try to reproduce it in a public one. Thanks to you!

fleed commented 8 years ago

Here it is: Test project

agc93 commented 8 years ago

Okay, I see the issue now, should be a simple enough fix. I'll put something together and try and organise a new release later today or tomorrow.

agc93 commented 8 years ago

You can also try the prerelease version on NuGet (0.5.0-unstable0001 or newer) which includes the fix for this issue.

fleed commented 8 years ago

Is there a way to reference a specific version of the package in the cake script, or should I download the package manually and reference the dll with #r? thanks

agc93 commented 8 years ago

You can reference a specific version using the newer #addin syntax as below:

#addin nuget:?package=Cake.NSwag&version=0.5.0-unstable0001

You may need to clear your tools/Addins directory to force the addin to be downloaded again

You can see the full range of options in the Cake documentation.

Let me know if you have any issues.

fleed commented 8 years ago

I've just tested version 0.5.0-unstable0001 and it works for me! Thanks for helping

RicoSuter commented 8 years ago

Just for your reference: https://github.com/NSwag/NSwag/wiki/WebApiAssemblyToSwaggerGenerator#net-core

agc93 commented 8 years ago

@rsuter Interesting. Problem being (for me) is I can't guarantee the environment we run in, given it's a build script, but might be able to at least add some checks.

I assume the AssemblyLoader package will continue to use the AppDomain for now, and therefore not be .NET Standard-compatible?

fleed commented 7 years ago

Any update on this? thanks

fleed commented 7 years ago

@rsuter the generated swagger file doesn't seem correct. Example: an operation marked with HttpHead attribute maps to a POST. Is it a bug in NSwag itself? Should I open an issue in that repository?

agc93 commented 7 years ago

Sorry about the delay @fleed I've had a few other things going on lately.

I will give this another shot and if I can't get "real" .NET Core support working, I'll publish 0.5.0 as-is around the end of this week.

I think unless @rsuter is planning a .NET Core/AssemblyLoader-based change, I will have to shelve .NET Core support for the addin itself. You should still be able to load .NET Core assemblies using the settings from the NSwag documentation linked above, but only from a full .NET framework host.

Until then, you can obviously use the version-specific #addin form above.

RicoSuter commented 7 years ago

Hi everyone, I'm planning to implement a .NET Core version of the command line tools. I think this also means implementing an assembly loader for .NET Core. However there will be two NuGet packages in the end:

How does cake support that? Can you bundle the dlls for full .net and .net core in a single cake NuGet?

RicoSuter commented 7 years ago

I think loading the types directly in the process is not an option as we cannot unload the assemblies?

RicoSuter commented 7 years ago

@fleed: Head support added: https://github.com/NSwag/NSwag/commit/b6cf197605f986c2eb83f662b660b03fa6e0dce3

fleed commented 7 years ago

@rsuter delay? you've been faster in fixing it than answering.. thank you very much :) @agc93 how is NSwag referenced by the Cake library? should it be updated to get this fixed version?

I have another quick question for rsuter: is the output equivalent to the one I get with Swashbuckle? Because I had a quick look at the output and it seems that custom attributes with documentation and result description are ignored? In such case, is there any plan to support them?

RicoSuter commented 7 years ago

Its a completely new implementation and Swashbuckle attributes are currently not supported. What is missing? Its better to create an issue in the nswag repo.. see https://github.com/NSwag/NSwag/issues/389

RicoSuter commented 7 years ago

PS: The head fix is not released yet...

fleed commented 7 years ago

Thanks @rsuter , I'll create an issue in your repo with a comparison between the outputs.

agc93 commented 7 years ago

The NSwag libraries are currently bundled into the NuGet packages for Cake.NSwag so I should be able to use your individual NSwag packages to multi-target my packages, from what I can tell. Once the head fix is released, I will try and integrate and release an updated addin package using the new support.

Thanks for the quick response @rsuter as always.

RicoSuter commented 7 years ago

v6.20 will be available in 10 minutes (with this HeadAttribute fix)

agc93 commented 7 years ago

Okay @rsuter any timeline on the .NET Core version and associated NSwag.AssemblyLoaderCore package? Just so I know whether I should publish what I have now or wait for that and integrate properly 😄

RicoSuter commented 7 years ago

I think this will take some time... I think there will be some issues with that, so maybe 2-3 weeks :), but maybe more...

agc93 commented 7 years ago

Okay sounds good. I will release 0.5.0 as-is (just with the fix for the original "does not appear to be an assembly" error @fleed reported), and keep the more complex work on hold for whenever that is ready; then I can integrate it without reworking too much hopefully.

RicoSuter commented 7 years ago

Ok, what NSwag version are you using then?

RicoSuter commented 7 years ago

Is there a way/output to check what NSwag version is used?

agc93 commented 7 years ago

I'm currently targeting 6.6.6124.16498 on the develop branch (where 0.5.0 is coming from), and I'll probably do a quick update to latest to pick up that head fix before I do the merge/publish.

I will see if I can get the version of NSwag in use to output to the Verbose logs, if I get a chance.

RicoSuter commented 7 years ago

In the best case you show both versions:

agc93 commented 7 years ago

Oh, that's convenient. I'll add that in before I publish as well in that case.

RicoSuter commented 7 years ago

Just read the original issue: @fleed is trying to load a .NET 4.5.2 assembly (not .NET Core!). This should work with the current Cake.NSwag addin but may be a little bit tricky: https://github.com/NSwag/NSwag/wiki/WebApiAssemblyToSwaggerGenerator#net-core

The scenario which is missing is running Cake on Linux (as a .NET Core host) and loading .NET Core assemblies (which are compiled for .NET Core not .NET 4.5.2).

agc93 commented 7 years ago

That's correct. My understanding is once you have your .NET Core-compatible version, and I update the addin to match, that scenario should work, correct?

RicoSuter commented 7 years ago

Yes, I hope so...

I now have a working NSwag.AssemblyLoaderCore which use the AssemblyLoadContext to load assemblies. But it seems that in .NET Core you cannot create a new AssemblyLoadContext so that the assemblies are loaded in an isolated context... AssemblyLoadContext is only used to dynamically loading of assemblies. I think if you want isolation, then you'll have to start a new process...

The problem is now, that NSwag.AssemblyLoaderCore loads the assemblies into the host process... Is this a problem for Cake? If it works with a short-lived process which is restarted for each call, then its fine...

agc93 commented 7 years ago

My understanding was that each time you load using the AssemblyLoadContext, it creates a new isolated context..

From the MSDN blog post about mscorlib compatability:

screenshot 74

So at the moment, the process lives for as long as the script runs, but it could be possible to work around this, maybe even just for the .NET Core (i.e. netstandard) version.

RicoSuter commented 7 years ago

Thanks for this blog post. Locally it works, but I don't know if this has some bad side effects...

https://github.com/NSwag/NSwag/commit/5b3b05d80adebdd88c83eab40a90c815a83fbb54

RicoSuter commented 7 years ago

Ok NSwag.AssemblyLoaderCore is now available: https://www.nuget.org/packages/NSwag.AssemblyLoaderCore

agc93 commented 7 years ago

Another quick update for you guys: I have just published a new version: 0.5.0-unstable0008 on NuGet.

This release includes full .NET Core support, meaning the addin should be loadable when running on CoreCLR and we support loading .NET Core libraries using @rsuter's updated AssemblyLoaderCore package.

Now, since this package is highly experimental and there's a bunch of dirty hacks required to get the packaging to work properly, this package will probably stay in prerelease until the .NET Core tooling situation calms down a bit more. You can include the new package in your build script using either:

#addin nuget:?package=Cake.NSwag&version=0.5.0-unstable0008

or get the latest prerelease packages using:

#addin nuget:?package=Cake.NSwag&prerelease

If you have any issues with these changes, just open an issue or hit me up directly.

fleed commented 7 years ago

Any update on this? Is a stable release planned with VS 2017 RC (February 7, 2017 build 26206.00), where tooling should be almost final? Thank you for support

agc93 commented 7 years ago

@fleed I've been working on this very recently and tooling is still a nightmare: I'm getting a lot of assembly load and binding errors as well as old System.Runtime quirks reappearing after the migration to csproj.

TL:DR; still working on it, .NET Core tooling still sucks

fleed commented 7 years ago

@agc93 thank you for sharing both status update and experience. Actually I've never tested the new tooling myself, and I trust you if you're saying that it still sucks.

agc93 commented 7 years ago

Okay so sorry for the silence on this issue (I've been pretty busy lately), but I've only got bad news to share: I'm not sure I can get full .NET Core support working for this library.

You can see the progress in the netcore-support branch, but it currently fails on load with a ton of assembly loading errors. To be honest, at this point I'm chasing my tail on resolving those issues.

I'm happy for anyone to keep looking into it, but for now, .NET Core support is on hold for this project as I can't seem to get it working 😢 .

agc93 commented 7 years ago

And now for much better news: I've published a new sibling addin for NSwag: Cake.NSwag.Console.

While Cake.NSwag (this addin) uses the library directly and therefore doesn't need any dependencies, the new Cake.NSwag.Console acts as a wrapper for the CLI and can be used on .NET Framework or .NET Core, but does need the NSwag CLI to be installed first (such as through npm).

Both addins are supported and are drop-in replacements for each other.

You can find the new addin on GitHub and a post explaining the new addin on my blog

fleed commented 7 years ago

thank you for the update @agc93