簡體   English   中英

無法在autofac中注冊wcf

[英]cannot register wcf with autofac

我正在嘗試在六角形體系結構中向autofac注冊wcf服務,其中僅核心層引用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.Container靜態屬性,然后才能實例化服務。

請用這個指導我。

您是否在應用程序啟動時編寫了代碼來注冊依賴項,並將其與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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM