Re: The AutofacServiceHost.Container static property must be set before services can be instantiated

1,375 views
Skip to first unread message

Alex Meyer-Gleaves

unread,
Apr 9, 2013, 1:26:30 AM4/9/13
to aut...@googlegroups.com
Hi Daniel,

You should probably build your container in the Application Start event in the Global.asax.cs file instead of in your custom service host.

The steps for hosting in IIS are:
  1. In your global application startup...
    1. Build a Container where your service type is registered.
    2. Set AutofacHostFactory.Container with this built container.
  2. Update your .svc files to use the AutofacServiceHostFactory (for BasicHttpBinding or WSHttpBinding services) or theAutofacWebServiceHostFactory (for WebHttpBinding services).
See the documentation below for more information:


Also, if you do want to use a custom service factory you need to reference it in the .svc file.

<%@ ServiceHost Language="C#" Debug="true" Service="WCFService.MyService, WCFService" Factory="WCFService.MyServiceFactory, WCFService"%>

Cheers,

Alex.


On 9 April 2013 11:51, <daniel...@gmail.com> wrote:
I just downloaded a new version of autofac, autofac.web and autofac.integration.  I am learning how to host a service in iis.  I have implemented a simple service which I am able to self-host it.  However, when I try to host it in IIS,  I got the error
"The AutofacServiceHost.Container static property must be set before services can be instantiated"
 
I am grateful for your help.  The code is
 
namespace WCFService
{
    [ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string Hello();
    }
    public class MyService : IMyService
    {
        public string Hello()
        {
            return "Hello WCF";
        }
    }
}
 
namespace WCFService
{
    public class MyServiceFactory : AutofacServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var builder = new ContainerBuilder();
            builder.Register(c => new ChannelFactory<IMyService>(new BasicHttpBinding(), new EndpointAddress("http://localhost/MyWcfService")))
                   .SingleInstance();
            builder.Register(c => c.Resolve<ChannelFactory<IMyService>>().CreateChannel()).UseWcfSafeRelease();
            builder.RegisterType<MyService>().As<IMyService>();
            Container = builder.Build();

            var address = new Uri("http://localhost:8080/MyWcfService");
            var host = new ServiceHost(typeof (MyService), address);
            host.AddServiceEndpoint(typeof (IMyService), new BasicHttpBinding(), string.Empty);
            host.AddDependencyInjectionBehavior<IMyService>(Container);
            host.Description.Behaviors.Add(new ServiceMetadataBehavior {HttpGetEnabled = true, HttpGetUrl = address});

            return host;
        }
    }
}
 
and the MyService.svc file is
 

<%@ ServiceHost Language="C#" Debug="true"
Service="WCFService.MyService, WCFService"    
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"%>

 

Thank you very much.

Daniel

 

 

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autofac+u...@googlegroups.com.
To post to this group, send email to aut...@googlegroups.com.
Visit this group at http://groups.google.com/group/autofac?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages