所以我在尝试让我的asmx webservice使用依赖注入和使用IoC来实现时遇到了困难.我希望我的webservice能够使用我的内部业务层服务.Web服务将由来自不同域的外部客户端使用,主要用于发送和接收有关订单和客户等实体的信息.
一个例子是:
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return new MyBusinessService().MyMethod();
}
}
public class MyBusinessService : IMyBusinessService
{
public string MyMethod()
{
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用依赖注入来消除"新"我的服务的需要,但我无法找到一种方法来做到这一点.我可以使用穷人的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();
}
}
Run Code Online (Sandbox Code Playgroud)
但我简直无法理解如何使用IoC容器来注入我的依赖项,因为我无法让服务在没有无参数构造函数的情况下运行.请善待,我不是一个有经验的程序员和刚开始测试的依赖注入,并得到它的工作在我的Windows罚款structuremap形成的应用,但就死在这一个.