112 lines
4.3 KiB
C#
112 lines
4.3 KiB
C#
using Hcs.ClientApi.RemoteCaller;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using NsiCommon = Hcs.Service.Async.NsiCommon.v15_7_0_1;
|
|
|
|
namespace Hcs.Service.Async.NsiCommon.v15_7_0_1
|
|
{
|
|
public partial class AckRequestAck : IHcsAck { }
|
|
public partial class getStateResult : IHcsGetStateResultOne { }
|
|
public partial class Fault : IHcsFault { }
|
|
public partial class HeaderType : IHcsHeaderType { }
|
|
}
|
|
|
|
namespace Hcs.ClientApi.NsiCommonApi
|
|
{
|
|
internal class HcsNsiCommonMethod : HcsRemoteCallMethod<IHcsGetStateResultOne>
|
|
{
|
|
public HcsEndPoints EndPoint => HcsEndPoints.NsiCommonAsync;
|
|
|
|
public NsiCommon.ISRequestHeader CreateRequestHeader() =>
|
|
HcsRequestHelper.CreateHeader<NsiCommon.ISRequestHeader>(ClientConfig);
|
|
|
|
public HcsNsiCommonMethod(HcsClientConfig config) : base(config) { }
|
|
|
|
public System.ServiceModel.EndpointAddress RemoteAddress
|
|
=> GetEndpointAddress(HcsConstants.EndPointLocator.GetPath(EndPoint));
|
|
|
|
private NsiCommon.NsiPortsTypeAsyncClient NewPortClient()
|
|
{
|
|
var client = new NsiCommon.NsiPortsTypeAsyncClient(_binding, RemoteAddress);
|
|
ConfigureEndpointCredentials(client.Endpoint, client.ClientCredentials);
|
|
return client;
|
|
}
|
|
|
|
public async Task<IHcsGetStateResultOne> SendAndWaitResultAsync(
|
|
object request,
|
|
Func<NsiCommon.NsiPortsTypeAsyncClient, Task<IHcsAck>> sender,
|
|
CancellationToken token)
|
|
{
|
|
token.ThrowIfCancellationRequested();
|
|
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
return await SendAndWaitResultAsyncImpl(request, sender, token);
|
|
}
|
|
catch (HcsRestartTimeoutException e)
|
|
{
|
|
if (!CanBeRestarted) throw new HcsException("Превышен лимит ожидания выполнения запроса", e);
|
|
Log($"Перезапускаем запрос типа {request.GetType().Name}...");
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task<IHcsGetStateResultOne> SendAndWaitResultAsyncImpl(
|
|
object request,
|
|
Func<NsiCommon.NsiPortsTypeAsyncClient, Task<IHcsAck>> sender,
|
|
CancellationToken token)
|
|
{
|
|
if (request == null) throw new ArgumentNullException("Null request");
|
|
string version = HcsRequestHelper.GetRequestVersionString(request);
|
|
_config.Log($"Отправляем запрос: {RemoteAddress.Uri}/{request.GetType().Name} в версии {version}...");
|
|
|
|
var stopWatch = System.Diagnostics.Stopwatch.StartNew();
|
|
|
|
IHcsAck ack;
|
|
using (var client = NewPortClient())
|
|
{
|
|
ack = await sender(client);
|
|
}
|
|
|
|
stopWatch.Stop();
|
|
_config.Log($"Запрос принят в обработку за {stopWatch.ElapsedMilliseconds}мс., подтверждение {ack.MessageGUID}");
|
|
|
|
var stateResult = await WaitForResultAsync(ack, true, token);
|
|
|
|
if (stateResult.Item is NsiCommon.ErrorMessageType x)
|
|
{
|
|
throw HcsRemoteException.CreateNew(x.ErrorCode, x.Description);
|
|
}
|
|
|
|
return stateResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Выполняет однократную проверку наличия результата.
|
|
/// Возвращает null если результата еще нет.
|
|
/// </summary>
|
|
protected override async Task<IHcsGetStateResultOne> TryGetResultAsync(IHcsAck sourceAck, CancellationToken token)
|
|
{
|
|
using (var client = NewPortClient())
|
|
{
|
|
var requestHeader = HcsRequestHelper.CreateHeader<NsiCommon.ISRequestHeader>(_config);
|
|
var requestBody = new NsiCommon.getStateRequest { MessageGUID = sourceAck.MessageGUID };
|
|
|
|
var response = await client.getStateAsync(requestHeader, requestBody);
|
|
var resultBody = response.getStateResult;
|
|
|
|
if (resultBody.RequestState == HcsAsyncRequestStateTypes.Ready)
|
|
{
|
|
return resultBody;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|