Files
BeWoPlaner/Service/ServiceProxy/ServiceFacade.cs
rene 4fbfdbaf2c Unit Tests erstellt
Informationen ausgelagert
Kleines Spy/Test Programm erstellt
Mehr Exceptions/InvalidUpdateFileRequestException.cs
Launcher Host in Download Server geändert
Download Server macht zusätzlich Abfrage an den Hauptserver
Download geht jetzt stückweise von statten
2022-12-28 16:36:59 +01:00

117 lines
3.6 KiB
C#

using BeWo.Service.Multitenancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace BeWo.Service.ServiceProxy
{
public class ServiceFacade
{
public static object _Lock = string.Empty;
private static Dictionary<Type, EndpointAddress> _EndpointAddresses;
private static readonly Dictionary<Type, object> _ProxyCache;
private static int _RunningAsyncs;
public static void UpdateServerAddresses(string main)
{
var serverName2 = main;
if (serverName2.Substring(serverName2.Length - 1, 1) != "/")
serverName2 += "/";
_EndpointAddresses = new Dictionary<Type, EndpointAddress>
{
{ typeof(ILauncherService), new EndpointAddress(serverName2 + "LauncherService.svc") }
};
}
static ServiceFacade()
{
_ProxyCache = new Dictionary<Type, object>();
}
public static void DoLauncherServiceSync(Action<ILauncherService> pAction)
{
try
{
pAction.Invoke(GetInstance<LauncherServiceClient, ILauncherService>());
}
catch (Exception e)
{
// if (HandleServiceProxyException(e))
// throw e;
throw e;
}
}
public static T DoLauncherServiceSync<T>(Func<ILauncherService, T> pFunc)
{
try
{
return pFunc.Invoke(GetInstance<LauncherServiceClient, ILauncherService>());
}
catch (Exception e)
{
throw e;
}
return default(T);
}
private static TInterface GetInstance<TClient, TInterface>()
where TClient : ClientBase<TInterface>, TInterface, new()
where TInterface : class
{
TClient lClient = null;
if (_ProxyCache.ContainsKey(typeof(TClient)))
{
lClient = (TClient)_ProxyCache[typeof(TClient)];
if (lClient.State == CommunicationState.Closing || lClient.State == CommunicationState.Closed || lClient.State == CommunicationState.Faulted)
{
if (lClient.State == CommunicationState.Faulted)
{
lClient.Abort();
}
lClient = null;
_ProxyCache.Remove(typeof(TClient));
}
}
if (lClient != null)
{// EndpointAddresses aktualisieren
if (!lClient.Endpoint.Address.Equals(_EndpointAddresses[typeof(TInterface)]))
{
if (lClient.State == CommunicationState.Opened)
lClient.Close();
if (_ProxyCache.ContainsKey(typeof(TClient)))
_ProxyCache.Remove(typeof(TClient));
lClient = null;
}
}
if (lClient == null)
{
lClient = new TClient();
if (!_ProxyCache.ContainsKey(lClient.GetType()))
{
_ProxyCache.Add(typeof(TClient), lClient);
}
// lClient.Endpoint.Behaviors.Add(new DownloadSecurityHeaderEndpointBehavior());
lClient.Endpoint.Behaviors.Add(new DMultitenancyEndpointBehavior());
lClient.Endpoint.Address = _EndpointAddresses[typeof(TInterface)];
}
return lClient;
}
}
}