Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 温莎城堡重建实例_C#_Wcf_Dependency Injection_Castle Windsor - Fatal编程技术网

C# 温莎城堡重建实例

C# 温莎城堡重建实例,c#,wcf,dependency-injection,castle-windsor,C#,Wcf,Dependency Injection,Castle Windsor,我试图在每次WCF调用后处理我的所有业务逻辑服务,并试图通过使用windsor实现它(Ninject只是不起作用),我有以下容器类: public class LDSWindsorContainer : WindsorContainer { public LDSWindsorContainer() { AddFacility<WcfFacility>(). Register(

我试图在每次WCF调用后处理我的所有业务逻辑服务,并试图通过使用windsor实现它(Ninject只是不起作用),我有以下容器类:

    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());
        }
    }
我的服务的构造函数如下所示:

            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    



到目前为止,问题是:我的实例只解决了一次(第一次调用),工作正常,在调用结束时被释放,并且没有为所有即将到来的调用重新实例化,所以基本上我只是得到了一个处理释放对象的例外,任何想法都非常感谢,提前谢谢你。

好吧,经过一番努力,我终于在这里找到了一个完美的答案-

我的容器设置:

     AddFacility<TypedFactoryFacility>().Register(Component.For<IDomainService>().ImplementedBy<DomainService().LifeStyle.PerWcfOperation(),

     AddFacility<WcfFacility>().Register(Component.For<IDirectoryService>().ImplementedBy<DirectoryService>().LifestyleSingleton());

AddFacility().Register(Component.For()).ImplementedByYour目录服务配置为单例,但插入的依赖项是按请求配置的,因此我认为这是正常行为。您需要按web请求设置目录服务生活方式,或者将其他服务设置为单例。不幸的是,在这种特殊情况下,我无法更改e服务的行为,但需要利用每请求DI,所以我仍然在这里寻找一个选项当你说你不能改变行为时,你的意思是你不能改变服务的生活方式吗?目录服务也需要是单例的吗?是的,我的IDirectoryService应该是单例的,只有单例的,长的简而言之,这是一个我不允许改变的行为,其他任何事情-没有问题,但你说你需要利用每个请求的生活方式?为什么?每个调用方都需要他们自己的依赖实例吗?可能每个依赖都有一个?一个有趣的解决方案,但似乎你在嵌入知识/假设IoC/DI通常是关于解耦依赖项的,而此解决方案在允许类型替换的同时,使事情紧密耦合。例如,如果某个依赖项的生命周期从perwcfooperation更改为Transient,则您的服务可能会所有这些东西都不会变为暂时性的,因为它的目的是在每次请求后处理服务,这样我的数据库上下文查询就不会被缓存。你正在处理Mark Seemann所说的捕获依赖性问题:是的,你的解决方案可以工作,但非常脆弱,让我想知道你在添加什么价值对于以后维护这段代码的人来说,这里发生的事情似乎非常混乱。
 <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" >
          <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
        </modules>
 </system.webServer>
     AddFacility<TypedFactoryFacility>().Register(Component.For<IDomainService>().ImplementedBy<DomainService().LifeStyle.PerWcfOperation(),

     AddFacility<WcfFacility>().Register(Component.For<IDirectoryService>().ImplementedBy<DirectoryService>().LifestyleSingleton());
    public DirectoryService(Func<IDomainService> domainService)
    {
        DomainServiceFactory = domainService;
    }
    private Func<IDomainService> DomainServiceFactory { get; set; }

    private IDomainService DomainService
    {
        get { return DomainServiceFactory(); }
    }