1

I am having a heck of a time trying to get this to work.

I want Autofac to manage my WCF services and use extension less services.

Any ideas why this does not work?

namespace SAIF.Services.WCF.Host
{
    using System;
    using System.ServiceModel.Activation;
    using System.Web;
    using System.Web.Routing;
    using Autofac;
    using Autofac.Integration.Wcf;
    using SAIF.Core.Domain;
    using SAIF.Core.Domain.Model;
    using SAIF.Repositories;
    using SAIF.Services.WCF.Core;
    using SAIF.Services.WCF.Services;

    public class Global : HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            // Create Routes
            var factory = new AutofacServiceHostFactory();
            RouteTable.Routes.Add(new ServiceRoute("FileService", factory, typeof(SAIF.Services.WCF.Core.IFileService)));
            RouteTable.Routes.Add(new ServiceRoute("WebContext", factory, typeof(SAIF.Services.WCF.Core.IWebContextService)));

            // Autofac Config
            var builder = new ContainerBuilder();

            builder.Register(x => new EFUnitOfWork())
                .As<EFUnitOfWork>()
                .As<IUnitOfWork>();

            builder.RegisterGeneric(typeof(Repository<>))
                .As(typeof(IRepository<>));

            builder.Register(x => new FileService())
                .As<IFileService>();

            builder.Register(x => new WebContextService(x.Resolve<IRepository<WebContextItem>>(), x.Resolve<IUnitOfWork>()))
                .As<IWebContextService>();

            AutofacHostFactory.Container = builder.Build();
        }
    }
}
Sam
  • 15,336
  • 25
  • 85
  • 148

1 Answers1

1

I'm not sure if the Application_Start method of the Global.asax gets called by WAS in IIS.

This SO answer seems to be similar to what you are looking for and links to this blog post

Implement an AppInitialize method and put it in the App_Code folder.

public class InitialiseService
{
   /// <summary>
   /// AppInitialize method to register the IOC container.
   /// </summary>
   public static void AppInitialize()
   {
       // Create Routes
       var factory = new AutofacServiceHostFactory();
       RouteTable.Routes.Add(new ServiceRoute("FileService", factory, typeof(SAIF.Services.WCF.Core.IFileService)));
       RouteTable.Routes.Add(new ServiceRoute("WebContext", factory, typeof(SAIF.Services.WCF.Core.IWebContextService)));

       // Autofac Config
       var builder = new ContainerBuilder();

       //...the rest of your code...
   }
}

Another SO answer talks about using ServiceHostFactory rather than WebServiceHostFactory.

Can you try using that instead of the AutofacServiceHostFactory?

Update

This is an old question/answer but I have since tried following the instructions on Autofac documentation

see https://code.google.com/p/autofac/wiki/WcfIntegration#WAS_Hosting_Extensionless_Services

A key part to extensionless routing in is adding the UrlRoutingModule

Community
  • 1
  • 1
EdmundYeung99
  • 2,461
  • 4
  • 27
  • 43