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

can this be used in blazer solution #3479

Open
swiftinitdotcom opened this issue May 22, 2021 · 4 comments
Open

can this be used in blazer solution #3479

swiftinitdotcom opened this issue May 22, 2021 · 4 comments

Comments

@swiftinitdotcom
Copy link

can this be used in blazer solution

@RicoSuter
Copy link
Owner

as far as i know it should work...
maybe you need to use the System.Text.Json output template.

@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented Jun 1, 2021

@swiftinitdotcom Yes, it works like a charm with Blazor.

@swiftinitdotcom
Copy link
Author

swiftinitdotcom commented Jun 2, 2021

great! ViRuSTriNiTy can you show me I have installed it but its not working for me. I want to use this for new blazer server project. Thank you for your help.

@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented Jun 2, 2021

Sure, here is how I use it. Some parts might be missing, it was a long process to set this up correctly.

WebApi project

  • add NuGet package NSwag.AspNetCore
  • Startup.cs
    • ConfigureServices()
      • call services.AddOpenApiDocument() and pass appropriate options, adds an OpenAPI v3 document
    • Configure()
      • call app.UseOpenApi() and pass appropriate options, serves the registered OpenAPI document
      • call app.UseSwaggerUi3() and pass appropriate options, serves Swagger UI
  • adjust controllers to use the ProducesResponseType attribute and ActionResult<> to express the return types explicitly, for example
public class FooController
{
  /// <summary>
  /// Get bar from foo.
  /// </summary>
  /// <response code="200">Found bar for the specified foo.</response>
  [HttpGet]
  [Route("GetBar")]
  [ProducesResponseType(typeof(IBar), StatusCodes.Status200OK)]
  public ActionResult<IBar> GetBar(int foo)
  {
    var result = _fooBarService.GetBar(foo);

    return Ok(result);
  }
}

WebApi.Client project

  • add NuGet package NSwag.MSBuild

  • add nswag.json generated in NSwagStudio to the project folder

    • make sure parameter assemblyPaths points to the WebApi assembly, for example

      "assemblyPaths": [
        "../WebApi/bin/$(Configuration)/net5.0/WebApi.dll"
      ],
    • make sure code generator openApiToCSharpClient is used and it outputs the file Clients.cs with client interfaces and classes, for example

      {
        "runtime": "Net50",
        ...
        "codeGenerators": {
          "openApiToCSharpClient": {
            "generateClientClasses": true,
            "generateClientInterfaces": true,
            ...
            "output": "Clients.cs"
          }
        }
      }
  • add build target to the project to build Clients.cs before project is built

    <Target Name="NSwag" BeforeTargets="Build;Rebuild">
      <Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" />
      <Exec Command="$(NSwagExe_Net50) run nswag.json /variables:Configuration=$(Configuration)" />
    
      <RemoveDir Directories="$(OutDir)References" />
    </Target>

BlazorApp project (in this case server-side)

  • add project reference WebApi.Client

  • Startup.cs

    • ConfigureServices()
      • register clients like

        services
          // add a shared HttpClient ...
          .AddHttpClient("WebApiHttpClient", (serviceProvider, httpClient) =>
          {
              httpClient.BaseAddress = ...
          })
          // ... that is injected into the following API clients ...
          .AddTypedClient<IFooClient, FooClient>()   
  • inject IFooClient where needed, for example in a page

    @inject IFooClient FooClient
    
    var bar = await FooClient.GetBar(1);

Now build WebApi, then build WebApi.Client, then build and run BlazorApp. The NSwag client should now request data from the WebApi project.

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

No branches or pull requests

3 participants