containerBuilder
.Register<IGraphClient>(context =>
{
var graphClient = new GraphClient(new Uri("http://localhost:9999/db/data"));
graphClient.Connect(); // Particularly this line
return graphClient;
})
.SingleInstance();
虽然我可以弄清楚如何将接口注册到具体类,但这个特定的类需要是一个实例(我很确定这是LifeStyle.Singleton(,并且还调用graphClient.Connect((方法。这是我坚持的主要部分。
根据JeffN825的回答,我这样做了:
container.Register(
Component.For(
typeof (IGraphClient))
.ImplementedBy(typeof (GraphClient))
.LifeStyle.Singleton.UsingFactoryMethod(() =>
{
var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
graphClient.Connect();
return graphClient;
}));
如果您想
自己控制实例创建,则可以使用 ComponentRegistration<T>.UsingFactoryMethod<T>
方法,该方法需要一个委托 (Func((这也将使您有机会调用 Connect(。