无法使用autoface注册WCF

本文关键字:注册 WCF autoface | 更新日期: 2023-09-27 18:03:26

我正在尝试注册wcf服务与autoface在六角形架构中,只有核心层引用UI层和所有其他层引用DI层。我在分享我到目前为止所做的事情。

Service.svc

<%@ ServiceHost Language="C#" Debug="true" Service="DemoApp.WebService.IAbcService, DemoApp.WebService"
    Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"
     CodeBehind="AbcService.svc.cs" %>

Service.svc.cs

public class AbcService : IAbcService
{
    private readonly IService<Person> _service;
    public AbcService(IService<Person> service)
    {
        _service = service;
    }
    public List<PersonDataContract> GetAllPerson()
    {
        var query = _service.GetAll().Distinct();
        List<PersonDataContract> studentList = new List<PersonDataContract>();
        query.ToList().ForEach(rec =>
        {
            studentList.Add(new PersonDataContract
            {
                PersonID = Convert.ToString(rec.Id),
                Age = Convert.ToString(rec.Age),
                Height = rec.Height,
                Name = rec.FullName,
                Sex = rec.Sex.ToString(),
                Nationality = rec.Nationality.ToString(),
                Weight = rec.Weight.ToString()
            });
        });
        return studentList;
    }
}

当我尝试将此服务层的引用添加到任何其他项目时,它会给出这个错误:

AutofacServiceHost。必须设置容器静态属性服务可以被实例化

无法使用autoface注册WCF

您是否在应用程序启动时编写了代码来注册依赖项并像这样将其与Autofac DI容器连接起来?

protected void Application_Start()
{
  var builder = new ContainerBuilder();
  // Register your service implementations.
  builder.RegisterType<TestService.Service1>();
  // Set the dependency resolver.
  var container = builder.Build();
  AutofacHostFactory.Container = container;
}

更多信息请访问http://autofac.readthedocs.org/en/latest/integration/wcf.html