-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from mayuanyang/responseHandling
Unify response middleware can now handle request/response
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/Mediator.Net.TestUtil/Middlewares/ChangeRequestResultMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Runtime.ExceptionServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Mediator.Net.Pipeline; | ||
using Mediator.Net.TestUtil.TestUtils; | ||
|
||
namespace Mediator.Net.TestUtil.Middlewares | ||
{ | ||
public static class ChangeRequestResultMiddleware | ||
{ | ||
public static void UseChangeRequestResultMiddleware<TContext>(this IPipeConfigurator<TContext> configurator) | ||
where TContext : IContext<IMessage> | ||
{ | ||
configurator.AddPipeSpecification(new ChangeRequestResultMiddlewareSpecification<TContext>()); | ||
} | ||
} | ||
|
||
public class ChangeRequestResultMiddlewareSpecification<TContext> : IPipeSpecification<TContext> | ||
where TContext : IContext<IMessage> | ||
{ | ||
public bool ShouldExecute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return true; | ||
} | ||
|
||
public Task BeforeExecute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task Execute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task AfterExecute(TContext context, CancellationToken cancellationToken) | ||
{ | ||
(context.Result as dynamic).ToBeSetByMiddleware = "i am from middleware"; | ||
return Task.CompletedTask; | ||
} | ||
|
||
|
||
public Task OnException(Exception ex, TContext context) | ||
{ | ||
RubishBox.Rublish.Add(ex); | ||
ExceptionDispatchInfo.Capture(ex).Throw(); | ||
throw ex; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters