55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System;
|
|
using System.ServiceModel;
|
|
using Kreta.Resources;
|
|
|
|
namespace Kreta.Client
|
|
{
|
|
public class ClientHelper
|
|
{
|
|
//TODO:Exceptionokat szétszedni
|
|
public T CallService<TService, T>(Func<TService, T> action) where TService : ICommunicationObject, new()
|
|
{
|
|
T res;
|
|
var client = default(TService);
|
|
|
|
try
|
|
{
|
|
client = new TService();
|
|
|
|
res = action(client);
|
|
client.Close();
|
|
}
|
|
catch (TimeoutException ex)
|
|
{
|
|
throw new TimeoutException(ErrorResource.AzAdatokLekerdezesereTullepteAMegadottIdokeretet, ex);
|
|
}
|
|
catch (FaultException ex)
|
|
{
|
|
throw new Exception(ErrorResource.AKirBejelentkezesiAdatokNemMegfeleloekKerjukProbaljaMegismetelniABejelentkezest, ex);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception(ex.Message, ex);
|
|
}
|
|
finally
|
|
{
|
|
CloseClientCommunication(client);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
private void CloseClientCommunication<TService>(TService client) where TService : ICommunicationObject, new()
|
|
{
|
|
if (client.State == CommunicationState.Faulted)
|
|
{
|
|
client.Abort();
|
|
|
|
}
|
|
else if (client.State != CommunicationState.Closed)
|
|
{
|
|
client.Close();
|
|
}
|
|
}
|
|
}
|
|
}
|