Ninject: Injection of dependency IntPtr into parameter method of constructor

一笑奈何 提交于 2019-12-11 20:16:12

问题


I got help earlier with this question:

Ninject: Error Activating Strings

@nemensv solved that one but I immediatley got a new exception regarding IntPtr. (see activation path below).

Been looking through the ctor here

Only place IntPtr shows up is in this line:

 Result r = Platform.SQLiteApi.Open(databasePathAsBytes, out handle, (int)openFlags, IntPtr.Zero);

How can I solve this using ninject?

Complete exception:

Activation path:

  6) Injection of dependency IntPtr into parameter method of constructor

  5) Injection of dependency BlobSerializerDelegate+SerializeDelegate into parameter serializeDelegate of constructor

  4) Injection of dependency IBlobSerializer into parameter serializer of constructor

  3) Injection of dependency SQLiteConnection into parameter connection of constructor

  2) Injection of dependency ICarRepository into parameter carRepository of constructor

  1) Request for MainViewModel

My code in Ninject:

 Bind<SQLiteConnection>()
                .ToSelf()
                .WithConstructorArgument("databasePath", path);
            Bind<ISQLitePlatform>().To<SQLitePlatformWinRT>();
            Bind<IBlobSerializer>().To<BlobSerializerDelegate>();

Thank you!

EDIT:

This worked for me:

Bind<SQLiteConnection>().ToMethod(ctx => new SQLiteConnection(new SQLitePlatformWP8(), path));


回答1:


As ninject states it can't created an BlobSerializerDelegate because it doesn't know how to instanciated a BlobSerializerDelegate+SerializeDelegate. You either need to tell ninject how to created BlobSerializerDelegate - if that's even necessary - or tell it how to instanciate the SQLiteConnection. I think for the given situtation the ToMethod binding would be best:

Bind<SQLiteConnection>().ToMethod(ctx => new SQLiteConnection(...));

The ToMethod action should just new the SQLiteConnection as you would without a DI container.



来源:https://stackoverflow.com/questions/27727682/ninject-injection-of-dependency-intptr-into-parameter-method-of-constructor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!