Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

asp.net - Ninject w/ ASMX web service in a MVC3/Ninject 3 environment

I'm looking for the best way to incorporate a classic asmx web service into my MVC3 environment. I'd like the kernel/container to be shared between the two.

In the past with ASP.NET web forms I was able to use the following:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service : WebServiceBase
{
    [Inject]
    public IContextProvider ContextProvider { get; set; }
...

This used the older Ninject.Web to shared the kernel using the KernelContainer that was shared between WebServiceBase, PageBase, etc. etc.

Now I've got a MVC3 application using Ninject.Web.MVC and I need an older web service to function in the same space, using the same kernel/bindings. I haven't been able to find any information on getting a setup like this up and running.

Does anyone have any ideas, samples, or posts they can point me to (other than don't use asmx web services)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The MVC framework supports the DependencyResolver which integrates with Ninject for MVC. Although it is not possible to define a custom factory for ASMX web services, you could do the following:

public class Service : WebService
{
  public IContextProvider ContextProvider {get;set;}

  public Service()
  {
    ContextProvider = DependencyResolver.Current.GetService<IContextProvider>();
  }
}

While you cannot inject dependencies during construction of your service, you are able to resolve them yourself within the constructor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...