SVI
SVI

Reputation: 1651

Constructor error in WCF service implemented with Repository and UnitofWork patterns

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;
    }

}

Upvotes: 2

Views: 2054

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364349

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.

Upvotes: 1

Related Questions