Dependency Injection wcf
Asked Answered
S

2

6

I want inject a implementation of my Interface in the WCF but I want initialize my container of Dependency Injection in the Client of the WCF. So I can have a different implementation for each client of the my service.

Stets answered 9/6, 2010 at 23:58 Comment(0)
M
20

When you use svcutil.exe or the Add Service Reference wizard in Visual Studio, one of the many types auto-generated will be a client interface. Let's call it IMyService. There will also be another auto-generated interface called something like IMyServiceChannel that implements IMyService and IDisposable. Use this abstraction in the rest of your client application.

Since you want to be able to create a new channel and close it again, you can introduce an Abstract Factory:

public interface IMyServiceFactory
{
    IMyServiceChannel CreateChannel();
}

In the rest of your client application, you can take a dependency on IMyServiceFactory:

public class MyClient
{
    private readonly IMyServiceFactory factory;

    public MyClient(IMyServiceFactory factory)
    {
        if (factory == null)
        {
            throw new ArgumentNullException("factory");
        }

        this.factory = factory;
    }

    // Use the WCF proxy
    public string Foo(string bar)
    {
        using(var proxy = this.factory.CreateChannel())
        {
            return proxy.Foo(bar);
        }
    }
}

You can create a concrete implementation of IMyServiceFactory that wraps WCF's ChannelFactory<T> as an implementation:

public MyServiceFactory : IMyServiceFactory
{
    public IMServiceChannel CreateChannel()
    {
        return new ChannelFactory<IMyServiceChannel>().CreateChannel();
    }
}

You can now configure your DI Container by mapping IMyServiceFactory to MyServiceFactory. Here's how it's done in Castle Windsor:

container.Register(Component
    .For<IMyServiceFactory>()
    .ImplementedBy<MyServiceFactory>());

Bonus info: Here's how to wire up a WCF service with a DI Container.

Margarettemargarida answered 10/6, 2010 at 3:37 Comment(4)
Ok, but How I do to inject in service a custom implementation of a class? Example: public interface ISendMail{ seng(string subject, string message, string to); }. I want implement a class different for each client and in the client I inject this implementation in the WCF Service. TksStets
The code may not exhibit cyclomatic complexity or anyother buzz word, but what I do know is that, taken in its current form, this requires alot of hooking up if you have more than a few services you are interested in. It requires a more generic approach to be worthwhile on bigger projects, IMO.Ferromagnetism
You keep using that word. I do not think it means what you think it means.Margarettemargarida
What word? I think I may have confused this post with your link..(regards cyclomatic complexity)Ferromagnetism
S
1

Here is what I understand from your question:

You have an interface that is not related to WCF. Let's call it IInterface

You have a WCF client that used a service. Let's call the service contract: IService

you want the ServiceClient class that by default implements the IService when you add a service reference also to implement IInterface.

IF this is the case, you can use the fact that the ServiceClient class is marked as partial.

Just make another partial declaration for ServiceClient and add the interface you need (You have to make sure that the namespaces are equal for the auto-generated code and your code). It should look somthing like:

namespace [ServiceClient Namespace]
{
    public partial class ServiceClient : IInterface
    {
    }
}

Hope it helped.

Seasoning answered 15/4, 2012 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.