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;
}));
答案 0 :(得分:0)
如果您想自己控制实例创建(这也可以让您有机会调用Connect),您可以使用带有委托(Func)的ComponentRegistration<T>.UsingFactoryMethod<T>
方法。