Tapeti/Tapeti/Default/MessageBinding.cs

30 lines
1.2 KiB
C#
Raw Normal View History

2016-12-11 14:08:58 +00:00
using System;
using Tapeti.Config;
namespace Tapeti.Default
{
/// <inheritdoc />
/// <summary>
/// Gets the message class from the first parameter of a controller method.
/// This middleware is included by default in the standard TapetiConfig.
/// </summary>
public class MessageBinding : IControllerBindingMiddleware
2016-12-11 14:08:58 +00:00
{
/// <inheritdoc />
public void Handle(IControllerBindingContext context, Action next)
2016-12-11 14:08:58 +00:00
{
if (context.Parameters.Count == 0)
throw new TopologyConfigurationException($"First parameter of method {context.Method.Name} in controller {context.Method.DeclaringType?.Name} must be a message class");
2016-12-11 14:08:58 +00:00
var parameter = context.Parameters[0];
if (!parameter.Info.ParameterType.IsClass)
throw new TopologyConfigurationException($"First parameter {parameter.Info.Name} of method {context.Method.Name} in controller {context.Method.DeclaringType?.Name} must be a message class");
2016-12-11 14:08:58 +00:00
parameter.SetBinding(messageContext => messageContext.Message);
context.SetMessageClass(parameter.Info.ParameterType);
2016-12-11 14:08:58 +00:00
next();
}
}
}