A database based on .net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

// 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<TService> : IAsyncDisposable
{
protected Service(string endpoint)
{
ServiceFactory = OpenFactory(endpoint);
}
private ChannelFactory<TService> ServiceFactory { get; }
private ChannelFactory<TService> OpenFactory(string endpoint)
{
ChannelFactory<TService> factory = new ChannelFactory<TService>(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();
}
}
}