Closed vvdb-architecture closed 1 year ago
I noticed that too.
Hi @vvdb-architecture , are on running the guidance on the acc environment or production? I am indeed changing this. When adding a database EF Core project if I didn't add the /Configs the msbuild task failed.
Just to know. I will test also the remark of @fcc753.
The NSwagStudio mention this
And the config folder are under Configs. But indeed it fails which was not the case when I tested => but the class was already generated...
I'm running the production guidance. @fcc753 's remark suggestion would avoid such troubles indeed. Suggestions can also be found at https://github.com/RicoSuter/NSwag/wiki/NSwag.MSBuild
Prod environment issue fixed. I don't close to implement in acc first the msbuild on the facade and interface
But working on just the facade/interface one by one seems this has become difficult to do... There's a long thread of issues on https://github.com/RicoSuter/NSwag/issues/3794 related to the error messages people started to receive with Net6 a year ago.
Running this with just the facade triggers: NSwag requires the assembly LoadManagement.Yarp.Facade, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null to have either an BuildWebHost or CreateWebHostBuilder/CreateHostBuilder method.
It seems the app has to be really running for NSWAg to do its job now...
The issue is when we add Hangfire. The error is located at the level of Hangfire. The current work around is to create another environment file with a hangfire connection string connecting to a localhost database for example and setting the new Hangfire environment in the nswag file => When the build task will run, Hangfire will silently skip itself and code will be generated correctly.
Here is another workaround, based on the need to detect when the Host is being run by Swagger, while keeping the ASPNETCORE_ENVIRONMENT as-is.
The current invocation in the host's .csproj
file is like this:
<Target Name="NSwagFacade" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Localhost" Command="$(NSwagExe_Net60) run ..\Sdks\HappyFlow.Service.Facade.Sdk\Generator\facade.nswag /variables:Configuration=$(Configuration)" />
</Target>
We could change this so that we add another environment variable called NSwag
and set it to true
like this:
<Target Name="NSwagFacade" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Localhost;NSwag=true" Command="$(NSwagExe_Net60) run ..\Sdks\HappyFlow.Service.Facade.Sdk\Generator\facade.nswag /variables:Configuration=$(Configuration)" />
</Target>
Then, in the Host startup code (or elsewhere), we can define the following method:
static bool IsInvokedByNSwag()
{
var env = System.Environment.GetEnvironmentVariable("NSwag");
return env != null && bool.TryParse(env, out var nswag) && nswag;
}
In our Host startup code, we can simply check if we are being started by NSwag
and omit any declaration which may cause the run
to fail:
if (!IsInvokedByNSwag())
{
// add HangFire or other stuff that could fail when NSwag is calling us
}
Since environment variables become part of the configuration by default, we could also write Configuration.GetValue<bool>("NSwag")
, but this requires access to an IConfiguration
, which is not always the case.
It is cleaner to encapsulate the test in its own method.
I think we can close this, since 2022.2.1.12-rc3 implemented NSwag=true
with associated tests
When generating a new solution X, the
facade.nswag
file in the X.[Yarp|Service].Facade.Sdk project contains the following:This causes the execution of [Yarp|Service] (launched by nswag.exe) to fail with the error:
.. because the configuration files are not found. The correct entry must be:
This was OK before. The bug is recent as of this time of writing.