Skip to content

Commit

Permalink
Calling AddMediatRIncludingLibraries without any registered assemblie…
Browse files Browse the repository at this point in the history
…s should not throw
  • Loading branch information
lee-oades-ecs committed Dec 1, 2020
1 parent 286188e commit 7a46eb3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
35 changes: 35 additions & 0 deletions Build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Taken from psake https://github.com/psake/psake

<#
.SYNOPSIS
This is a helper function that runs a scriptblock and checks the PS variable $lastexitcode
to see if an error occcured. If an error is detected then an exception is thrown.
This function allows you to run command-line programs without having to
explicitly check the $lastexitcode variable.
.EXAMPLE
exec { svn info $repository_trunk } "Error executing SVN. Please verify SVN command-line client is installed"
#>
function Exec
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][scriptblock]$cmd,
[Parameter(Position=1,Mandatory=0)][string]$errorMessage = ($msgs.error_bad_command -f $cmd)
)
& $cmd
if ($lastexitcode -ne 0) {
throw ("Exec: " + $errorMessage)
}
}

$artifacts = ".\artifacts"

if(Test-Path $artifacts) { Remove-Item $artifacts -Force -Recurse }

exec { & dotnet clean -c Release }

exec { & dotnet build -c Release }

exec { & dotnet test -c Release -r $artifacts --no-build -l trx --verbosity=normal }

exec { dotnet pack .\src\MediatR.Extensions.Microsoft.DependencyInjection.Libraries -c Release -o $artifacts --no-build }
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public static IServiceCollection AddMediatRIncludingLibraries(this IServiceColle
var isMediatRAlreadyRegistered = serviceCollection.Any(c => c.ServiceType == typeof(IMediator));
if (isMediatRAlreadyRegistered) throw new Exception($"MediatR is already registered in the container. {nameof(AddMediatRIncludingLibraries)} can not run.");

return serviceCollection.AddMediatR(MediatRLibraryRegistrar.GetAssemblies().ToArray(), MediatRLibraryRegistrar.GetConfigurationActions());
// If there aren't any assemblies registered, then don't add Mediator as it will error. "No assemblies found to scan. Supply at least one assembly to scan for handlers."
var assembliesToScan = MediatRLibraryRegistrar.GetAssemblies().ToArray();
if (assembliesToScan.Any())
return serviceCollection
.AddMediatR(assembliesToScan, MediatRLibraryRegistrar.GetConfigurationActions());
return serviceCollection;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ public void AddMediatRIncludingLibraries_when_Mediatr_already_registered_should_
Assert.Contains("already registered", exception.Message);
}


[Fact]
public void Calling_AddMediatRIncludingLibraries_without_any_registered_assemblies_should_not_throw()
{
// ARRANGE
var serviceCollection = new ServiceCollection();

// ACT
var exception = Record.Exception(() => serviceCollection.AddMediatRIncludingLibraries());

// ASSSERT
Assert.Null(exception);
}
}
}

0 comments on commit 7a46eb3

Please sign in to comment.