// Author: Gockner, Simon // Created: 2020-02-11 // Copyright(c) 2020 SimonG. All Rights Reserved. using System; using System.ServiceModel; using System.Threading.Tasks; namespace GBase.Client.Services { public abstract class Service : IAsyncDisposable { protected Service(string endpoint) { ServiceFactory = OpenFactory(endpoint); } private ChannelFactory ServiceFactory { get; } private ChannelFactory OpenFactory(string endpoint) { ChannelFactory factory = new ChannelFactory(new BasicHttpBinding(), new EndpointAddress(endpoint)); factory.Open(); return factory; } protected TService OpenChannel() { TService channel = ServiceFactory.CreateChannel(); ((IClientChannel)channel).Open(); return channel; } protected void CloseChannel(TService service) { ((IClientChannel) service).Close(); } public async ValueTask DisposeAsync() { ServiceFactory.Close(); } } }