c# – Ideologically correct work with WCF service through WinForms application?


Tell me, how is it ideologically correct to work with a WCF service in a WinForms application?

My current implementation creates a mediator in various parts of the code. That is, the client needs to receive data from sales, the method is called, and in this method an intermediary is created, which pulls a couple of methods of the WCF service and the intermediary is destroyed.

How true is this?

Or is it more correct to have a property with an intermediary type in the main form and initialize it when creating a form, and then pass it as arguments to the constructors of other forms?

Or are there prettier options?


And another question:

Let's say the service has multiple endpoints.

Accordingly, on the client side, each point needs its own proxy.

How to rein in this client-side zoo? Is it correct to create a client-side class that will store all proxies inside itself, or is there something prettier?


You can create a DI container where you can register a class for working with a WCF service and make requests through it. In the created class, you encapsulate the logic for working with the WCF service, and you can also cache some requests.

  1. First, let's define the interface for working with the wcf service. For example, this will get a list of students, groups, teachers.

     public interface IUniverDataService { IEnumerable<Student> Students {get;} IEnumerable<StudGroup> StudGroups {get;} //и т.д. }
  2. Let us inherit the interface and implement the class for working with the wcf service

     WcfUniverDataService : IUniverDataService { //реализуем интерфейс для работы с WCF сервисом }
  3. After registering the class in the container, we can use the object of the WcfUniverDataService class in the class we need by specifying its interface in the constructor

     public class SomeClass { IUniverDataService _univerDataService; public SomeClass(IUniverDataService univerDataService) { _univerDataService = univerDataService; //используем наш класс работы с wcf сервисом в это классе. } }
  4. But in order to use, create a DI container and register a class there. DI containers are different, for example, take Unity. It can be found in NuGet.

We create a container, in your case in the main form

var container = new UnityContainer();

In the container we register the previously created class

container.RegisterType<IUniverDataService,WcfUniverDataService>(new ContainerControlledLifetimeManager());
Scroll to Top