【问题标题】:Why is this Castle Windsor DI code failing to compile?为什么这个 Castle Windsor DI 代码编译失败?
【发布时间】:2013-12-31 07:39:10
【问题描述】:

这段代码:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(AllTypes.FromThisAssembly()      
                  //AllTypes.FromAssemblyNamed("Acme.Crm.Data")
                  .Where(type => type.Name.EndsWith("Repository"))
                  .WithService.DefaultInterfaces()
                  .Configure(c => c.LifeStyle.PerWebRequest));
}

...从官方文档here 派生/改编,失败了,“只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句 em>"

我犯了什么错误?还是官方文档误导了我?

更新

应西蒙·怀特黑德的要求,这是整个班级:

using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.MicroKernel.Lifestyle;

namespace HandheldServer.DIInstallers
{
    public class RepositoriesInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(//Classes.FromThisAssembly() 
                               //AllTypes.FromAssemblyNamed("Acme.Crm.Data")
                               AllTypes.FromThisAssembly()
                                       .Where(type => type.Name.EndsWith("Repository"))
                                       .WithService.DefaultInterfaces()
                                        //.Configure(c => c.LifeStyle.PerWebRequest));
                                       .Configure(c => c.LifeStyle.PerWebRequest));
        }
    }
}

【问题讨论】:

  • 看起来不错。问题可能出在文件中的其他地方。你能告诉我们整个源文件吗?
  • 好的,不过就是这样。

标签: c# dependency-injection asp.net-web-api castle-windsor asp.net-web-api-routing


【解决方案1】:

我认为又是文档。 Configure 接受一个 Action,它是一个应该返回 void 的函数,而这个 lambda 正试图返回一个值。

使用

.Configure(c => c.LifestylePerWebRequest())

或者,由于您似乎不打算为不同的组件使用不同的生活方式,您可以直接应用生活方式而不使用 Configure:

.Where(type => type.Name.EndsWith("Repository"))
.WithService.DefaultInterfaces()
.LifestylePerWebRequest());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2019-07-08
    相关资源
    最近更新 更多