我使用本教程在我的解决方案中创建插件架构,并且我也是第一次使用 ninject:
现在,在 MVC 应用程序中,当用户正在结帐过程中时,我会获取他选择的付款方式,并且需要检索所选付款方式的插件。我已经成功地以这种方式检索插件 Controller ,尽管我不知道它是否安全或可接受的做法:
Type type = Type.GetType(paymentMethod.PaymentMethodPluginType);
//get plugin controller
var paymentController = ServiceLocator.Current.GetInstance(type) as BasePaymentController;
//get validations from plugin
var warnings = paymentController.ValidatePaymentForm(form);
//get payment info from plugin
var paymentInfo = paymentController.GetPaymentInfo(form);
//…
我还需要访问一个插件类来处理付款。 我有一个接口(interface) IPaymentMethod
public partial interface IPaymentMethod
{
void PostProcessPayment (PostProcessPaymentRequest postprocessPaymentRequest);
}
以及像这样的插件 PaymentProcessor
public class PluginPaymentProcessor :IPaymentMethod
{
public void PostProcessPayment (PostProcessPaymentRequest postprocessPaymentRequest)
{
///
}
Now in MVC project I try to access PostProcessPayment method this way
IPaymentMethod pluginpaymentmethod = ServiceLocator.Current.GetInstance<IPaymentMethod>(paymentMethod.PaymentProcessor);
这里 paymentMethod.PaymentProcessor 是“MyApp.Plugins.MyPlugin.PluginPaymentProcessor, MyApp.Plugins.MyPlugin,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”
And want to use pluginpaymentmethod like i do in controller example
pluginpaymentmethod.PostProcessPayment(postProcessPaymentRequest);
但它抛出错误,资源未找到并且插件支付方法未加载。我该如何修复它,或者您可以建议任何具有类似实现的教程吗?谢谢。
最佳答案
假设您有一个名为 MyPlugin
的具体类,它具有 IPaymentMethod
接口(interface),那么您的 ninject 绑定(bind)应该类似于:
private static void RegisterServices(IKernel kernel){
kernel.Bind<IPaymentMethod>().To<MyPlugin>().InRequestScope();
}
检查它是否位于 App_Start
文件夹下的 NinjectWebCommon.cs
类中。一个更棘手的场景可能是 IPaymentMethod
必须以与绑定(bind) Ninject IKernel
相同的方式注册:
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
这可能是一个更棘手的问题。
关于asp.net-mvc-3 - 具有 ninject 的插件架构 - 将类和 Controller 实例从插件程序集加载到主 MVC 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11672794/