1

我有一段时间试图让它发挥作用。

我希望 Autofac 管理我的 WCF 服务并使用无扩展服务。

任何想法为什么这不起作用?

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();
        }
    }
}
4

1 回答 1

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

于 2012-10-31T20:25:30.733 回答