English 中文(简体)
ASMX 依赖性注射和IoC
原标题:ASMX Dependency Injection and IoC

在试图获得我的<代码>asmx网络服务时,我一直 st忙使用依赖注射,并利用IoC这样做。 我希望我的网络服务能够利用我的内部业务层服务。 网络服务将由来自不同领域的外部客户使用,主要用于发送和接收有关实体的信息,如订单和客户。

例如:

public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
        return new MyBusinessService().MyMethod();
    } 
}

public class MyBusinessService : IMyBusinessService
{
    public string MyMethod()
    {
        return "hello";
    }
}

我想利用依赖性注射来消除“新”我服务的必要性,但我可以表明这样做的方法。 我可以让它利用穷人的人类发展指数开展工作。

与此类似:

public class MyService : System.Web.Services.WebService
{
    private IMyBusinessService _myService;

    public MyService(IMyBusinessService myService)
    {
        _myService = myService;
    }

    public MyService() : this(new MyBusinessServie()) { }

    [WebMethod]
    public string HelloWorld()
    {
        return _myService.MyMethod();
    }
}

But I can t work out how to use a IoC container to inject my dependencies because I can t get the service to run without a parameterless constructor. I have just started to test dependency injection and got it to work fine on my windows forms application with StructureMap but got stuck on this one.

问题回答

不幸的是,没有任何办法用伙伴关系中的网络服务进行建筑注射。 NET。 这是协会的要求。 NET 您提供违约构造。 我的建筑商即将接近你能够利用这种网络服务的构成基础,而没有使用DI集装箱。

有了ASP.NET,具有多重组成根源并非罕见。 哪些是各网络服务和网页的构造者。 如果你使用协会。 NET MVC是比较友好的主计长Factory。

在你执行之后,从网络服务中搬出物体图表的重要部分,因为这是你的组成基础。 要做的主要工作是使网络服务尽可能少,保持对依赖性的大多数逻辑,以便能够测试或再利用。 从吉卜赛人基金会负责人那里收集信息,就是网络服务机构可以向依赖者传递这一信息的一个实例。

http://rads.stackoverflow.com/amzn/click/com/1935182501"rel=“noreferer” Dependency Injection in .NET by Mark Seemann.

如果贵国的网络服务实施了<代码>System.Web.IHttpHandler,而不是从System.Web.services.Webservices衍生。 您可以执行国际移民政策。

Global.ashx.cs

public class Global : HttpApplication
{
    protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        var context = ((HttpApplication)sender).Context;

        var needsMyBusinessService = context.Handler as INeedMyBusinessService;
        if (needsMyBusinessService != null)
            needsMyBusinessService.MyBusinessService = new MyBusinessService();
    }
}

Myservice.ashx.cs

public class MyService : IHttpHandler, INeedMyBusinessService
{
    public IMyBusinessService MyBusinessService { get; set; }

    public bool IsReusable { get { return true; } }

    public void ProcessRequest(HttpContext context)
    {
        // uses my dependency
    }
}

INeedMyBusiness Services.cs

public interface INeedMyBusinessService
{
    IMyBusinessService MyBusinessService { get; set; }
}

However the catch with this implementation is that is does not work with web services that implement System.Web.Services.WebService as the web service object is not initialized until after the PreRequestHandlerExecute event is call, which is the last event before ProcessRequest is called.

如果你希望每个网络服务有一个独特的榜样,上述例子就发挥了作用。 如果你希望每个网络服务申请采用<代码>MyBusiness Service的同一案例(Singleton liferix)的话,你就可以执行全球文件。

public class Global : HttpApplication
{
    private static IMyBusinessService businessService;

    protected void Application_Start(object sender, EventArgs e)
    {
        Global.businessService = new MyBusinessService();
    }

    protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        var context = ((HttpApplication)sender).Context;

        var needsMyBusinessService = context.Handler as INeedMyBusinessService;
        if (needsMyBusinessService != null)
            needsMyBusinessService.MyBusinessService = Global.businessService;
    }
}

You can only have a parameter-less constructor on a WebService. However, what you could do, is have a property on the WebService class, and inside of the parameter-less constructor call something like _myDependency = container.resolve();

This is an old question and unlikely to help very many people, but you basically need IDependencyResolver and IDependencyScope implementations that when ServiceProvider::GetService() fails to return an instance of a requested type, the type is explicitly instanced (ideally using the container to resolve ctor parameters.)

这种类型可以是“直截了当”的思考,也可以是使用现代技术,如<条码>。

By default, when the dependency resolver does not provide an instance of a requested type for a controller, page, module (webservice), etc the runtime will attempt to use the de facto Activator class to create an instance, and Activator can only create instances of types which have a public, parameterless constructor.

因此,可以说,你的类型需要一个公共的、无足轻重的机器人。 如果您无法/愿意制定<代码>IDependencyResolver/IDependencyScope 执行。 习俗依赖性解决者是执行工作的三边事,而MSDN有基本执行的文章和实例。

唯一需要的是,有类型注册的明显国际移民交易箱和在业登记注册的“IDetusResolver > 。 该网利用了贵国的DI箱。 今天,这可能会有以下几点:www.c.org/t.extensions.DeistencyInmission /index.html。 储存和整合一个DI 箱,是超出Q &A范围的一个范围,可能还有其他的Q & 已有的涵盖范围。 我将指出,如果你重新建立自己的国际独立制度,你也可以选择采用更现代的DI框架(如在伙伴关系中采用的框架)。 尽管你可能使用协会,但网络核心信条。 2000年代的网络版本。 依赖性决心是这方面的关键。

另见:

HTH!





相关问题
How to structure files / dependencies in Visual C++ project

This is more of a design question than a C++ question. I m working on a Texas Hold Em poker game in C++. So far, I have a HandChecker module written that is responsible for determining a player s ...

draw dependency graph for a java class

Heyho, I m searching for a tool like JDepend to draw a graph for a java classfile. JDepend seams to be fine, but it s not resolving the deps from the deps (maybe I m just missing some special options?)...

How to use the -MG flag in cc 4.4.1?

I ve recently upgraded my Ubuntu installation from Jaunty to Karmic. This apparently includes an update of the GNU C compiler, because code that compiled previously no longer does. Running cc 4.4.1 (...

How to manually disable/blacklist Maven repository

In my base project I use dependency of JasperReports which has non-existent repository declaration in its pom. When I run every Maven commad there is dependency looking for commons-collection in this ...

Batch Job Dependencies Using Open Source/Free Software

I run a large data warehouse plant where we have a lot of nightly jobs running concerruently however many have dependencies on a extract or data load process before they start. Currently we use an ...

热门标签