501 lines
19 KiB
C#
501 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.ServiceModel;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
using BeWoLauncher.Multitenancy;
|
|
using BeWoLauncher.Security;
|
|
using BS.SharedLauncher.Information;
|
|
|
|
namespace BeWoLauncher.ServiceProxy
|
|
{
|
|
public static class LauncherServiceFacade
|
|
{
|
|
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 siteOfOrigin)
|
|
{
|
|
var serverName = siteOfOrigin;
|
|
if (serverName.Substring(serverName.Length - 1, 1) != "/")
|
|
serverName += "/";
|
|
|
|
_EndpointAddresses = new Dictionary<Type, EndpointAddress>
|
|
{
|
|
{ typeof(ILauncherService), new EndpointAddress(serverName + "LauncherService.svc") },
|
|
{ typeof(IEnumTranslationService), new EndpointAddress(serverName + "EnumTranslationService.svc") },
|
|
{ typeof(IDownloadBeWoService), new EndpointAddress(serverName + "DownloadBeWoService.svc") }
|
|
};
|
|
}
|
|
|
|
static LauncherServiceFacade()
|
|
{
|
|
_ProxyCache = new Dictionary<Type, object>();
|
|
}
|
|
|
|
#region Launcher
|
|
public static void DoLauncherServiceSync(Action<ILauncherService> pAction)
|
|
{
|
|
try
|
|
{
|
|
pAction.Invoke(GetInstance<LauncherServiceClient, ILauncherService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// if (HandleServiceProxyException(e))
|
|
// throw e;
|
|
ShowErrorMessage(e);
|
|
}
|
|
}
|
|
public static T DoLauncherServiceSync<T>(Func<ILauncherService, T> pFunc)
|
|
{
|
|
try
|
|
{
|
|
return pFunc.Invoke(GetInstance<LauncherServiceClient, ILauncherService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ShowErrorMessage(e);
|
|
}
|
|
return default(T);
|
|
}
|
|
public static void DoLauncherServiceAsync<T>(Func<ILauncherService, T> pFunc, Action<T> pCallBack)
|
|
{
|
|
DoAsync<LauncherServiceClient, ILauncherService, T>(pFunc, pCallBack, true);
|
|
}
|
|
public static void DoLauncherServiceAsync<T>(Func<ILauncherService, T> pFunc, Action<T> pCallBack, bool showWaitDialog)
|
|
{
|
|
DoAsync<LauncherServiceClient, ILauncherService, T>(pFunc, pCallBack, showWaitDialog);
|
|
}
|
|
public static void DoLauncherServiceAsync(Action<ILauncherService> pAction)
|
|
{
|
|
DoAsync<LauncherServiceClient, ILauncherService>(pAction, true);
|
|
}
|
|
public static void DoLauncherServiceAsync(Action<ILauncherService> pAction, Action pCallBack)
|
|
{
|
|
DoAsync<LauncherServiceClient, ILauncherService>(pAction, pCallBack, true);
|
|
}
|
|
public static void DoMultipleLauncherServicesAsync(List<Action<ILauncherService>> pActions, Action pAllDoneCallBack)
|
|
{
|
|
DoMulitpleAsync<LauncherServiceClient, ILauncherService>(pActions, pAllDoneCallBack, true);
|
|
}
|
|
public static void DoMultipleLauncherServicesSync(List<Action<ILauncherService>> pActions)
|
|
{
|
|
DoMulitpleSync<LauncherServiceClient, ILauncherService>(pActions, true);
|
|
}
|
|
#endregion
|
|
#region EnumTranslation
|
|
|
|
public static void DoEnumTranslationServiceSync(Action<IEnumTranslationService> pAction)
|
|
{
|
|
try
|
|
{
|
|
pAction.Invoke(GetInstance<EnumTranslationServiceClient, IEnumTranslationService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// if (HandleServiceProxyException(e))
|
|
// throw e;
|
|
ShowErrorMessage(e);
|
|
}
|
|
}
|
|
public static T DoEnumTranslationServiceSync<T>(Func<IEnumTranslationService, T> pFunc)
|
|
{
|
|
try
|
|
{
|
|
return pFunc.Invoke(GetInstance<EnumTranslationServiceClient, IEnumTranslationService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ShowErrorMessage(e);
|
|
}
|
|
return default(T);
|
|
}
|
|
public static void DoEnumTranslationServiceAsync<T>(Func<IEnumTranslationService, T> pFunc, Action<T> pCallBack)
|
|
{
|
|
DoAsync<EnumTranslationServiceClient, IEnumTranslationService, T>(pFunc, pCallBack, true);
|
|
}
|
|
public static void DoEnumTranslationServiceAsync<T>(Func<IEnumTranslationService, T> pFunc, Action<T> pCallBack, bool showWaitDialog)
|
|
{
|
|
DoAsync<EnumTranslationServiceClient, IEnumTranslationService, T>(pFunc, pCallBack, showWaitDialog);
|
|
}
|
|
public static void DoEnumTranslationServiceAsync(Action<IEnumTranslationService> pAction)
|
|
{
|
|
DoAsync<EnumTranslationServiceClient, IEnumTranslationService>(pAction, true);
|
|
}
|
|
public static void DoEnumTranslationServiceAsync(Action<IEnumTranslationService> pAction, Action pCallBack)
|
|
{
|
|
DoAsync<EnumTranslationServiceClient, IEnumTranslationService>(pAction, pCallBack, true);
|
|
}
|
|
public static void DoMultipleEnumTranslationServicesAsync(List<Action<IEnumTranslationService>> pActions, Action pAllDoneCallBack)
|
|
{
|
|
DoMulitpleAsync<EnumTranslationServiceClient, IEnumTranslationService>(pActions, pAllDoneCallBack, true);
|
|
}
|
|
public static void DoMultipleEnumTranslationServicesSync(List<Action<IEnumTranslationService>> pActions)
|
|
{
|
|
DoMulitpleSync<EnumTranslationServiceClient, IEnumTranslationService>(pActions, true);
|
|
}
|
|
|
|
#endregion
|
|
#region DownloadBeWo
|
|
public static void DoDownloadBeWoServiceSync(Action<IDownloadBeWoService> pAction)
|
|
{
|
|
try
|
|
{
|
|
pAction.Invoke(GetInstance<DownloadBeWoServiceClient, IDownloadBeWoService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// if (HandleServiceProxyException(e))
|
|
// throw e;
|
|
ShowErrorMessage(e);
|
|
}
|
|
}
|
|
public static T DoDownloadBeWoServiceSyncWithException<T>(Func<IDownloadBeWoService, T> pFunc)
|
|
{
|
|
try
|
|
{
|
|
return pFunc.Invoke(GetInstance<DownloadBeWoServiceClient, IDownloadBeWoService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
return default(T);
|
|
}
|
|
public static T DoDownloadBeWoServiceSync<T>(Func<IDownloadBeWoService, T> pFunc)
|
|
{
|
|
try
|
|
{
|
|
return pFunc.Invoke(GetInstance<DownloadBeWoServiceClient, IDownloadBeWoService>());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
ShowErrorMessage(e);
|
|
}
|
|
return default(T);
|
|
}
|
|
public static void DoDownloadBeWoServiceAsync<T>(Func<IDownloadBeWoService, T> pFunc, Action<T> pCallBack)
|
|
{
|
|
DoAsync<DownloadBeWoServiceClient, IDownloadBeWoService, T>(pFunc, pCallBack, true);
|
|
}
|
|
public static void DoDownloadBeWoServiceAsync<T>(Func<IDownloadBeWoService, T> pFunc, Action<T> pCallBack, bool showWaitDialog)
|
|
{
|
|
DoAsync<DownloadBeWoServiceClient, IDownloadBeWoService, T>(pFunc, pCallBack, showWaitDialog);
|
|
}
|
|
public static void DoDownloadBeWoServiceAsync(Action<IDownloadBeWoService> pAction)
|
|
{
|
|
DoAsync<DownloadBeWoServiceClient, IDownloadBeWoService>(pAction, true);
|
|
}
|
|
public static void DoDownloadBeWoServiceAsync(Action<IDownloadBeWoService> pAction, Action pCallBack)
|
|
{
|
|
DoAsync<DownloadBeWoServiceClient, IDownloadBeWoService>(pAction, pCallBack, true);
|
|
}
|
|
#endregion
|
|
|
|
private static void DoAsync<TClient, TInterface, T>(Func<TInterface, T> pFunc, Action<T> pCallback, bool showWaitDialog)
|
|
where TClient : ClientBase<TInterface>, TInterface, new()
|
|
where TInterface : class
|
|
{
|
|
DoAsync<TClient, TInterface, T>(pFunc, pCallback, null, showWaitDialog);
|
|
}
|
|
private static void DoAsync<TClient, TInterface, T>(Func<TInterface, T> pFunc, Action<T> pCallback, Action pErrorCallback, bool showWaitDialog)
|
|
where TClient : ClientBase<TInterface>, TInterface, new()
|
|
where TInterface : class
|
|
{
|
|
try
|
|
{
|
|
if (showWaitDialog)
|
|
{
|
|
StartWaiting();
|
|
}
|
|
|
|
pFunc.BeginInvoke(
|
|
GetInstance<TClient, TInterface>(),
|
|
cb =>
|
|
{
|
|
try
|
|
{
|
|
T lResult = pFunc.EndInvoke(cb);
|
|
if (pCallback != null)
|
|
{
|
|
pCallback(lResult);
|
|
}
|
|
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// bool lThrow = ShowErrorMessage(e);
|
|
// if (showWaitDialog) EndWaiting();
|
|
// if (lThrow)
|
|
// throw e;
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
|
|
ShowErrorMessage(e);
|
|
if (pErrorCallback != null)
|
|
{
|
|
pErrorCallback();
|
|
}
|
|
}
|
|
},
|
|
null);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// bool lThrow = ShowErrorMessage(e);
|
|
// if (showWaitDialog) EndWaiting();
|
|
// if (lThrow)
|
|
// throw e;
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
|
|
ShowErrorMessage(e);
|
|
}
|
|
}
|
|
private static void DoAsync<TClient, TInterface>(Action<TInterface> pAction, bool showWaitDialog)
|
|
where TClient : ClientBase<TInterface>, TInterface, new()
|
|
where TInterface : class
|
|
{
|
|
DoAsync<TClient, TInterface>(pAction, null, showWaitDialog);
|
|
}
|
|
private static void DoAsync<TClient, TInterface>(Action<TInterface> pAction, Action pCallback, bool showWaitDialog)
|
|
where TClient : ClientBase<TInterface>, TInterface, new()
|
|
where TInterface : class
|
|
{
|
|
try
|
|
{
|
|
if (showWaitDialog)
|
|
{
|
|
StartWaiting();
|
|
}
|
|
|
|
pAction.BeginInvoke(
|
|
GetInstance<TClient, TInterface>(),
|
|
cb =>
|
|
{
|
|
try
|
|
{
|
|
pAction.EndInvoke(cb);
|
|
if (pCallback != null)
|
|
{
|
|
pCallback();
|
|
}
|
|
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// bool lThrow = ShowErrorMessage(e);
|
|
// if (showWaitDialog) EndWaiting();
|
|
// if (lThrow)
|
|
// throw e;
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
|
|
ShowErrorMessage(e);
|
|
}
|
|
},
|
|
null);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
// bool lThrow = ShowErrorMessage(e);
|
|
// if (showWaitDialog) EndWaiting();
|
|
// if (lThrow)
|
|
// throw e;
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
|
|
ShowErrorMessage(e);
|
|
}
|
|
}
|
|
private static void DoMulitpleSync<TClient, TInterface>(List<Action<TInterface>> pActions, bool showWaitDialog)
|
|
where TClient : ClientBase<TInterface>, TInterface, new()
|
|
where TInterface : class
|
|
{
|
|
int lCount = pActions.Count;
|
|
|
|
if (showWaitDialog)
|
|
{
|
|
StartWaiting();
|
|
}
|
|
foreach (var iAction in pActions)
|
|
{
|
|
iAction.Invoke(GetInstance<TClient, TInterface>());
|
|
}
|
|
if (showWaitDialog)
|
|
{
|
|
EndWaiting();
|
|
}
|
|
}
|
|
private static void DoMulitpleAsync<TClient, TInterface>(List<Action<TInterface>> pActions, Action pAllDoneCallBack, bool showWaitDialog)
|
|
where TClient : ClientBase<TInterface>, TInterface, new()
|
|
where TInterface : class
|
|
{
|
|
int lCount = pActions.Count;
|
|
if (lCount == 0)
|
|
{
|
|
pAllDoneCallBack();
|
|
}
|
|
|
|
foreach (var iAction in pActions)
|
|
{
|
|
DoAsync<TClient, TInterface>(
|
|
iAction,
|
|
delegate
|
|
{
|
|
Monitor.Enter(_Lock);
|
|
lCount--;
|
|
Monitor.Exit(_Lock);
|
|
if (lCount == 0)
|
|
{
|
|
pAllDoneCallBack();
|
|
}
|
|
},
|
|
showWaitDialog);
|
|
}
|
|
}
|
|
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 LauncherSecurityHeaderEndpointBehavior());
|
|
lClient.Endpoint.Behaviors.Add(new MultitenancyEndpointBehavior());
|
|
|
|
lClient.Endpoint.Address = _EndpointAddresses[typeof(TInterface)];
|
|
}
|
|
|
|
return lClient;
|
|
}
|
|
private static void ShowErrorMessage(Exception e)
|
|
{
|
|
if (e is FaultException<BeWoFault>)
|
|
{
|
|
var lBeWoFault = (e as FaultException<BeWoFault>).Detail;
|
|
|
|
String rowUpdated = "Der Datensatz wurde in der Zwischenzeit von einem anderen Benutzer geändert. Ihre Änderungen konnten leider NICHT gespeichert werden. Das Problem tritt auf, wenn mehrere Benutzer gleichzeitig den gleichen Datensatz bearbeiten.\n\nSie müssen den Datensatz schließen und erneut öffnen, um die Änderungen vornehmen zu können.";
|
|
switch (lBeWoFault.FaultType)
|
|
{
|
|
case BeWoFaultType.Concurrency:
|
|
LauncherApp.ShowError(rowUpdated, e, false);
|
|
break;
|
|
case BeWoFaultType.DeleteNotPossible:
|
|
MessageBox.Show(lBeWoFault.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
break;
|
|
case BeWoFaultType.LoginNameTaken:
|
|
MessageBox.Show(lBeWoFault.Message, "Warnung", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
return;
|
|
case BeWoFaultType.CustomWithoutDetails:
|
|
LauncherApp.ShowError(lBeWoFault.Message, e, false);
|
|
break;
|
|
default:
|
|
if (!string.IsNullOrEmpty(e.Message) && e.Message.IndexOf("Row was updated or deleted by another transaction") >= 0)
|
|
{
|
|
LauncherApp.ShowError(rowUpdated, e, false);
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(lBeWoFault.Message))
|
|
{
|
|
LauncherApp.ShowError(lBeWoFault.Message, e, true);
|
|
}
|
|
else
|
|
{
|
|
LauncherApp.ShowError(null, e, true);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
LauncherApp.ShowError(null, e, true);
|
|
}
|
|
}
|
|
private static void StartWaiting()
|
|
{
|
|
//Monitor.Enter(_Lock);
|
|
//if ((++_RunningAsyncs) == 1)
|
|
//{
|
|
// LauncherApp.Window.StartWaiting();
|
|
//}
|
|
|
|
//Monitor.Exit(_Lock);
|
|
}
|
|
private static void EndWaiting()
|
|
{
|
|
//Monitor.Enter(_Lock);
|
|
//if ((--_RunningAsyncs) == 0)
|
|
//{
|
|
// LauncherApp.Window.EndWaiting();
|
|
//}
|
|
|
|
//Monitor.Exit(_Lock);
|
|
}
|
|
}
|
|
}
|