/// <summary> /// Constructs a new DefaultServiceProviderHostFactory and ensures the app is initialized /// </summary> public DefaultServiceProviderHostFactory() { if (DI.DefaultServiceProvider != null) { return; } const string CfgAppInit = "xomfwk:AppInitializer"; Type initType = AppInitializer.GetType(ConfigurationManager.AppSettings[CfgAppInit]); if (initType == null) { throw new ApplicationException("Cannot instantiate DI application initializer. Make sure you set the " + CfgAppInit + " config to a subclass of the AppInitalizer with a default constructor."); } AppInitializer.Initalize(Activator.CreateInstance(initType) as AppInitializer); }
/// <summary> /// Registers WCF services type mappings from the given configuration excluding specified endpoints if needed. /// </summary> public static IServiceCollection AddWcfServices(this IServiceCollection container, ContextInformation ctx, Func <ServiceEndpointElement, bool> exclEndpoints) { string servicesPath = "system.serviceModel/services"; ServicesSection services = (ServicesSection)(ctx != null ? ctx.GetSection(servicesPath) : ConfigurationManager.GetSection(servicesPath)); foreach (ServiceElement service in services.Services) { Type serviceType = AppInitializer.GetType(service.Name); if (serviceType == null) { continue; } foreach (ServiceEndpointElement endpoint in service.Endpoints) { Type contractType = AppInitializer.GetType(endpoint.Contract); if (contractType == null || endpoint.IsSystemEndpoint || exclEndpoints != null && exclEndpoints(endpoint)) { continue; } container.AddScoped(contractType, serviceType); } } return(container); }
/// <summary> /// Registers WCF client type mappings from the given configuration excluding specified endpoints if needed. /// </summary> public static IServiceCollection AddWcfClientServices(this IServiceCollection container, Func <SecurityToken> tokenProvider, ContextInformation ctx, Func <ChannelEndpointElement, bool> exclEndpoints) { string clientPath = "system.serviceModel/client"; ClientSection client = (ClientSection)(ctx != null ? ctx.GetSection(clientPath) : ConfigurationManager.GetSection(clientPath)); foreach (ChannelEndpointElement endpoint in client.Endpoints) { if (endpoint.Name == null || endpoint.Contract == null || exclEndpoints != null && exclEndpoints(endpoint)) { continue; } Type contractType = AppInitializer.GetType(endpoint.Contract); Type factoryType = ctx != null ? typeof(ConfigurationChannelFactory <>).MakeGenericType(contractType) : typeof(ChannelFactory <>).MakeGenericType(contractType); container.AddSingleton(factoryType, sp => ctx == null ? Activator.CreateInstance(factoryType, endpoint.Name) : Activator.CreateInstance(factoryType, endpoint.Name, ctx, null)); container.AddScoped(contractType, sp => tokenProvider != null ? factoryType.InvokeMember("CreateChannelWithIssuedToken", BindingFlags.InvokeMethod, null, sp.GetService(factoryType), new object[] { tokenProvider() }) : factoryType.InvokeMember("CreateChannel", BindingFlags.InvokeMethod, null, sp.GetService(factoryType), new object[] { })); } return(container); }