我写了一个虚拟程序,在下面实现了Windsor城堡。但我的问题是我想在我的WEB CONFIG中编写它的组件,但我不知道在哪里写什么...可以任何人帮助我在纠正那个
请更正我写的是App.config的行为级别,即使其可配置??? 虚拟程序:
namespace NnjectFramework
{
class Program
{
#region windsor castle
interface IFoo
{
void test();
void test2();
}
public class Foo: IFoo
{
private readonly string _arg1;
private readonly string _arg2;
public Foo(string arg1, string arg2)
{
_arg1 = arg1;
_arg2 = arg2;
}
public void test()
{
Console.WriteLine("Success method 1");
}
public void test2()
{
Console.WriteLine("Success method 2");
}
}
class Bar
{
private Foo bar;
public Bar(Foo bar)
{
this.bar = bar;
}
}
static void Main(string[] args)
{
IWindsorContainer container = new WindsorContainer();
container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Is(LifestyleType.Transient).Named("AFooNamedFoo"));
IFoo foo = container.Resolve<Foo>("AFooNamedFoo", new { arg1 = "hello", arg2 = "world" });
foo.test();
foo.test2();
Console.ReadKey();
}
#endregion
}
}
我在App.config中尝试了什么:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<components>
<component id="uniqueId"
service="Acme.Crm.Services.INotificationService, Acme.Crm"
type="Acme.Crm.Services.EmailNotificationService, Acme.Crm"
inspectionBehavior="all|declaredonly|none"
lifestyle="transient"
customLifestyleType="type that implements ILifestyleManager"
componentActivatorType="type that implements IComponentActivator"
initialPoolSize="2" maxPoolSize="6"></component>
</components></configuration>
答案 0 :(得分:1)
虚拟应用程序(modyfied):
Console.Read()
和app.config:
using System;
using Castle.Windsor;
namespace StackOwerflow
{
public class Program
{
#region windsor castle
interface IFoo
{
void test();
void test2();
}
public class Foo : IFoo
{
private readonly string _arg1;
private readonly string _arg2;
public Foo(string arg1, string arg2)
{
_arg1 = arg1;
_arg2 = arg2;
}
public void test()
{
Console.WriteLine("Success method 1");
}
public void test2()
{
Console.WriteLine("arg1: {0}. arg2: {1}", _arg1, _arg2);
}
}
class Bar
{
private Foo foo;
public Bar(Foo foo)
{
this.foo = foo;
}
}
static void Main(string[] args)
{
IWindsorContainer container = new WindsorContainer();
container.Install(Castle.Windsor.Installer.Configuration.FromAppConfig());
IFoo foo = container.Resolve<Foo>("AFooNamedFoo");
foo.test();
foo.test2();
Console.ReadKey();
}
#endregion
}
}
我的程序集名称是StackOwerflow,因此您应该将其修改为程序集名称。