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.

53 lines
1.7 KiB

// Author: Gockner, Simon
// Created: 2020-02-11
// Copyright(c) 2020 SimonG. All Rights Reserved.
using System;
using System.ServiceModel;
using System.Threading.Tasks;
using GBase.Api.Communication;
namespace GBase.Client.Services
{
public abstract class Service<TService> : IAsyncDisposable
{
protected Service(ServerProtocol serverProtocol, string endpoint)
{
ServiceFactory = OpenFactory(serverProtocol, endpoint);
}
private ChannelFactory<TService> ServiceFactory { get; }
private ChannelFactory<TService> OpenFactory(ServerProtocol serverProtocol, string endpoint)
{
ChannelFactory<TService> factory;
if (serverProtocol == ServerProtocol.Http)
factory = new ChannelFactory<TService>(new BasicHttpBinding(), new EndpointAddress(endpoint));
else if (serverProtocol == ServerProtocol.Tcp)
factory = new ChannelFactory<TService>(new NetTcpBinding(), new EndpointAddress(endpoint)); //TODO: Set security mode here?
else
throw new ArgumentOutOfRangeException(nameof(serverProtocol), serverProtocol, "Invalid server protocol used.");
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();
}
}
}