This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
MvcServiceCollectionExtensions.cs
98 lines (88 loc) · 4.18 KB
/
MvcServiceCollectionExtensions.cs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.Framework.DependencyInjection
{
public static class MvcServiceCollectionExtensions
{
public static IServiceCollection AddMvc([NotNull] this IServiceCollection services)
{
ConfigureDefaultServices(services);
services.TryAdd(MvcServices.GetDefaultServices());
return services;
}
/// <summary>
/// Configures a set of <see cref="MvcOptions"/> for the application.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">The <see cref="MvcOptions"/> which need to be configured.</param>
public static void ConfigureMvc(
[NotNull] this IServiceCollection services,
[NotNull] Action<MvcOptions> setupAction)
{
services.Configure(setupAction);
}
/// <summary>
/// Register the specified <paramref name="controllerTypes"/> as services and as a source for controller
/// discovery.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register in the
/// <paramref name="services"/> and used for controller discovery.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection WithControllersAsServices(
[NotNull] this IServiceCollection services,
[NotNull] IEnumerable<Type> controllerTypes)
{
var controllerTypeProvider = new FixedSetControllerTypeProvider();
foreach (var type in controllerTypes)
{
services.AddTransient(type);
controllerTypeProvider.ControllerTypes.Add(type.GetTypeInfo());
}
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
services.Replace(ServiceDescriptor.Instance<IControllerTypeProvider>(controllerTypeProvider));
return services;
}
/// <summary>
/// Registers controller types from the specified <paramref name="assemblies"/> as services and as a source
/// for controller discovery.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="controllerAssemblies">Assemblies to scan.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection WithControllersAsServices(
[NotNull] this IServiceCollection services,
[NotNull] IEnumerable<Assembly> controllerAssemblies)
{
var assemblyProvider = new FixedSetAssemblyProvider();
foreach (var assembly in controllerAssemblies)
{
assemblyProvider.CandidateAssemblies.Add(assembly);
}
var loggerFactory = new LoggerFactory();
var controllerTypeProvider = new DefaultControllerTypeProvider(assemblyProvider, loggerFactory);
var controllerTypes = controllerTypeProvider.ControllerTypes;
return WithControllersAsServices(services, controllerTypes.Select(type => type.AsType()));
}
private static void ConfigureDefaultServices(IServiceCollection services)
{
services.AddOptions();
services.AddDataProtection();
services.AddRouting();
services.AddCors();
services.AddAuthorization();
services.AddWebEncoders();
services.Configure<RouteOptions>(
routeOptions => routeOptions.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint)));
}
}
}