static void Main() { Func <IFoo> getFoo = () => new DummyFoo(); var module = new CommandModule(getFoo); var resolver = new CommandHandlerResolver(module); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { // Use a string to decouple command name from the command clr type. This will ensure // refactoring, i.e. moving CommandThatHasASyncHandler or renaming it, won't change your http API. { "CommandThatHasASyncHandler", typeof(CommandThatHasASyncHandler) }, // Can use typeof().Name if you are not concerned with backwards compat or versioning. { typeof(CommandThatHasAnAsyncHandler).Name, typeof(CommandThatHasAnAsyncHandler) }, }; var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap); var commandHandlingMiddleware = CommandHandlingMiddleware.HandleCommands(settings); // 5. Add the middleware to your owin pipeline using (WebApp.Start("http://localhost:8080", app => { app.Use(commandHandlingMiddleware); })) { Console.WriteLine("Press any key"); } }
static void Main() { Func<IFoo> getFoo = () => new DummyFoo(); var module = new CommandModule(getFoo); var resolver = new CommandHandlerResolver(module); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { // Use a string to decouple command name from the command clr type. This will ensure // refactoring, i.e. moving CommandThatHasASyncHandler or renaming it, won't change your http API. { "CommandThatHasASyncHandler", typeof(CommandThatHasASyncHandler) }, // Can use typeof().Name if you are not concerned with backwards compat or versioning. { typeof(CommandThatHasAnAsyncHandler).Name, typeof(CommandThatHasAnAsyncHandler) }, }; var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap); var commandHandlingMiddleware = CommandHandlingMiddleware.HandleCommands(settings); // 5. Add the middleware to your owin pipeline using(WebApp.Start("http://localhost:8080", app => { app.Use(commandHandlingMiddleware); })) { Console.WriteLine("Press any key"); } }
private static void Main() { var resolver = new CommandHandlerResolver(new CommandModule()); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(CommandThatIsAccepted).Name, typeof(CommandThatIsAccepted) }, { typeof(CommandThatThrowsProblemDetailsException).Name, typeof(CommandThatThrowsProblemDetailsException) } }; var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap); var commandHandlingMiddleware = CommandHandlingMiddleware.HandleCommands(settings); using (WebApp.Start("http://*****:*****@"..\..\wwwroot") }); app.UseStaticFiles(new StaticFileOptions { RequestPath = new PathString("/cedarjs"), FileSystem = new PhysicalFileSystem(@"..\..\..\Cedar.CommandHandling.Http.Js") }); app.Map("/test/commands", commandsApp => commandsApp.Use(commandHandlingMiddleware)); })) { Process.Start("http://localhost:8080/index.html"); Console.WriteLine("Press any key to exit"); Console.ReadKey(); } }
public CommandHandlingFixture() { var module = CreateCommandHandlerModule(); var handlerResolver = new CommandHandlerResolver(module); CommandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).Name.ToLowerInvariant(), typeof(Command) }, { typeof(CommandThatThrowsStandardException).Name.ToLowerInvariant(), typeof(CommandThatThrowsStandardException) }, { typeof(CommandThatThrowsProblemDetailsException).Name.ToLowerInvariant(), typeof(CommandThatThrowsProblemDetailsException) }, { typeof(CommandThatThrowsMappedException).Name.ToLowerInvariant(), typeof(CommandThatThrowsMappedException) }, { typeof(CommandThatThrowsCustomProblemDetailsException).Name.ToLowerInvariant(), typeof(CommandThatThrowsCustomProblemDetailsException) } }; var commandHandlingSettings = new CommandHandlingSettings(handlerResolver, CommandMediaTypeMap) { MapProblemDetailsFromException = CreateProblemDetails }; _midFunc = CommandHandlingMiddleware.HandleCommands(commandHandlingSettings); }
public async Task Can_invoke_command_over_http() { // 1. Setup the middlware var resolver = new CommandHandlerResolver(new CommandModule()); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).FullName.ToLower(), typeof(Command) } }; var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap); var middleware = CommandHandlingMiddleware.HandleCommands(settings); // 2. Create an embedded HttpClient. This allows invoking of the // HttpPipeline in-memory without a server / listener. using(HttpClient client = middleware.CreateEmbeddedClient()) { // 3. This is as close as you can get to simulating a real client call // without needing real server. // Can use this to do acceptance testing also. await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap); } }
public async Task Can_invoke_command_over_http() { // 1. Setup the middlware var resolver = new CommandHandlerResolver(new CommandModule()); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).FullName.ToLower(), typeof(Command) } }; var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap); var middleware = CommandHandlingMiddleware.HandleCommands(settings); // 2. Create an embedded HttpClient. This allows invoking of the // HttpPipeline in-memory without a server / listener. using (HttpClient client = middleware.CreateEmbeddedClient()) { // 3. This is as close as you can get to simulating a real client call // without needing real server. // Can use this to do acceptance testing also. await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap); } }
public async Task Should_invoke_predispatch_hook() { var module = new CommandHandlerModule(); string correlationId = null; const string correlationIdKey = "CorrelationId"; module.For <Command>().Handle((commandMessage, __) => { correlationId = commandMessage.Metadata.Get <string>(correlationIdKey); return(Task.FromResult(0)); }); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).Name.ToLower(), typeof(Command) } }; var settings = new CommandHandlingSettings(new CommandHandlerResolver(module), commandMediaTypeMap) { OnPredispatch = (metadata, headers) => { var correlationIdHeader = headers.SingleOrDefault(kvp => kvp.Key == correlationIdKey); if (correlationIdHeader.Value != null) { metadata[correlationIdKey] = correlationIdHeader.Value.SingleOrDefault(); } } }; var midFunc = CommandHandlingMiddleware.HandleCommands(settings); using (var client = midFunc.CreateEmbeddedClient()) { await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap, customizeRequest : request => { request.Headers.Add(correlationIdKey, "cor-1"); }); correlationId.Should().Be("cor-1"); } }
public async Task Can_invoke_command_over_http() { var resolver = new CommandHandlerResolver(new CommandModule()); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).FullName.ToLower(), typeof(Command) } }; // 1. Create the serializer var jsonSerializer = new JsonSerializer(); var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap) { // 2. Customize the deserialization DeserializeCommand = (commandReader, type) => { using (var reader = new JsonTextReader(commandReader)) { return(jsonSerializer.Deserialize(reader, type)); } } }; var middleware = CommandHandlingMiddleware.HandleCommands(settings); // 3. Customize the serialization SerializeCommand serializeCommand = (writer, command) => { jsonSerializer.Serialize(writer, command); }; // 3. Create an embedded HttpClient. This allows invoking of the // HttpPipeline in-memory without a server / listener. using (HttpClient client = middleware.CreateEmbeddedClient()) { // 3. This is as close as you can get to simulating a real client call // without needing real server. // Can use this to do acceptance testing also. await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap, serializeCommand : serializeCommand); } }
public async Task Should_invoke_predispatch_hook() { var module = new CommandHandlerModule(); string correlationId = null; const string correlationIdKey = "CorrelationId"; module.For<Command>().Handle((commandMessage, __) => { correlationId = commandMessage.Metadata.Get<string>(correlationIdKey); return Task.FromResult(0); }); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).Name.ToLower(), typeof(Command) } }; var settings = new CommandHandlingSettings(new CommandHandlerResolver(module), commandMediaTypeMap) { OnPredispatch = (metadata, headers) => { var correlationIdHeader = headers.SingleOrDefault(kvp => kvp.Key == correlationIdKey); if(correlationIdHeader.Value != null) { metadata[correlationIdKey] = correlationIdHeader.Value.SingleOrDefault(); } } }; var midFunc = CommandHandlingMiddleware.HandleCommands(settings); using(var client = midFunc.CreateEmbeddedClient()) { await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap, customizeRequest: request => { request.Headers.Add(correlationIdKey, "cor-1"); }); correlationId.Should().Be("cor-1"); } }
public async Task Can_invoke_command_over_http() { var resolver = new CommandHandlerResolver(new CommandModule()); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(Command).FullName.ToLower(), typeof(Command) } }; // 1. Create the serializer var jsonSerializer = new JsonSerializer(); var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap) { // 2. Customize the deserialization DeserializeCommand = (commandReader, type) => { using(var reader = new JsonTextReader(commandReader)) { return jsonSerializer.Deserialize(reader, type); } } }; var middleware = CommandHandlingMiddleware.HandleCommands(settings); // 3. Customize the serialization SerializeCommand serializeCommand = (writer, command) => { jsonSerializer.Serialize(writer, command); }; // 3. Create an embedded HttpClient. This allows invoking of the // HttpPipeline in-memory without a server / listener. using(HttpClient client = middleware.CreateEmbeddedClient()) { // 3. This is as close as you can get to simulating a real client call // without needing real server. // Can use this to do acceptance testing also. await client.PutCommand(new Command(), Guid.NewGuid(), commandMediaTypeMap, serializeCommand: serializeCommand); } }
public static Task PutCommand( this HttpClient client, object command, Guid commandId, CommandMediaTypeMap commandMediaTypeMap, string basePath = null, Action <HttpRequestMessage> customizeRequest = null, SerializeCommand serializeCommand = null) { Ensure.That(client, "client").IsNotNull(); Ensure.That(command, "command").IsNotNull(); Ensure.That(commandId, "commandId").IsNotEmpty(); Ensure.That(commandMediaTypeMap, "commandMediaTypeMap").IsNotNull(); return(PutCommand( client, command, commandId, commandMediaTypeMap.GetMediaType, basePath, customizeRequest, serializeCommand)); }
public static Task PutCommand( this HttpClient client, object command, Guid commandId, CommandMediaTypeMap commandMediaTypeMap, string basePath = null, Action<HttpRequestMessage> customizeRequest = null, SerializeCommand serializeCommand = null) { Ensure.That(client, "client").IsNotNull(); Ensure.That(command, "command").IsNotNull(); Ensure.That(commandId, "commandId").IsNotEmpty(); Ensure.That(commandMediaTypeMap, "commandMediaTypeMap").IsNotNull(); return PutCommand( client, command, commandId, commandMediaTypeMap.GetMediaType, basePath, customizeRequest, serializeCommand); }
public CommandMediaTypeMapTests() { _sut = new CommandMediaTypeMap(new CommandMediaTypeWithDotVersionFormatter()); }
public CommandHandlingSettings( [NotNull] ICommandHandlerResolver handlerResolver, [NotNull] CommandMediaTypeMap commandMediaTypeMap) : this(handlerResolver, commandMediaTypeMap.GetCommandType) { }
public async Task Example_exception_handling() { var resolver = new CommandHandlerResolver(new CommandModule()); var commandMediaTypeMap = new CommandMediaTypeMap(new CommandMediaTypeWithQualifierVersionFormatter()) { { typeof(CommandThatThrowsStandardException).Name.ToLower(), typeof(CommandThatThrowsStandardException) }, { typeof(CommandThatThrowsProblemDetailsException).Name.ToLower(), typeof(CommandThatThrowsProblemDetailsException) }, { typeof(CommandThatThrowsMappedException).Name.ToLower(), typeof(CommandThatThrowsMappedException) }, { typeof(CommandThatThrowsCustomProblemDetailsException).Name.ToLower(), typeof(CommandThatThrowsCustomProblemDetailsException) } }; var settings = new CommandHandlingSettings(resolver, commandMediaTypeMap) { // 10. Specify the exception -> HttpProblemDetails mapper here MapProblemDetailsFromException = MapExceptionToProblemDetails }; var middleware = CommandHandlingMiddleware.HandleCommands(settings); using (HttpClient client = middleware.CreateEmbeddedClient()) { // 11. Handling standard exceptions. try { await client.PutCommand(new CommandThatThrowsStandardException(), Guid.NewGuid(), commandMediaTypeMap); } catch (HttpRequestException ex) { Console.WriteLine(ex.Message); } // 12. Handling explicit HttpProblemDetailsExceptions try { await client.PutCommand(new CommandThatThrowsProblemDetailsException(), Guid.NewGuid(), commandMediaTypeMap); } catch (HttpProblemDetailsException <HttpProblemDetails> ex) { Console.WriteLine(ex.ProblemDetails.Detail); Console.WriteLine(ex.ProblemDetails.Status); } // 13. Handling mapped exceptions, same as #6 try { await client.PutCommand(new CommandThatThrowsMappedException(), Guid.NewGuid(), commandMediaTypeMap); } catch (HttpProblemDetailsException <HttpProblemDetails> ex) { Console.WriteLine(ex.ProblemDetails.Detail); Console.WriteLine(ex.ProblemDetails.Status); } // 14. Handling custom HttpProblemDetailExceptions try { await client.PutCommand(new CommandThatThrowsCustomProblemDetailsException(), Guid.NewGuid(), commandMediaTypeMap); } catch (CustomHttpProblemDetailsException ex) { Console.WriteLine(ex.ProblemDetails.Detail); Console.WriteLine(ex.ProblemDetails.Status); Console.WriteLine(ex.ProblemDetails.Name); } } }