必须先设置AutofacServiceHost.Container静态属性,然后才能实例化服务. [英] The AutofacServiceHost.Container static property must be set before services can be instantiated

查看:48
本文介绍了必须先设置AutofacServiceHost.Container静态属性,然后才能实例化服务.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的XXXX.WS WCF服务项目中,我正在尝试使用autofac来获取DI/IOC.我不明白如何动摇...必须设置AutofacServieHost.Container静态属性..." ..但是我想我正在设置它!我在做什么错了?

Within my XXXX.WS WCF services project I'm trying to get DI/IOC using autofac going...been at it all day but I think I'm close (different errors are progress here)...this error I can't understand how to shake..."AutofacServieHost.Container static property must be set..."..but I think I am setting it!?! What am I doing wrong?

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

    }

然后,服务:命名空间LOTW2012.WS{

Then the SERVICE: namespace LOTW2012.WS {

public class Service1 : IService1
{
    private IListingRepository _listingRepository { get; set; }

    public Service1(IListingRepository iLR) {
        this._listingRepository = iLR;
    }

    public Service1()
    {
    }


    public List<Listing> GetListingsByStateName(string stateName)
    {   
        //todo ..getall for now
        var listings = _listingRepository.GetAll().ToList();

        return listings;
    }

推荐答案

您需要通过设置有问题的属性来告诉Autofac WCF集成有关所构建容器的信息:

You need to tell the Autofac WCF integration about the container you build by setting the property in question:

var builder = new ContainerBuilder();

// ...

AutofacHostFactory.Container = builder.Build();

// ...

这将允许Autofac解析服务类型.

This will allow Autofac to resolve service types.

这篇关于必须先设置AutofacServiceHost.Container静态属性,然后才能实例化服务.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆