因此,我试图让我的asmx webservice使用依赖注入并使用IoC来实现它。我希望我的web服务能够使用我的内部业务层服务。 Web服务将由来自不同域的外部客户端使用,主要用于发送和接收有关实体(如订单和客户)的信息。ASMX依赖注入和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";
}
}
我想使用依赖注入,以消除“newing”我的服务的需要,但我不能想出一个办法做到这一点。我可以使用可怜的人DI进行工作,或者至少我认为它被称为“可怜的人”。
这样的:
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();
}
}
但我只是不能让我围绕着如何使用IoC容器注入我的依赖,因为我不能去无参数的构造函数运行服务头。 请友好,我不是一个有经验的程序员,并且刚开始测试依赖注入,并使它在结构映射的Windows窗体应用程序上正常工作,但陷入了这个问题。