0

I am new to Rest services. I did fair bit of research on this topic but still not clearly understood. Hope someone clear this to me. Thanks in advance.

My client is going to execute a method on my service class through an url. All I understood is client going to call

localhost/service/get/customers

In my rest service I have dependencies like a IRepository so that I can go to database and fetch the records. If I go with constructor injection when would client call this constructor?

It looks like I need to use a service locator to fetch IRepository inside the method which I need it.

Doesn't this violate OOP principle? Is testing easy without DI?

Can anyone please clarify this.

For any service how do we expect client to know what is IRepository is what methods it does have? Is isn't it for internal implementation purpose Do we need t expose to the client? Why should he bother about it? Can't I provide just Uri Which method to call (localhost/service/Products) and job done.

I will appreciate if you provide any live example.

Thanks a lot.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Jayee
  • 517
  • 5
  • 15
  • You've stumbled across a thorny issue: since it's the **WCF runtime** that will instantiate your WCF service class to handle the request, you **cannot** use constructor injection to resolve your dependencies. You'll have to find a way to let the WCF runtime create your service class, and then once it's created satisfy its dependencies. Some DI containers call this "post-creation injection" or something like that – marc_s Nov 18 '13 at 07:46

1 Answers1

2

No, it will not violate OOP priciple, if you will resolve for dependency inside a method. But there is no need for that, you can use constructor Dependency Injection within WCF using Instance Provider and ServiceHostFactory, there is also good example in this question.

If you are self-hosting the service, it will be even more simpler because it requires implementing only IInstanceProvider and ServiceHost. Assuming that you have Service service with constructor taking an IDependency parameter:

public class CustomInstanceProvider : IInstanceProvider, IContractBehavior
{
    private readonly IDependency dependency;

    public CustomInstanceProvider(IDependency dependency)
    {
        this.dependency = dependency;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        // Here you are injecting dependency by constructor
        return new Service(dependency);
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return this.GetInstance(instanceContext);
    }
    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {

    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {

    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.InstanceProvider = this;
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {

    }

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {

    }
}

public class CustomServiceHost : ServiceHost
{
    public CustomServiceHost(IDependency dependency, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        foreach (var cd in this.ImplementedContracts.Values)
        {
            cd.Behaviors.Add(new CustomInstanceProvider(dependency));
        }
    }
}

Then when creating service, use CustomServiceHost:

var serviceHost = new CustomServiceHost(dependency, typeof(Service), uris);
Community
  • 1
  • 1
Konrad Kokosa
  • 15,790
  • 2
  • 33
  • 54
  • Do I need to go through this much complexity when I am self hosting the service? isn't it overkill? Can you please give your opinion thanks – Jayee Nov 19 '13 at 04:49
  • No, if you are self hosting the service, it will be a little simpler because it will require implementing only `IInstanceProvider` and `ServiceHost`. I provide an example. – Konrad Kokosa Nov 19 '13 at 07:50