Reputation: 547
I have configured simple injector as my DI container. I have followed this.
I'm guessing my issue has got to do with Registering the container to the SimpleInjectorServiceHostFactory
.
I'm using class library for wcf services hosted on web application. I have no .svc files but I have configured relative addresses. Where do I do the container registration?
public static class ContainerBootstrap
{
public static Container Container { get; set; }
static ContainerBootstrap()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WcfOperationLifestyle();
container.RegisterSingleton<ITypeAdapterFactory, AutomapperTypeAdapterFactory>();
container.Register<IDepartmentService, DepartmentService>();
var typeAdapterFactory = container.GetInstance<ITypeAdapterFactory>();
TypeAdapterFactory.SetCurrent(typeAdapterFactory);
Container.Verify();
Container = container;
}
}
AutomapperTypeAdapterFactory:
public class AutomapperTypeAdapterFactory : ITypeAdapterFactory
{
public AutomapperTypeAdapterFactory()
{
var profiles = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.BaseType == typeof(Profile));
var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
if (profile.FullName != "AutoMapper.SelfProfiler`2")
cfg.AddProfile(Activator.CreateInstance(profile) as Profile);
}
});
ContainerBootstrap.Container.Register<MapperConfiguration>(() => config);
ContainerBootstrap.Container.Register<IMapper>(() =>
ContainerBootstrap.Container.GetInstance<MapperConfiguration>()
.CreateMapper());
}
public ITypeAdapter Create() => new AutomapperTypeAdapter();
}
Custom ServiceFactory
public class WcfServiceFactory : SimpleInjectorServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new SimpleInjectorServiceHost(ContainerBootstrap.Container, serviceType,
baseAddresses);
}
}
In WebHost web.config
<serviceActivations>
<add factory="Department.InstanceProviders.WcfServiceFactory"
service="Department.DepartmentService" relativeAddress="DepartmentService.svc" />
</serviceActivations>
Upvotes: 3
Views: 1005
Reputation: 172835
You should add a factory entry under the serviceActivation
element in the web.config file. This ensures that the SimpleInjectorServiceHostFactory
is used to activate the service.
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true">
<serviceActivations>
<add factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory,
SimpleInjector.Integration.Wcf"
relativeAddress="~/Service1.svc"
service="TestService.Service1" />
</serviceActivations>
</serviceHostingEnvironment>
Upvotes: 4