为什么CastleWindsor的BeginScope超出范围? [英] Why is CastleWindsor's BeginScope out of scope?

查看:668
本文介绍了为什么CastleWindsor的BeginScope超出范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想温莎城堡添加到我的Web API项目,是跟着<一个href=\"http://blog.kerbyyoung.com/2013/01/setting-up-castle-windsor-for-aspnet.html#comment-form\">this帖子,但我在这条线code的得到一个编译时错误:

I'm trying to add Castle Windsor to my Web API project, and am following this post, but am getting a compile-time error on this line of code:

this._scope = container.BeginScope();

...为Castle.Windsor.IWindsorContainer'不包含'BeginScope的定义,并没有扩展方法'BeginScope接受型Castle.Windsor.IWindsorContainer'的第一个参数可以发现(是否缺少using指令或程序集引用?)

下面是整个code,以便它可以在上下文中可以看出:

Here is the entire code so that it can be seen in context:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Dependencies;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using System.Web.Http;

namespace PlatypiPieServer
{
    public class WindsorDependencyResolver : IDependencyResolver
    {
        private readonly IWindsorContainer _container;

        public WindsorDependencyResolver(IWindsorContainer container)
        {
            _container = container;
        }

        public IDependencyScope BeginScope()
        {
            return new WindsorDependencyScope(_container);
        }

        public object GetService(Type serviceType)
        {
            if (_container.Kernel.HasComponent(serviceType))
                return this._container.Resolve(serviceType);
            else
                return null;
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.ResolveAll(serviceType).Cast<object>();
        }

        public void Dispose()
        {
            _container.Dispose();
        }
    }

    public class WindsorDependencyScope : IDependencyScope
    {
        private readonly IWindsorContainer _container;
        private readonly IDisposable _scope;

        public WindsorDependencyScope(IWindsorContainer container)
        {
            this._container = container;
            this._scope = container.BeginScope();
        }

        public object GetService(Type serviceType)
        {
            if (_container.Kernel.HasComponent(serviceType))
                return _container.Resolve(serviceType);
            else
                return null;
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return this._container.ResolveAll(serviceType).Cast<object>();
        }

        public void Dispose()
        {
            this._scope.Dispose();
        }
    }

    public class ApiControllersInstaller : IWindsorInstaller
    {
        public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {
            container.Register(Classes.FromThisAssembly()
             .BasedOn<ApiController>()
             .LifestylePerWebRequest());
        }
    }
}

在哪里BeginScope?它已经被撤销precated?

Where is BeginScope? Has it been deprecated?

推荐答案

这是一个扩展方法。您需要导入 Castle.MicroKernel.Lifestyle 命名空间。

It's an extension method. You need to import the Castle.MicroKernel.Lifestyle namespace.

这篇关于为什么CastleWindsor的BeginScope超出范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆