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.

85 lines
3.4 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;
using GBase.Client.Interfaces;
namespace GBase.Client.Services
{
/// <summary>
/// Base class for services of the <see cref="IGBaseClient"/>
/// </summary>
/// <typeparam name="TService">The <see cref="Type"/> of the inheriting service</typeparam>
public abstract class Service<TService> : IAsyncDisposable
{
/// <summary>
/// Base class for services of the <see cref="IGBaseClient"/>
/// </summary>
/// <param name="serverProtocol">The <see cref="ServerProtocol"/></param>
/// <param name="endpoint">The endpoint for this <typeparamref name="TService"/></param>
protected Service(ServerProtocol serverProtocol, string endpoint)
{
ServiceFactory = OpenFactory(serverProtocol, endpoint);
}
/// <summary>
/// The <see cref="ChannelFactory{TChannel}"/> for this <typeparamref name="TService"/>
/// </summary>
private ChannelFactory<TService> ServiceFactory { get; }
/// <summary>
/// Open a <see cref="ChannelFactory{TChannel}"/> for the given <see cref="ServerProtocol"/> and endpoint
/// </summary>
/// <param name="serverProtocol">The <see cref="ServerProtocol"/></param>
/// <param name="endpoint">The endpoint for this <typeparamref name="TService"/></param>
/// <returns>A <see cref="ChannelFactory{T}"/> for the <typeparamref name="TService"/></returns>
/// <exception cref="ArgumentOutOfRangeException">Invalid <see cref="ServerProtocol"/> used</exception>
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;
}
/// <summary>
/// Open an <see cref="IClientChannel"/> for this <typeparamref name="TService"/>
/// </summary>
/// <returns>The opened channel of type <typeparamref name="TService"/></returns>
protected TService OpenChannel()
{
TService channel = ServiceFactory.CreateChannel();
((IClientChannel)channel).Open();
return channel;
}
/// <summary>
/// Close the <see cref="IClientChannel"/> for a given <typeparamref name="TService"/>
/// </summary>
/// <param name="service">The <typeparamref name="TService"/></param>
protected void CloseChannel(TService service)
{
((IClientChannel) service).Close();
}
/// <summary>
/// Dispose used resources asynchronously
/// </summary>
/// <returns>A <see cref="ValueTask"/> to await</returns>
public async ValueTask DisposeAsync()
{
ServiceFactory.Close();
}
}
}