In one of my web applications, I have a web service that is using Autofac, the svc file is as follows:
<%@ ServiceHost Language="C#" Debug="true"
Service="MyApp.WebServices.Contracts.Interfaces.IMyWebService, MyApp.WebServices.Contracts"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf" %>
I register the dependencies for my web service application like this:
public class IocConfig
{
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
//web services
builder.RegisterType<MyWebService>().As<IMyWebService>().InstancePerLifetimeScope();
var container = builder.Build();
AutofacHostFactory.Container = container;
}
}
From another web application, I want to connect to those services also using Autofac, so I register the dependencies like:
public class IocConfig
{
//Ioc dependencies for frontend application
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModule<AutofacWebTypesModule>();
builder
.Register(c => new ChannelFactory<ISomeWebService>(
new BasicHttpBinding(),
new EndpointAddress("http://localhost/MyApp.WebServices/MyWebService.svc")))
.InstancePerLifetimeScope();
//register service proxies
builder.Register(c => c.Resolve<ChannelFactory<IMyWebService>>().CreateChannel())
.As<IMyWebService>()
.UseWcfSafeRelease();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
Inside one of the controllers, I do the following:
public SomeController(IMyWebService myServiceProxy)
{
_myServiceProxy = myServiceProxy;
var response =_myServiceProxy.CallSomeMethod(new Request()
{
EmailAddress = "[email protected]"
});
}
The myServiceProxy of type IMyWebService is being resolved, I see that it is a transparent proxy, but when I call the "CallSomeMethod" I get an error:
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
What am I doing wrong here? Thanks!