我試圖在每次WCF調用後嘗試使用windsor來實現所有業務邏輯服務(Ninject只是沒有工作),我有以下容器類:Castle Windsor重新創建實例
public class LDSWindsorContainer : WindsorContainer
{
public LDSWindsorContainer()
{
AddFacility<WcfFacility>().
Register(
Component.For<IDomainService>().ImplementedBy<DomainService>().LifeStyle.PerWcfOperation(),
Component.For<IChangeLogService>().ImplementedBy<ChangeLogService>().LifeStyle.PerWcfOperation(),
Component.For<ILogService>().ImplementedBy<LogService>().LifeStyle.PerWcfOperation(),
Component.For<ICentreInformationService>()
.ImplementedBy<CentreInformationService>()
.LifeStyle.PerWcfOperation().OnCreate(x=>Debug.WriteLine("Created")),
Component.For<ICentreService>().ImplementedBy<CentreService>().LifeStyle.PerWcfOperation(),
Component.For<ITimeZoneService>().ImplementedBy<TimeZoneService>().LifeStyle.PerWcfOperation(),
Component.For<IBrandService>().ImplementedBy<BrandService>().LifeStyle.PerWcfOperation(),
Component.For<IPricingService>().ImplementedBy<PricingService>().LifeStyle.PerWcfOperation(),
Component.For<IDirectoryService>()
.ImplementedBy<DirectoryService>().LifestyleSingleton());
}
}
之外的所有IDirectoryService及其實施的對象是我的商業邏輯服務,最後一個是一個WCF服務本身,我有以下的Application_Start()方法:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
var container = new LDSWindsorContainer();
...
}
}
和我的服務的構造函數如下所示:
private ILogService Logger { get; set; }
private IDomainService DomainService { get; set; }
private ICentreInformationService CentreInformationService { get; set; }
private ICentreService CentreService { get; set; }
private ITimeZoneService TimeZoneService { get; set; }
private IBrandService BrandService { get; set; }
private IPricingService PricingService { get; set; }
public DirectoryService(IBrandService brandService, ITimeZoneService timeZoneService, ICentreService centreService, ICentreInformationService centreInformationService, IDomainService domainService, ILogService logger, IPricingService pricingService)
{
BrandService = brandService;
TimeZoneService = timeZoneService;
CentreService = centreService;
CentreInformationService = centreInformationService;
DomainService = domainService;
Logger = logger;
PricingService = pricingService;
}
我也有我的DirectoryService.svc和Web.config中對應的線 -
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration
和
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" >
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
</modules>
</system.webServer>
到目前爲止,問題是:我的情況越來越下不爲例(對於第一個調用),工作正常,處理通話結束,並沒有重新實例化所有即將到來的調用,所以基本上我只是得到一個例外與處理對象的工作,任何想法非常讚賞,第提前和你聯繫。
目錄服務被配置爲單身,但在每個請求配置注入的依賴關係,所以這是我認爲的正常行爲。您需要根據網絡請求製作您的目錄服務生活方式,或者讓您的其他服務單身。 – 2014-12-19 12:54:24
不幸的是,在這種特殊情況下,我不能改變服務的行爲,但需要利用每個請求DI的優勢,所以我仍然在這裏尋找一個選項 – HardLuck 2014-12-19 12:57:45
當你說你不能改變行爲時,你的意思是你不能改變服務的生活方式?目錄服務也需要是單身人士嗎? – 2014-12-19 13:01:52