简体   繁体   English

Ninject .Net 3.5 SOAP Web服务问题

[英]Ninject .Net 3.5 SOAP Webservice issues

this is my first time posting up here and trust me I have searched high and low for an answer to my question but have had very little success. 这是我第一次在这里发布信息,请相信我,我在高空搜寻我的问题的答案,但收效甚微。

Background: I have currently started trying to re-factor our existing SOAP web service (.Net 3.5) in order to do some IOC and DI using Ninject. 背景:我目前已经开始尝试重构现有的SOAP Web服务(.Net 3.5),以便使用Ninject进行一些IOC和DI。 I have a CacheManager which I am trying to initalize in the web method, however the injection does not seem to kick in. 我有一个CacheManager,正在尝试在Web方法中启动它,但是注入似乎没有开始。

I have an console application that calls the webservice with the below: 我有一个控制台应用程序,该应用程序使用以下命令调用网络服务:

static void Main(string[] args)
    {
        TestService service = new CachingService.TestService();

        DataResult result = service.GetSomething(1);
    }

The webservice is below: TestService.asmx.cs 该Web服务如下: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 inherits Base to initialize CacheManager) Base.cs(TestService.asmx.cs继承Base来初始化CacheManager)

public class Base
{
    [Inject]
    public ICacheManager CacheManager
    {
        get
        {
            if (cacheProxy == null)
            {
                cacheProxy = new CacheProxy();
            }
            return cacheProxy.CacheManager;
        }
    }  
}

CacheProxy.cs CacheProxy.cs

public class CacheProxy
{
    [Inject]
    public ICacheManager CacheManager { get; set; }
}

CacheManager.cs CacheManager.cs

public class CacheManager : ICacheManager
{
   //implements iCacheManager methods
}

App_Start/NinjectWebCommon.cs App_Start / NinjectWebCommon.cs

private static void RegisterServices(IKernel kernel)
    {
kernel.Bind<ICacheManager>()
            .ToMethod(x => x.Kernel.Get<ICacheManagerFactoryBuilder>().GetCacheManagerFactory().CreateCacheManager())
            .InRequestScope();
}

CacheManagerFactoryBuilder.cs CacheManagerFactoryBuilder.cs

public class CacheManagerFactoryBuilder : ICacheManagerFactoryBuilder
{
    private ICacheManagerFactory _Factory;

    public CacheManagerFactoryBuilder(ICacheManagerFactory factory)
    {
        _Factory = factory;
    }

    public ICacheManagerFactory GetCacheManagerFactory()
    {
        return _Factory;
    }
}

CacheManagerFactory.cs 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;
    }

}  

Everytime I run the console application and it hits GetSomething , CacheManager is null. 每次我运行控制台应用程序并点击GetSomething时CacheManager为null。 Why is it that the injection is not happening when I do a call to the web method? 为什么在我调用Web方法时没有进行注入?

Another member of the team eventually stumbled across this in another thread here: 团队的另一名成员最终在另一个线程中偶然发现了这一点:

How can I implement Ninject or DI on asp.net Web Forms? 如何在asp.net Web窗体上实现Ninject或DI?

All I was missing was inheriting WebServiceBase on my TestService web service class! 我所缺少的只是在TestService Web服务类上继承WebServiceBase!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM