WebMediator 1.0.0
dotnet add package WebMediator --version 1.0.0
NuGet\Install-Package WebMediator -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WebMediator" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebMediator" Version="1.0.0" />
<PackageReference Include="WebMediator" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WebMediator --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WebMediator, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WebMediator@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WebMediator&version=1.0.0
#tool nuget:?package=WebMediator&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
WebMediator
A universal WebApi endpoint for any mediators.
Features
- Suitable for any mediators
- In/out file streams support
- Generics request types support
- Ready-to-use API clients in JavaScript and .NET
Example 1: WebMediator with MediatR
.NET CLI
dotnet new web --name "WebMediatorExample"
cd WebMediatorExample
dotnet add package WebMediator
dotnet add package MediatR --version 12.4.1
Change Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(PingHandler).Assembly));
var app = builder.Build();
app.MapMediator("mediator",
// register possible request types
cfg => cfg.RegisterTypesAssignableTo<MediatR.IBaseRequest>(typeof(Ping).Assembly),
// invoke the MediatR
async ctx => await ctx.Services.GetRequiredService<MediatR.IMediator>()
.Send(await ctx.ReadData(), ctx.CancellationToken));
app.Run();
Example 2: Request/Response
Request
GET /mediator/Ping?data={"Message":"TEST"}
or
POST /mediator/Ping
{"Message":"TEST"}
Response
{"Message":"TEST PONG"}
Example 3: .NET client
.NET CLI
dotnet new console --name "WebMediatorClientExample"
cd WebMediatorClientExample
dotnet add package WebMediator.Client
Program.cs:
using WebMediator.Client;
// create client
using var client = new WebMediatorClient("https://localhost:7263/mediator");
// send request
var response = await client.Send(new Ping { Message = "TEST" });
Console.WriteLine(response.Message);
Console output:
TEST PONG
Example 4: JavaScript client
NPM CLI
npm i web-mediator-client
JS
import { WebMediatorClient } from 'web-mediator-client';
const client = new WebMediatorClient('https://localhost:7263/mediator');
let response = await client.send('Ping', { Message: 'TEST' });
console.log(response.data);
Console output:
{"Message":"TEST PONG"}
Example 5: File upload
Create RequestHandler
public class FileUpload : MediatR.IRequest
{
public string Name { get; set; }
public Stream Content { get; set; }
}
public class FileUploadHandler : MediatR.IRequestHandler<FileUpload>
{
public async Task Handle(FileUpload request, CancellationToken cancellationToken)
{
var filePath = Path.GetFullPath(request.Name);
using var fileStream = File.Create(filePath);
await request.Content.CopyToAsync(fileStream, cancellationToken);
}
}
Sending a file in JavaScript
import { WebMediatorClient } from 'web-mediator-client';
const client = new WebMediatorClient('https://localhost:7263/mediator');
let file = document.getElementById('my-input').files[0];
let response = await client.send('FileUpload', { Name: file.name, Content: file });
Example 6: Generics requests
app.MapMediator("mediator",
// existing generic types will suffice for this example
cfg => cfg.RegisterTypes([typeof(List<>), typeof(Dictionary<,>)]),
// for simplicity, return the received data
ctx => ctx.ReadData());
Request #1: Equivalent of List<String>
POST /mediator/List(String)
["text1","text2","text3"]
Request #2: Equivalent of Dictionary<string,int?[]>
POST /mediator/Dictionary(String-Array(Nullable(Int32)))
{"key1":[555,null,777]}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- TypeSerialization (>= 1.3.0)
-
net8.0
- TypeSerialization (>= 1.3.0)
-
net9.0
- TypeSerialization (>= 1.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.