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

Add CreateWebHostBuilderMethod & AspNetCoreEnvironment setting, implement UseDocumentProvider & DocumentName #1655

Merged
merged 11 commits into from
Nov 2, 2018

Conversation

RicoSuter
Copy link
Owner

@RicoSuter RicoSuter commented Oct 5, 2018

Closes #1654, closes #1698

Update: API slightly changed/Improved by PR #1714

  • Add CreateWebHostBuilderMethod setting for scenarios where no (default) Startup.cs is available
    • StartupType setting is ignored when CreateWebHostBuilderMethod is defined
  • Add UseDocumentProvider setting support
    • Add DocumentName setting (default: v1)
  • Add AspNetCoreEnvironment setting

Breaking changes:

  • UseDocumentProvider will be set to true by default for new documents (previously it was false)
    • Old nswag.json documents without UseDocumentProvider setting will be transformed to false
    • To keep old behavior in the aspnetcore2swagger command you need to explicitly set /UseDocumentProvider:false
  • Used NetFx in commands upgraded from 4.5.1 to 4.6.1

Usage of UseDocumentProvider:

image

@ilmax
Copy link

ilmax commented Oct 15, 2018

@RSuter Sorry for being late, I was able to try this one a bit more in depth, so I will write my findings here:

MsBuild integration
I was able to run the program, but there are a couple of issues here:

  1. It will and run the program, but reading from configuration does not work because WebHost.CreateDefaultBuilder uses the current directory (which is e.g. bin/Debug/netcoreapp2.1 in my case) so I had to override it in Program.cs with:
.ConfigureAppConfiguration((context, config) =>
                {
                    config.SetBasePath(@"TheProperPath");
                })
                .UseStartup<Startup>();
  1. after fixing this one, the app launches, but I got a BadImageFormatException
 System.BadImageFormatException: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
  File name: 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
     at Microsoft.EntityFrameworkCore.SqlServerRetryingExecutionStrategy.ShouldRetryOn(Exception exception)
     at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state)
     at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
     at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
     at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
     at Test.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IReflectedPermissionService reflectedPermissionService) in C:\Code\TicketingSolution\PortalAPI\Startup.cs:line 149
  --- End of stack trace from previous location where exception was thrown ---
     at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
     at Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions.InjectApiVersionRoutePolicy.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
     at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Please note I'm using the copy @ReferencePaths trick in my csproj e.g.

<Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />

NSwagStudio
After setting the Environment via command line

set ASPNETCORE_ENVIRONMENT=Development
NSwagStudio.exe

Setting the new field appropriately and the correct runtime, I was able to generate the swagger document!

@RicoSuter RicoSuter changed the title Add CreateWebHostBuilderMethod setting [WIP] Add CreateWebHostBuilderMethod setting Oct 26, 2018
@RicoSuter
Copy link
Owner Author

It will and run the program, but reading from configuration does not work because WebHost.CreateDefaultBuilder uses the current directory (which is e.g. bin/Debug/netcoreapp2.1 in my case) so I had to override it in Program.cs with:

I think there is an option to set the working directory (WorkingDirectory setting?) - have you tried that?

@RicoSuter RicoSuter changed the title [WIP] Add CreateWebHostBuilderMethod setting [WIP] Add CreateWebHostBuilderMethod setting, implement UseDocumentProvider Oct 31, 2018
@ilmax
Copy link

ilmax commented Oct 31, 2018

@RSuter yes, I've tried setting the correct location with:

.ConfigureAppConfiguration((context, config) =>
	{
		config.SetBasePath(@"TheProperPath");
	})
	.UseStartup<Startup>();

But it still result in an exception (see message above)

@RicoSuter
Copy link
Owner Author

Im talking about the workingDirectory setting of the nswag cli

@ilmax
Copy link

ilmax commented Oct 31, 2018

Oh, sorry I didn't get that one, I can try again and get back to you

@RicoSuter
Copy link
Owner Author

I'll add the AspNetCoreEnvironment setting in the next hours, fix your suggestions and merge this PR

@ilmax
Copy link

ilmax commented Oct 31, 2018

👍

@RicoSuter RicoSuter changed the title [WIP] Add CreateWebHostBuilderMethod setting, implement UseDocumentProvider Add CreateWebHostBuilderMethod & AspNetCoreEnvironment setting, implement UseDocumentProvider Oct 31, 2018
@RicoSuter RicoSuter changed the title Add CreateWebHostBuilderMethod & AspNetCoreEnvironment setting, implement UseDocumentProvider Add CreateWebHostBuilderMethod & AspNetCoreEnvironment setting, implement UseDocumentProvider & DocumentName Oct 31, 2018
@RicoSuter
Copy link
Owner Author

@ilmax ok, PR is ready and I now start running some integration tests with it... it should now completely cover your scenarios. Also see updated PR description.

If possible, please test with the following settings:

  • use new CreateWebHostBuilderMethod setting
  • use new AspNetCoreEnvironment setting
  • use WorkingDirectory setting

If everything works as expected, we can merge.

@RicoSuter RicoSuter merged commit 95b0b27 into master Nov 2, 2018
This was referenced Nov 15, 2018
@RicoSuter RicoSuter deleted the feature/add-web-host-builder-setting branch December 19, 2018 15:48
@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented May 5, 2020

@RicoSuter How exactly do i use the setting CreateWebHostBuilderMethod ? I cannot find any documentation. The comment in the sources stating The CreateWebHostBuilder method in the form 'assemblyName:fullTypeName.methodName' or 'fullTypeName.methodName'." is not saying much, i tried tousand different values getting various exceptions. I assume you had an example why testing this? Where can i find it?

:edit

Ok nevermind, i see that proper auto discovery is already implemented hence CreateWebHostBuilder method should be called when using the default Program.cs code ... which i do.

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