我有一段時間試圖讓這個工作。WCF和Autofac w/Extensionless Services
我希望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();
}
}
}
你是否能夠步驟t hrough?任何錯誤?最後的容器裏有什麼? – EdmundYeung99
踩過它時,一切看起來都不錯。 COntainer構建,註冊在那裏,只是代碼不起作用! – Sam