Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection wcf

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.

like image 288
Diego Dias Avatar asked Jun 09 '10 23:06

Diego Dias


People also ask

Is WCF discontinued?

Just like Web Forms and other . NET Framework technologies, your WCF applications will continue to work for a long time. In fact, WCF will likely work for the next two decades thanks to . NET Framework being considered part of the windows operating system.

What is WCF and why it is used?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.

Does .NET framework support dependency injection?

.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in .NET is a built-in part of the framework, along with configuration, logging, and the options pattern.

What are the three types of dependency injection?

There are three main styles of dependency injection, according to Fowler: Constructor Injection (also known as Type 3), Setter Injection (also known as Type 2), and Interface Injection (also known as Type 1).


2 Answers

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.

like image 113
Mark Seemann Avatar answered Sep 25 '22 13:09

Mark Seemann


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.

like image 25
Koby Mizrahy Avatar answered Sep 24 '22 13:09

Koby Mizrahy