-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathVersioningTestPlugin.cs
More file actions
49 lines (43 loc) · 1.89 KB
/
VersioningTestPlugin.cs
File metadata and controls
49 lines (43 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace MyTested.AspNetCore.Mvc.Plugins
{
using System;
using System.Linq;
using Asp.Versioning;
using Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
public class VersioningTestPlugin
: IHttpFeatureRegistrationPlugin,
IServiceRegistrationPlugin,
IRoutingServiceRegistrationPlugin
{
public Action<HttpContext> HttpFeatureRegistrationDelegate
=> httpContext => httpContext
.Features
.Set<IApiVersioningFeature>(new ApiVersioningFeature(httpContext));
public Func<ServiceDescriptor, bool> ServiceSelectorPredicate
=> serviceDescriptor
=> serviceDescriptor.ServiceType == typeof(IActionConstraintProvider);
public Action<IServiceCollection> ServiceRegistrationDelegate
=> serviceCollection
=> serviceCollection.AddSingleton<IActionConstraintProvider, ApiVersionActionConstraintProvider>();
public Action<IServiceCollection> RoutingServiceRegistrationDelegate
=> serviceCollection =>
{
var selectorDescriptor = serviceCollection
.FirstOrDefault(d => d.ServiceType == typeof(IActionSelector));
if (selectorDescriptor != null)
{
serviceCollection.Remove(selectorDescriptor);
serviceCollection.AddSingleton<IActionSelector>(sp =>
{
var innerSelector = (IActionSelector)ActivatorUtilities
.CreateInstance(sp, selectorDescriptor.ImplementationType);
return new ApiVersionAwareActionSelector(innerSelector);
});
}
};
}
}