我使用本教程在我的解决方案中创建插件架构,我也是第一次使用ninject:
现在,在用户处于结账过程中的MVC应用程序中,我获得了他选择的付款方式,需要检索所选付款方式的插件。我成功地以这种方式检索插件控制器,虽然我不知道它是安全的还是可接受的练习:
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);
//…
我还需要访问一个插件类来处理付款。 我有一个接口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);
但它会抛出错误,找不到资源并且未加载pluginpaymentmethod。我该如何修复它,或者你可以建议任何类似实现的教程吗?谢谢。
答案 0 :(得分:2)
假设你有一个名为MyPlugin
的具体类,它有IPaymentMethod
接口,那么你的ninject绑定看起来应该有点像:
private static void RegisterServices(IKernel kernel){
kernel.Bind<IPaymentMethod>().To<MyPlugin>().InRequestScope();
}
检查NinjectWebCommon.cs
文件夹下的App_Start
课程中是否存在此问题。更棘手的情况可能是IPaymentMethod
必须以与Ninject IKernel
绑定相同的方式注册:
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
这可能是一个棘手的问题。