过去2天我一直坚持这个问题,我想做的是阻止创建DisplayWeatherAstronomy的实例,我认为昨晚在Tadmas的帮助下修复了另一个线程,但我遇到了Ninject的问题。
所以我修改了我的代码,并使用接口将我的类转换为抽象类,但我仍然遇到Ninject的问题,我不知道如何解决它,即使错误看起来很自我解释。
我的错误是:
Ninject.ActivationException was unhandled by user code
HResult=-2146233088
Message=Error activating IDisplayWeatherAstronomy using binding from IDisplayWeatherAstronomy to GetWeatherAstronomyData
No constructor was available to create an instance of the implementation type.
Activation path:
2) Injection of dependency IDisplayWeatherAstronomy into parameter igwa of constructor of type WeatherController
1) Request for WeatherController
Suggestions:
1) Ensure that the implementation type has a public constructor.
2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
Source=Ninject
StackTrace:
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 82
at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:line 197
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:\Projects\Ninject\ninject\src\Ninject\Planning\Targets\Target.cs:line 165
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 114
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 96
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 96
at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType) in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectDependencyResolver.cs:line 56
at Castle.Proxies.Invocations.IDependencyResolver_GetService.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed()
at Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult)
at Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context)
at Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IDependencyResolverProxy.GetService(Type serviceType)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
InnerException:
代码:
public abstract class GetWeatherAstronomyData : IDisplayWeatherAstronomy
{
public class DisplayWeatherAstronomy
{
public string SunRise { get; internal set; }
public string SunSet { get; internal set; }
public string MoonRise { get; internal set; }
public string MoonSet { get; internal set; }
}
public IEnumerable<DisplayWeatherAstronomy> WeatherAstronomy(string id)
{
var doc = WeatherForcast.WeatherLocationSearchXmlDataAsync(id);
var result = XDocument.Parse(doc.Result);
var displayAstronomy = (from wd in result.Descendants("astronomy")
select new DisplayWeatherAstronomy
{
SunRise = (string)wd.Element("sunrise") ?? string.Empty,
SunSet = (string)wd.Element("sunset") ?? string.Empty
});
return displayAstronomy;
}
}
接口
public interface IDisplayWeatherAstronomy
{
IEnumerable<GetWeatherAstronomyData.DisplayWeatherAstronomy> WeatherAstronomy(string id);
}
Ninject
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IDisplayWeatherAstronomy>()
.To<GetWeatherAstronomyData>()
.InSingletonScope();
}
最后控制器
private readonly IDisplayWeatherAstronomy _igwa;
public WeatherController(IDisplayWeatherAstronomy igwa)
{
_igwa = igwa;
}
最后解决这个问题的任何帮助都将不胜感激
乔治
答案 0 :(得分:0)
您无法实例化抽象类。 Ninject也不能这样做。从abstract
删除GetWeatherAstronomyData
修饰符。
答案 1 :(得分:0)
如果您想阻止您的类被其他人实例化,请将其与界面一起放在“额外”项目/程序集B中,并将其设为internal
,而不是public
(不是abstract
,也不是!)。界面必须为public
。
然后还将ninject模块添加到同一个程序集中,从而创建绑定:
public class WeatherModule : NinjectModule
{
this.Bind<IDisplayWeatherAstronomy>().To<DisplayWeatherAstronomy>();
}
然后,在原始项目中,添加对项目/程序集B的引用并加载WeatherModule
:
kernel.Load<WeatherModule>();
就是这样。
但请记住,仍有人可以使用
创建课程IResolutionRoot.Get<IDisplayWeatherAstronomy>();
所以如果你可以限制对内核的访问,你只能获得一些东西,这通常是不切实际的。