【问题标题】:WCF and Autofac w/Extensionless Services带有无扩展服务的 WCF 和 Autofac
【发布时间】:2012-10-31 19:22:11
【问题描述】:

我正在努力让它发挥作用。

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

【问题讨论】:

  • 你能通过吗?有什么错误吗?最后容器里有什么?
  • 单步执行时一切看起来都很好。容器构建,注册在那里,只是代码不起作用!

标签: wcf autofac


【解决方案1】:

我不确定 Global.asax 的 Application_Start 方法是否被 IIS 中的 WAS 调用。

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

【讨论】:

  • Application_Start 确实被解雇了......仍然被难住了。
  • 是的,但是你可以尝试在这里初始化一些东西吗?
  • Autofac 有自己的服务工厂实现AutofacServiceHostFactory
  • 对此有何改进?我遇到了类似的问题。在link查看我的详细步骤
猜你喜欢
  • 2013-04-15
  • 2021-07-03
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多