小编Bry*_*ant的帖子

必须先设置AutofacServiceHost.Container静态属性,然后才能实例化服务

在我的XXXX.WS WCF服务项目中,我正在尝试使用autofac进行DI/IOC ...整天都在使用它但是我认为我很接近(这里有不同的错误)...这个错误我可以'知道如何动摇......"必须设置AutofacServieHost.Container静态属性......"但我想我正在设置它!?!我究竟做错了什么?

  protected void Application_Start(object sender, EventArgs e)
    {
        var builder = new ContainerBuilder();

        builder.Register(c => new DatabaseFactory()).As<IDatabaseFactory>().Named<DatabaseFactory>("DBFactory");
        builder.Register(c => new ListingSqlRepository(c.ResolveNamed<DatabaseFactory>("DBFactory"))).As<IListingSqlRepository>().Named<ListingSqlRepository>("LSR");
        builder.Register(c => new ListingRepository(c.ResolveNamed<ListingSqlRepository>("LSR"))).As<IListingRepository>().Named<ListingRepository>("LR");
        builder.Register(c => new Service1(c.ResolveNamed<IListingRepository>("LR"))).As<IService1>();

        using (var container = builder.Build())
        {
            Uri address = new Uri("http://localhost:57924/Service1");
            ServiceHost host = new ServiceHost(typeof(Service1), address);

            host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), string.Empty);

            host.AddDependencyInjectionBehavior<IService1>(container);

            //BREAKS HERE?
            host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpGetUrl = address });
            host.Open();

            Console.WriteLine("The host has been opened.");
            Console.ReadLine();

            host.Close();
            Environment.Exit(0);
        }

    }
Run Code Online (Sandbox Code Playgroud)

然后是SERVICE:namespace LOTW2012.WS {

public …
Run Code Online (Sandbox Code Playgroud)

wcf autofac

4
推荐指数
1
解决办法
4004
查看次数

标签 统计

autofac ×1

wcf ×1