Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into TryAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogard committed Jun 30, 2020
2 parents 8ba4579 + 008bfb2 commit 22f463f
Show file tree
Hide file tree
Showing 24 changed files with 358 additions and 115 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
strategy:
fail-fast: false
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Build and Test
run: ./Build.ps1
shell: pwsh
- name: Push to MyGet
env:
NUGET_URL: https://www.myget.org/F/mediatr-ci/api/v3/index.json
NUGET_API_KEY: ${{ secrets.MYGET_MEDIATR_CI_API_KEY }}
run: ./Push.ps1
shell: pwsh
- name: Artifacts
uses: actions/upload-artifact@v2
with:
name: artifacts
path: artifacts/**/*
17 changes: 17 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: PR

on:
pull_request:

jobs:
build:

runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Build and Test
run: ./Build.ps1
shell: pwsh
36 changes: 36 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Release

on:
push:
tags:
- '*.*.*'
jobs:
build:
strategy:
fail-fast: false
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Build and Test
run: ./Build.ps1
shell: pwsh
- name: Push to MyGet
env:
NUGET_URL: https://www.myget.org/F/mediatr-ci/api/v3/index.json
NUGET_API_KEY: ${{ secrets.MYGET_MEDIATR_CI_API_KEY }}
run: ./Push.ps1
shell: pwsh
- name: Push to NuGet
env:
NUGET_URL: https://api.nuget.org/v3/index.json
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: ./Push.ps1
shell: pwsh
- name: Artifacts
uses: actions/upload-artifact@v2
with:
name: artifacts
path: artifacts/**/*
42 changes: 6 additions & 36 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
function Test-Project
{
param([string] $DirectoryName)
& dotnet test -c Release ("""" + $DirectoryName + """")
}

# Taken from psake https://github.com/psake/psake

<#
Expand All @@ -27,39 +21,15 @@ function Exec
throw ("Exec: " + $errorMessage)
}
}
########################
# THE BUILD!
########################

Push-Location $PSScriptRoot

if(Test-Path .\artifacts) { Remove-Item .\artifacts -Force -Recurse }
$artifacts = ".\artifacts"

$branch = @{ $true = $env:APPVEYOR_REPO_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$env:APPVEYOR_REPO_BRANCH -ne $NULL];
$revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:APPVEYOR_BUILD_NUMBER, 10); $false = "local" }[$env:APPVEYOR_BUILD_NUMBER -ne $NULL];
$suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)))-$revision"}[$branch -eq "master" -and $revision -ne "local"]
$commitHash = $(git rev-parse --short HEAD)
$buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""]
$versionSuffix = @{ $true = "--version-suffix=$($suffix)"; $false = ""}[$suffix -ne ""]
if(Test-Path $artifacts) { Remove-Item $artifacts -Force -Recurse }

echo "build: Package version suffix is $suffix"
echo "build: Build version suffix is $buildSuffix"

exec { dotnet build -c Release --version-suffix=$buildSuffix -v q /nologo }
exec { & dotnet clean -c Release }

foreach ($test in ls test/*.Tests) {
Push-Location $test

echo "build: Testing project in $test"

try {
& dotnet test -c Release --no-build --no-restore
if($LASTEXITCODE -ne 0) { exit 3 }
} finally {
Pop-Location
}
}
exec { & dotnet build -c Release }

exec { dotnet pack .\src\MediatR.Extensions.Microsoft.DependencyInjection -c Release -o ..\..\artifacts --include-symbols --no-build $versionSuffix }
exec { & dotnet test -c Release -r $artifacts --no-build -l trx --verbosity=normal }

Pop-Location
exec { dotnet pack .\src\MediatR.Extensions.Microsoft.DependencyInjection -c Release -o $artifacts --no-build }
5 changes: 4 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>7.0.0</VersionPrefix>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>
</Project>
5 changes: 4 additions & 1 deletion MediatR.DI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ VisualStudioVersion = 16.0.28803.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{1082ED20-13D1-444F-BAFD-17EFB9152981}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
Build.ps1 = Build.ps1
.github\workflows\ci.yml = .github\workflows\ci.yml
Directory.Build.props = Directory.Build.props
.github\workflows\pr.yml = .github\workflows\pr.yml
Push.ps1 = Push.ps1
README.md = README.md
.github\workflows\release.yml = .github\workflows\release.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatR.Extensions.Microsoft.DependencyInjection", "src\MediatR.Extensions.Microsoft.DependencyInjection\MediatR.Extensions.Microsoft.DependencyInjection.csproj", "{CD3BCC44-44C0-4A5A-9041-34B558A9FBF2}"
Expand Down
14 changes: 14 additions & 0 deletions Push.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$scriptName = $MyInvocation.MyCommand.Name
$artifacts = "./artifacts"

if ([string]::IsNullOrEmpty($Env:NUGET_API_KEY)) {
Write-Host "${scriptName}: NUGET_API_KEY is empty or not set. Skipped pushing package(s)."
} else {
Get-ChildItem $artifacts -Filter "*.nupkg" | ForEach-Object {
Write-Host "$($scriptName): Pushing $($_.Name)"
dotnet nuget push $_ --source $Env:NUGET_URL --api-key $Env:NUGET_API_KEY
if ($lastexitcode -ne 0) {
throw ("Exec: " + $errorMessage)
}
}
}
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# MediatR extensions for Microsoft.Extensions.DependencyInjection

![CI](https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection/workflows/CI/badge.svg)
[![NuGet](https://img.shields.io/nuget/dt/mediatr.extensions.microsoft.dependencyinjection.svg)](https://www.nuget.org/packages/mediatr.extensions.microsoft.dependencyinjection)
[![NuGet](https://img.shields.io/nuget/vpre/mediatr.extensions.microsoft.dependencyinjection.svg)](https://www.nuget.org/packages/mediatr.extensions.microsoft.dependencyinjection)
[![MyGet (dev)](https://img.shields.io/myget/mediatr-ci/v/mediatr.extensions.microsoft.dependencyinjection.svg)](https://myget.org/gallery/mediatr-ci)

Scans assemblies and adds handlers, preprocessors, and postprocessors implementations to the container. To use, with an `IServiceCollection` instance:

```
Expand All @@ -12,12 +17,30 @@ or with an assembly:
services.AddMediatR(typeof(Startup).GetTypeInfo().Assembly);
```

Supports generic variance of handlers.
This registers:

- `IMediator` as transient
- `IRequestHandler<>` concrete implementations as transient
- `INotificationHandler<>` concrete implementations as transient
- `IRequestPreProcessor<>` concrete implementations as transient
- `IRequestHandler<>` concrete implementations as transient
- `IRequestPostProcessor<,>` concrete implementations as transient
- `IRequestExceptionHandler<,,>` concrete implementations as transient

This also registers open generic implementations for:

- `INotificationHandler<>`
- `IRequestPreProcessor<>`
- `IRequestHandler<>`
- `IRequestPostProcessor<,>`
- `IRequestExceptionHandler<,,>`

Keep in mind that the built-in container does not support constrained open generics. If you want this behavior, you will need to add any one of the conforming containers.

To customize registration, such as lifecycle or the registration type:

```c#
services.AddMediatR(cfg => cfg.Using<MyCustomMediator>().AsSingleton(), typeof(Startup));
```

To register behaviors, pre- or post-processors, register them individually before or after calling `AddMediatR`.
To register behaviors, register them individually before or after calling `AddMediatR`.
40 changes: 0 additions & 40 deletions appveyor.yml

This file was deleted.

Binary file added assets/logo/flat_128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/flat_32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/flat_64x64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/gradient_128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/gradient_32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo/gradient_64x64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
<AssemblyName>MediatR.Extensions.Microsoft.DependencyInjection</AssemblyName>
<PackageId>MediatR.Extensions.Microsoft.DependencyInjection</PackageId>
<PackageTags>mediator;request;response;queries;commands;notifications</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/jbogard/MediatR/master/assets/logo/gradient_128x128.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/jbogard/MediatR/blob/master/LICENSE</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection</RepositoryUrl>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\MediatR.snk</AssemblyOriginatorKeyFile>
<PackageIcon>gradient_128x128.png</PackageIcon>

<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\assets\logo\gradient_128x128.png" Pack="true" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageReference Include="MediatR" Version="7.0.0" />
<PackageReference Include="MediatR" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="2.3.0" PrivateAssets="All" />
</ItemGroup>

</Project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ public static void AddMediatRClasses(IServiceCollection services, IEnumerable<As
ConnectImplementationsToTypesClosing(typeof(INotificationHandler<>), services, assembliesToScan, true);
ConnectImplementationsToTypesClosing(typeof(IRequestPreProcessor<>), services, assembliesToScan, true);
ConnectImplementationsToTypesClosing(typeof(IRequestPostProcessor<,>), services, assembliesToScan, true);
ConnectImplementationsToTypesClosing(typeof(IRequestExceptionHandler<,,>), services, assembliesToScan, true);
ConnectImplementationsToTypesClosing(typeof(IRequestExceptionAction<,>), services, assembliesToScan, true);

var multiOpenInterfaces = new[]
{
typeof(INotificationHandler<>),
typeof(IRequestPreProcessor<>),
typeof(IRequestPostProcessor<,>)
typeof(IRequestPostProcessor<,>),
typeof(IRequestExceptionHandler<,,>),
typeof(IRequestExceptionAction<,>)
};

foreach (var multiOpenInterface in multiOpenInterfaces)
{
var concretions = assembliesToScan
.SelectMany(a => a.DefinedTypes)
.Where(type => Enumerable.Any<Type>(type.FindInterfacesThatClose(multiOpenInterface)))
.Where(type => type.FindInterfacesThatClose(multiOpenInterface).Any())
.Where(type => type.IsConcrete() && type.IsOpenGeneric())
.ToList();

Expand Down Expand Up @@ -59,7 +63,7 @@ private static void ConnectImplementationsToTypesClosing(Type openRequestInterfa
var interfaces = new List<Type>();
foreach (var type in assembliesToScan.SelectMany(a => a.DefinedTypes).Where(t => !t.IsOpenGeneric()))
{
var interfaceTypes = Enumerable.ToArray<Type>(type.FindInterfacesThatClose(openRequestInterface));
var interfaceTypes = type.FindInterfacesThatClose(openRequestInterface).ToArray();
if (!interfaceTypes.Any()) continue;

if (type.IsConcrete())
Expand Down Expand Up @@ -165,7 +169,7 @@ public static bool IsOpenGeneric(this Type type)

public static IEnumerable<Type> FindInterfacesThatClose(this Type pluggedType, Type templateType)
{
return Enumerable.Distinct<Type>(FindInterfacesThatClosesCore(pluggedType, templateType));
return FindInterfacesThatClosesCore(pluggedType, templateType).Distinct();
}

private static IEnumerable<Type> FindInterfacesThatClosesCore(Type pluggedType, Type templateType)
Expand Down Expand Up @@ -219,6 +223,8 @@ public static void AddRequiredServices(IServiceCollection services, MediatRServi
// registration for IPipelineBehavior<,> already exists. But only once.
services.TryAddTransientExact(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.TryAddTransientExact(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
services.TryAddTransientExact(typeof(IPipelineBehavior<,>), typeof(RequestExceptionActionProcessorBehavior<,>));
services.TryAddTransientExact(typeof(IPipelineBehavior<,>), typeof(RequestExceptionProcessorBehavior<,>));
}

/// <summary>
Expand All @@ -228,7 +234,7 @@ public static void AddRequiredServices(IServiceCollection services, MediatRServi
/// <param name="services">The service collection</param>
/// <param name="serviceType">Service type</param>
/// <param name="implementationType">Implementation type</param>
public static void TryAddTransientExact(this IServiceCollection services, Type serviceType, Type implementationType)
private static void TryAddTransientExact(this IServiceCollection services, Type serviceType, Type implementationType)
{
if (services.Any(reg => reg.ServiceType == serviceType && reg.ImplementationType == implementationType))
{
Expand Down
Loading

0 comments on commit 22f463f

Please sign in to comment.