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
40 changes: 40 additions & 0 deletions samples/basic/NetFramework/Console.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Console", "Console\Console.csproj", "{7CBE34BE-E223-4C4A-B0D7-19029535C3C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "..\..\..\src\Mediator\Mediator.csproj", "{06CD98C6-8655-4704-939E-E16FAA03D22E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator.SourceGenerator", "..\..\..\src\Mediator.SourceGenerator\Mediator.SourceGenerator.csproj", "{FE177F43-4286-4925-87DD-7A9B17F0A611}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9DD5375D-AC95-488F-9F5D-90DC0C2DF19C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7CBE34BE-E223-4C4A-B0D7-19029535C3C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7CBE34BE-E223-4C4A-B0D7-19029535C3C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7CBE34BE-E223-4C4A-B0D7-19029535C3C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7CBE34BE-E223-4C4A-B0D7-19029535C3C8}.Release|Any CPU.Build.0 = Release|Any CPU
{06CD98C6-8655-4704-939E-E16FAA03D22E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06CD98C6-8655-4704-939E-E16FAA03D22E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06CD98C6-8655-4704-939E-E16FAA03D22E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06CD98C6-8655-4704-939E-E16FAA03D22E}.Release|Any CPU.Build.0 = Release|Any CPU
{FE177F43-4286-4925-87DD-7A9B17F0A611}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE177F43-4286-4925-87DD-7A9B17F0A611}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE177F43-4286-4925-87DD-7A9B17F0A611}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE177F43-4286-4925-87DD-7A9B17F0A611}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{06CD98C6-8655-4704-939E-E16FAA03D22E} = {9DD5375D-AC95-488F-9F5D-90DC0C2DF19C}
{FE177F43-4286-4925-87DD-7A9B17F0A611} = {9DD5375D-AC95-488F-9F5D-90DC0C2DF19C}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions samples/basic/NetFramework/Console/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
81 changes: 81 additions & 0 deletions samples/basic/NetFramework/Console/Console.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7CBE34BE-E223-4C4A-B0D7-19029535C3C8}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Console.NetFramework</RootNamespace>
<AssemblyName>Console.NetFramework</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<LangVersion>9</LangVersion>
<ImplicitUsings>disable</ImplicitUsings>
<!-- Tells the compiler to emit the code generated by Mediator.SourceGenerator as files in the project -->
<!-- This is useful for debugging purposes -->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<!-- The path where the generated files will be placed -->
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<!-- Tells the compiler to ignore the generated files when compiling the project (it will still be part of the compilation) -->
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\Mediator.SourceGenerator\Mediator.SourceGenerator.csproj" OutputItemType="Analyzer">
<Project>{fe177f43-4286-4925-87dd-7a9b17f0a611}</Project>
<Name>Mediator.SourceGenerator</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\src\Mediator\Mediator.csproj">
<Project>{06cd98c6-8655-4704-939e-e16faa03d22e}</Project>
<Name>Mediator</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
119 changes: 119 additions & 0 deletions samples/basic/NetFramework/Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Mediator;
using Microsoft.Extensions.DependencyInjection;

namespace SimpleConsole
{
internal class Program
{
static async Task<int> Main(string[] args)
{
var services = new ServiceCollection();

// This extensions method is generated, and is put in the "Microsoft.Extensions.DependencyInjection" namespace.
// We override the namespace in the "MediatorOptions" object.
services.AddMediator(options =>
{
options.Namespace = "SimpleConsole";
});

// Standard handlers are added by default, but we need to add pipeline steps manually.
// Here are two examples.
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(GenericLoggerHandler<,>)); // This will run 1st
services.AddSingleton<IPipelineBehavior<Ping, Pong>, PingValidator>(); // This will run 2nd

var serviceProvider = services.BuildServiceProvider();

var mediator = serviceProvider.GetRequiredService<IMediator>();

var id = Guid.NewGuid();
var request = new Ping(id);

var response = await mediator.Send(request);

Console.WriteLine("-----------------------------------");
Console.WriteLine("ID: " + id);
Console.WriteLine(request);
Console.WriteLine(response);

return response.Id == id ? 0 : 1;
}
}

//
// Here are the types used
//

public sealed class Ping : IRequest<Pong>
{
public Guid Id { get; }

public Ping(Guid id)
{
Id = id;
}
}

public sealed record Pong
{
public Guid Id { get; }

public Pong(Guid id)
{
Id = id;
}
}

public sealed class GenericLoggerHandler<TMessage, TResponse> : IPipelineBehavior<TMessage, TResponse>
where TMessage : IMessage
{
public async ValueTask<TResponse> Handle(
TMessage message,
MessageHandlerDelegate<TMessage, TResponse> next,
CancellationToken cancellationToken
)
{
Console.WriteLine("1) Running logger handler");
try
{
var response = await next(message, cancellationToken);
Console.WriteLine("5) No error!");
return response;
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
}

public sealed class PingValidator : IPipelineBehavior<Ping, Pong>
{
public ValueTask<Pong> Handle(
Ping request,
MessageHandlerDelegate<Ping, Pong> next,
CancellationToken cancellationToken
)
{
Console.WriteLine("2) Running ping validator");
if (request is null || request.Id == default)
throw new ArgumentException("Invalid input");
else
Console.WriteLine("3) Valid input!");

return next(request, cancellationToken);
}
}

public sealed class PingHandler : IRequestHandler<Ping, Pong>
{
public ValueTask<Pong> Handle(Ping request, CancellationToken cancellationToken)
{
Console.WriteLine("4) Returning pong!");
return new ValueTask<Pong>(new Pong(request.Id));
}
}
}
33 changes: 33 additions & 0 deletions samples/basic/NetFramework/Console/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Console")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Console")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7cbe34be-e223-4c4a-b0d7-19029535c3c8")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
3 changes: 3 additions & 0 deletions samples/basic/NetFramework/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Project>
<!-- Just to not import anything -->
</Project>
Loading