我有组件:
public interface IFoo
{ }
public interface IBar
{ }
public class Foo : IFoo
{
public IBar Bar { get; set; }
}
public class Bar : IBar
{
public IFoo Foo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有Castle-Windsor配置:
Container.AddComponent("IFoo", typeof (IFoo), typeof (Foo));
Container.AddComponent("IBar", typeof (IBar), typeof (Bar));
Run Code Online (Sandbox Code Playgroud)
和单元测试失败:
[Test]
public void FooBarTest()
{
var container = ObjFactory.Container;
var foo = container.Resolve<IFoo>();
var bar = container.Resolve<IBar>();
Assert.IsNotNull(((Foo) foo).Bar);
Assert.IsNotNull(((Bar) bar).Foo);
}
Run Code Online (Sandbox Code Playgroud)
它失败了,因为循环依赖,"foo".Bar或"bar".Foo为null.如何配置Castle以正确初始化两个组件?
我可以手动正确初始化两个组件:
[Test]
public void FooBarTManualest()
{ …Run Code Online (Sandbox Code Playgroud) methodX()这两个片段中的执行是否不同?
SemaphoreSlim _locker、.Wait()、 和WaitAsync()是具有同步和异步版本的同一方法的示例:
A:
SemaphoreSlim _locker = new SemaphoreSlim(1);
async Task methodX()
{
_locker.Wait();
// .. rest of the code
}
Run Code Online (Sandbox Code Playgroud)
乙:
SemaphoreSlim _locker = new SemaphoreSlim(1);
async Task methodX()
{
await _locker.WaitAsync();
// .. rest of the code
}
Run Code Online (Sandbox Code Playgroud)