Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<IsWindows>$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::get_Windows())))</IsWindows>

<LangVersion>13.0</LangVersion>
<NoWarn>$(NoWarn);CS1701;CS1702;CS1591</NoWarn>
<NoWarn>$(NoWarn);CS1701;CS1702;CS1591;NU1900</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,15 @@ services.AddMediatR(cfg =>
})
```

Or if not using Microsoft.Extensions.DependencyInjection:

```csharp
Mediator.LicenseKey = "<license key here>";
```

> [!TIP]
> The license key does not need to be set on client applications (such as Blazor WASM).
> Turn off the license warning by configuring logging in your logging start configuration:
> `builder.Logging.AddFilter("LuckyPennySoftware.AutoMapper.License", LogLevel.None);`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example is for a different library. It should be:

builder.Logging.AddFilter("LuckyPennySoftware.MediatR.License", LogLevel.None);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Siiiiiiiiiigh

Thanks!


You can register for your license key at [MediatR.io](https://mediatr.io)
12 changes: 10 additions & 2 deletions src/MediatR/Licensing/LicenseAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ namespace MediatR.Licensing;

internal class LicenseAccessor
{
private readonly MediatRServiceConfiguration _configuration;
private readonly MediatRServiceConfiguration? _configuration;
private readonly ILogger _logger;

public LicenseAccessor(MediatRServiceConfiguration configuration, ILoggerFactory loggerFactory)
{
_configuration = configuration;
_logger = loggerFactory.CreateLogger("LuckyPennySoftware.MediatR.License");
}

public LicenseAccessor(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger("LuckyPennySoftware.MediatR.License");
}

private License? _license;
Expand All @@ -35,7 +40,10 @@ private License Initialize()
return _license;
}

var key = _configuration.LicenseKey;
var key = _configuration?.LicenseKey
?? Mediator.LicenseKey
?? null;

if (key == null)
{
return new License();
Expand Down
5 changes: 5 additions & 0 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public Mediator(IServiceProvider serviceProvider, INotificationPublisher publish
_serviceProvider.CheckLicense();
}

/// <summary>
/// Gets or sets the license key. You can find your license key in your <a href="https://luckypennysoftware.com/account">account</a>.
/// </summary>
public static string? LicenseKey { get; set; }

public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
{
if (request == null)
Expand Down
23 changes: 23 additions & 0 deletions test/MediatR.Tests/Licensing/LicenseValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ public void Should_return_valid_for_actual_valid_license()
logMessages
.ShouldNotContain(log => log.Level == LogLevel.Error);
}

[Fact(Skip = "Needs license")]
public void Should_return_valid_for_actual_valid_license_via_static_property()
{
var factory = new LoggerFactory();
var provider = new FakeLoggerProvider();
factory.AddProvider(provider);

Mediator.LicenseKey = "<>";
var licenseAccessor = new LicenseAccessor(factory);

var licenseValidator = new LicenseValidator(factory);
var license = licenseAccessor.Current;

license.IsConfigured.ShouldBeTrue();

licenseValidator.Validate(license);

var logMessages = provider.Collector.GetSnapshot();

logMessages
.ShouldNotContain(log => log.Level == LogLevel.Error);
}
}
Loading