本文整理汇总了C#中ServiceCollection.AddMvc方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddMvc方法的具体用法?C# ServiceCollection.AddMvc怎么用?C# ServiceCollection.AddMvc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.AddMvc方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MultiRegistrationServiceTypes_AreRegistered_MultipleTimes
public void MultiRegistrationServiceTypes_AreRegistered_MultipleTimes()
{
// Arrange
var services = new ServiceCollection();
// Register a mock implementation of each service, AddMvcServices should add another implemenetation.
foreach (var serviceType in MutliRegistrationServiceTypes)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType.Key);
services.Add(ServiceDescriptor.Transient(serviceType.Key, mockType));
}
// Act
services.AddMvc();
// Assert
foreach (var serviceType in MutliRegistrationServiceTypes)
{
AssertServiceCountEquals(services, serviceType.Key, serviceType.Value.Length + 1);
foreach (var implementationType in serviceType.Value)
{
AssertContainsSingle(services, serviceType.Key, implementationType);
}
}
}
示例2: Resolve_AddTypedRoutingShouldAddExpressionRouteHelper
public void Resolve_AddTypedRoutingShouldAddExpressionRouteHelper()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddMvc().AddTypedRouting();
var serviceProvider = serviceCollection.BuildServiceProvider();
var expressionRouteHelper = serviceProvider.GetRequiredService<IExpressionRouteHelper>();
Assert.IsAssignableFrom<ExpressionRouteHelper>(expressionRouteHelper);
}
示例3: ShoppingCartControllerTest
public ShoppingCartControllerTest()
{
var services = new ServiceCollection();
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<MusicStoreContext>(options => options.UseInMemoryDatabase());
services.AddMvc();
_serviceProvider = services.BuildServiceProvider();
}
示例4: GetServiceProvider
public static IServiceProvider GetServiceProvider()
{
var services = new ServiceCollection();
services.AddMvc();
services.AddEntityFramework()
.AddInMemoryDatabase()
.AddDbContext<ChatLeIdentityDbContext>(options => options.UseInMemoryDatabase());
services.AddInstance<ILoggerFactory>(new LoggerFactory());
services.AddIdentity<ChatLeUser, IdentityRole>();
services.AddChatLe();
return services.BuildServiceProvider();
}
示例5: ShoppingCartControllerTest
public ShoppingCartControllerTest()
{
var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();
var services = new ServiceCollection();
services
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase().UseInternalServiceProvider(efServiceProvider));
services.AddMvc();
_serviceProvider = services.BuildServiceProvider();
}
示例6: CreateNode
public static Node CreateNode(out IServiceProvider provider)
{
IServiceCollection sc = new ServiceCollection();
sc.AddLogging();
sc.AddInMemoryRPC();
sc.AddRaft(p => {
p.UseLogging = true;
p.FailureTolerance = -1;
});
sc.AddMvc();
sc.Configure<RaftOptions>(p => { });
provider = sc.BuildServiceProvider();
var loggerFactory = provider.GetService<ILoggerFactory>();
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole();
loggerFactory.AddDebug();
return provider.GetService<Node>();
}
示例7: SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes
public void SingleRegistrationServiceTypes_AreNotRegistered_MultipleTimes()
{
// Arrange
var services = new ServiceCollection();
// Register a mock implementation of each service, AddMvcServices should not replace it.
foreach (var serviceType in SingleRegistrationServiceTypes)
{
var mockType = typeof(Mock<>).MakeGenericType(serviceType);
services.Add(ServiceDescriptor.Transient(serviceType, mockType));
}
// Act
services.AddMvc();
// Assert
foreach (var singleRegistrationType in SingleRegistrationServiceTypes)
{
AssertServiceCountEquals(services, singleRegistrationType, 1);
}
}
示例8: InitializeServices
private static void InitializeServices(HttpContext httpContext, Action<MvcOptions> updateOptions = null)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddMvc();
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
var actionContext = new ActionContext(httpContext, new RouteData(), new ControllerActionDescriptor());
var actionContextAccessor =
httpContext.RequestServices.GetRequiredService<IActionContextAccessor>();
actionContextAccessor.ActionContext = actionContext;
var options = new TestMvcOptions().Value;
if (updateOptions != null)
{
updateOptions(options);
}
var actionBindingContextAccessor =
httpContext.RequestServices.GetRequiredService<IActionBindingContextAccessor>();
actionBindingContextAccessor.ActionBindingContext = GetActionBindingContext(options, actionContext);
}
示例9: GetHttpContext
private static HttpContext GetHttpContext(
Action<HttpRequest> updateRequest = null,
Action<MvcOptions> updateOptions = null)
{
var httpContext = new DefaultHttpContext();
if (updateRequest != null)
{
updateRequest(httpContext.Request);
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddMvc();
serviceCollection.AddTransient<ILoggerFactory, LoggerFactory>();
if (updateOptions != null)
{
serviceCollection.Configure(updateOptions);
}
httpContext.RequestServices = serviceCollection.BuildServiceProvider();
return httpContext;
}
示例10: AddMvcServicesTwice_DoesNotAddDuplicates
public void AddMvcServicesTwice_DoesNotAddDuplicates()
{
// Arrange
var services = new ServiceCollection();
// Act
services.AddMvc();
services.AddMvc();
// Assert
var singleRegistrationServiceTypes = SingleRegistrationServiceTypes;
foreach (var service in services)
{
if (singleRegistrationServiceTypes.Contains(service.ServiceType))
{
// 'single-registration' services should only have one implementation registered.
AssertServiceCountEquals(services, service.ServiceType, 1);
}
else if (service.ImplementationType != null && !service.ImplementationType.GetTypeInfo().Assembly.FullName.Contains("Mvc"))
{
// Ignore types that don't come from MVC
}
else
{
// 'multi-registration' services should only have one *instance* of each implementation registered.
AssertContainsSingle(services, service.ServiceType, service.ImplementationType);
}
}
}
示例11: AddMvc_AddsAssemblyPartsForFrameworkTagHelpers
public void AddMvc_AddsAssemblyPartsForFrameworkTagHelpers()
{
// Arrange
var mvcRazorAssembly = typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;
var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly;
var services = new ServiceCollection();
var providers = new IApplicationFeatureProvider[]
{
new ControllerFeatureProvider(),
new ViewComponentFeatureProvider()
};
// Act
services.AddMvc();
// Assert
var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));
Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
Assert.NotNull(descriptor.ImplementationInstance);
var manager = Assert.IsType<ApplicationPartManager>(descriptor.ImplementationInstance);
Assert.Equal(2, manager.ApplicationParts.Count);
Assert.Single(manager.ApplicationParts.OfType<AssemblyPart>(), p => p.Assembly == mvcRazorAssembly);
Assert.Single(manager.ApplicationParts.OfType<AssemblyPart>(), p => p.Assembly == mvcTagHelpersAssembly);
}
示例12: AddMvcTwice_DoesNotAddApplicationFeatureProvidersTwice
public void AddMvcTwice_DoesNotAddApplicationFeatureProvidersTwice()
{
// Arrange
var services = new ServiceCollection();
var providers = new IApplicationFeatureProvider[]
{
new ControllerFeatureProvider(),
new ViewComponentFeatureProvider()
};
// Act
services.AddMvc();
services.AddMvc();
// Assert
var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));
Assert.Equal(ServiceLifetime.Singleton, descriptor.Lifetime);
Assert.NotNull(descriptor.ImplementationInstance);
var manager = Assert.IsType<ApplicationPartManager>(descriptor.ImplementationInstance);
Assert.Collection(manager.FeatureProviders,
feature => Assert.IsType<ControllerFeatureProvider>(feature),
feature => Assert.IsType<ViewComponentFeatureProvider>(feature),
feature => Assert.IsType<TagHelperFeatureProvider>(feature),
feature => Assert.IsType<MetadataReferenceFeatureProvider>(feature));
}
示例13: AddMvcCore_ReusesExistingApplicationPartManagerInstance_IfFoundOnServiceCollection
public void AddMvcCore_ReusesExistingApplicationPartManagerInstance_IfFoundOnServiceCollection()
{
// Arrange
var services = new ServiceCollection();
var manager = new ApplicationPartManager();
services.AddSingleton(manager);
// Act
services.AddMvc();
// Assert
var descriptor = Assert.Single(services, d => d.ServiceType == typeof(ApplicationPartManager));
Assert.Same(manager, descriptor.ImplementationInstance);
}