ServiceComposer.AspNetCore

Upgrade guide from v1.x to v2.0.0

Contents

Target frameworks

Starting with version 2.0.0 the ServiceComposer.AspNetCore package targets only the following frameworks

ASP.Net Core endopints integration

ServiceComposer uses ASP.Net endpoints. The RunCompositionGateway API is deprecated in favor of explict ASP.Net endpoint configuration; Use the new endpoints mapping API:

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    app.UseRouting();
    app.UseEndpoints(builder => builder.MapCompositionHandlers());
}

snippet source | anchor

Extending the IRouteBuilder intrerface to map custom route is not anymore supported. Instead, use mapping attributes on composition handlers. See below for details about the composition handlers API.

Composition handlers API changes

Version 2.0.0 introduces a new composition API. IHandleRequests, IPublishCompositionEvents, and IHandleRequestsErrors are not supported anymore.

IHandleRequests -> ICompositionRequestsHandler

The IHandleRequests has been replaced by ICompositionRequestsHandler:

public class SampleHandler : ICompositionRequestsHandler
{
    [HttpGet("/sample/{id}")]
    public Task Handle(HttpRequest request)
    {
        return Task.CompletedTask;
    }
}

snippet source | anchor

NOTE: The IInterceptRoutes.Matches method has been replaced by routing attributes, HttpGet, HttpPost, etc.

ISubscribeToCompositionEvents -> ICompositionEventsSubscriber

The ISubscribeToCompositionEvents has been replaced by the new ICompositionEventsSubscriber:

public class SamplePublisher : ICompositionEventsSubscriber
{
    [HttpGet("/sample/{id}")]
    public void Subscribe(ICompositionEventsPublisher publisher)
    {
        // Use the publisher to subscriber to published events
        publisher.Subscribe<SampleEvent>((evt, httpRequest)=>
        {
            // Handle the event
            return Task.CompletedTask;
        });
    }
}

snippet source | anchor

NOTE: The IInterceptRoutes.Matches method has been replaced by routing attributes, HttpGet, HttpPost, etc.

IHandleRequestsErrors -> ICompositionErrorsHandler

The IHandleRequestsErrors has been replaced by ICompositionErrorsHandler:

public class SampleErrorHandler : ICompositionErrorsHandler
{
    public Task OnRequestError(HttpRequest request, Exception ex)
    {
        return Task.CompletedTask;
    }
}

snippet source | anchor

ICompositionContext

DynamicViewModel doesn’t implement ICompositionContext anymore. The composition context can be retrived from the current HttpRequest:

var context = request.GetCompositionContext();

snippet source | anchor

To raise an event use the composition context API:

await context.RaiseEvent(new AnEvent());

snippet source | anchor

The composition context also exposes the current request identifier:

var requestId = context.RequestId;

snippet source | anchor

MapCompositionHandlers write support

By default ServiceComposer responds only to HTTP GET requests, to enable write support, for example to handle POST requests, the following configuration can be used when adding ServiceComposer to the application:

public void ConfigureServices(IServiceCollection services)
{
    services.AddViewModelComposition(options => options.EnableWriteSupport());
}

snippet source | anchor

IViewModelPreviewHandler Preview

Route matching

When using composition over controllers route matching is now case insensitive by default. The previous behavior can be configured via the composition over controllers options:

public void ConfigureServices(IServiceCollection services)
{
    services.AddViewModelComposition(config =>
    {
        config.EnableCompositionOverControllers(useCaseInsensitiveRouteMatching: false);
    });
}

snippet source | anchor