使用存储库和 UnitofWork 模式实现的 WCF 服务中的构造函数错误
我有一个使用存储库和工作单元模式实现的 WCF 服务。 现在我收到以下错误:
提供的服务类型无法作为服务加载,因为它没有默认(无参数)构造函数。要解决此问题,请向该类型添加默认构造函数,或将该类型的实例传递给主机。
当我在没有这些模式的情况下工作时,它没有抛出任何错误。 帮助 ??有什么建议吗?如何通过这个错误?
以下是代码片段:
public class Service : IService
{
private IUnitOfWork _unitOfWork;
private IMyRepository _myRepository;
// Dependency Injection enabled constructors
public Service(IUnitOfWork uow, IMyRepository myRepository)
{
_unitOfWork = uow;
_myRepository = myRepository;
}
}
I have a WCF service which implemented using Repository and UnitofWork patterns.
And now I am getting following error:
The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.
When I worked WIHTOUT these patterns it did not throw any error.
HELP ?? SUGGESTIONS? How to get passed this error?
Following is the code snippet:
public class Service : IService
{
private IUnitOfWork _unitOfWork;
private IMyRepository _myRepository;
// Dependency Injection enabled constructors
public Service(IUnitOfWork uow, IMyRepository myRepository)
{
_unitOfWork = uow;
_myRepository = myRepository;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用默认服务实例,则必须提供无参数构造函数。您的设计通过构造函数提供依赖注入。在这种情况下,您必须有自己的实例提供程序来调用构造函数并创建服务实例。您可以为每个服务创建实例提供程序, 行为和可选的服务主机 但这确实是糟糕的方式。更好的方法是使用控制反转容器,它将解决配置中的依赖关系。在这种情况下,您将只有一个新的实例提供程序、行为和可选的服务主机。
这里< /a> 你有一篇关于创建新的实例提供程序的非常好的文章,该提供程序通过 Unity 解析服务。
If you use default service instancing you must provide parameterless constructor. Your design provides dependency injection through constructor. In such case you must have your own instance provider to call the constructor and create service instance. You can create per service instance provider, behavior and optionally service host but it is really bad way. The better way is to use Inversion of Control container which will resolve your dependencies from configuration. In that case you will have only one new instance provider, behavior and optionally service host.
Here you have very nice post about creating new instnace provider which resolve services through Unity.