2012-10-31 36 views
1

我有一段時間試圖讓這個工作。WCF和Autofac w/Extensionless Services

我希望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(); 
     } 
    } 
} 
+0

你是否能夠步驟t hrough?任何錯誤?最後的容器裏有什麼? – EdmundYeung99

+0

踩過它時,一切看起來都不錯。 COntainer構建,註冊在那裏,只是代碼不起作用! – Sam

回答

1

我不知道,如果在Global.asax的Application_Start方法中被調用在IIS了。

This SO answer好像你正在尋找並鏈接到this blog post

實現一個AppInitialize方法,並把它在App_Code文件夾類似。

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有關使用ServiceHostFactory而不是WebServiceHostFactory會談。

你可以嘗試使用它來代替AutofacServiceHostFactory嗎?

更新

這是一個老問題/答案,但我曾經試圖繼Autofac文件中的說明

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

的一個關鍵部分擴展名路由在正在添加UrlRoutingModule

+0

Application_Start確實被解僱......仍然難住。 – Sam

+0

是真的,但你可以嘗試在這裏初始化東西嗎? – EdmundYeung99

+0

是的,它的工作原理是容器的構建。 – Sam