这是我第一次在这里发帖,相信我,我已经从高处和低处搜索了我的问题的答案,但收效甚微。
背景:我目前已经开始尝试重构我们现有的 SOAP Web 服务(.Net 3.5),以便使用 Ninject 进行一些 IOC 和 DI。我有一个 CacheManager,我试图在 web 方法中初始化它,但是注入似乎没有启动。
我有一个控制台应用程序,它使用以下内容调用 Web 服务:
static void Main(string[] args)
{
TestService service = new CachingService.TestService();
DataResult result = service.GetSomething(1);
}
网络服务如下:TestService.asmx.cs
[WebMethod(Description = "Get something")]
public DataResult GetSomething(int param)
{
try
{
return this.CacheManager.Get();
}
catch (Exception ex)
{
throw;
}
}
Base.cs(TestService.asmx.cs继承Base初始化CacheManager)
public class Base
{
[Inject]
public ICacheManager CacheManager
{
get
{
if (cacheProxy == null)
{
cacheProxy = new CacheProxy();
}
return cacheProxy.CacheManager;
}
}
}
缓存代理.cs
public class CacheProxy
{
[Inject]
public ICacheManager CacheManager { get; set; }
}
缓存管理器.cs
public class CacheManager : ICacheManager
{
//implements iCacheManager methods
}
App_Start/NinjectWebCommon.cs
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ICacheManager>()
.ToMethod(x => x.Kernel.Get<ICacheManagerFactoryBuilder>().GetCacheManagerFactory().CreateCacheManager())
.InRequestScope();
}
CacheManagerFactoryBuilder.cs
public class CacheManagerFactoryBuilder : ICacheManagerFactoryBuilder
{
private ICacheManagerFactory _Factory;
public CacheManagerFactoryBuilder(ICacheManagerFactory factory)
{
_Factory = factory;
}
public ICacheManagerFactory GetCacheManagerFactory()
{
return _Factory;
}
}
CacheManagerFactory.cs
public class CacheManagerFactory : ICacheManagerFactory
{
private readonly ICacheManager Manager;
public CacheManagerFactory(ICacheManager manager)
{
if (this.Manager == null)
{
this.Manager = manager;
}
}
public ICacheManager CreateCacheManager()
{
return this.Manager;
}
}
每次我运行控制台应用程序并点击GetSomething时,CacheManager为空。为什么当我调用 web 方法时没有发生注入?