|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="WcfProxy.cs" company="Marimer LLC"> |
| 3 | +// Copyright (c) Marimer LLC. All rights reserved. |
| 4 | +// Website: https://cslanet.com |
| 5 | +// </copyright> |
| 6 | +// <summary>Provides consistent context information between the client</summary> |
| 7 | +//----------------------------------------------------------------------- |
| 8 | + |
| 9 | +using System.ServiceModel; |
| 10 | +using Csla.Configuration; |
| 11 | +using Csla.DataPortalClient; |
| 12 | + |
| 13 | +namespace Csla.Channels.Wcf.Client |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Represents a <see cref="DataPortalProxy"/> that communicates with a remote data portal using WCF. |
| 17 | + /// </summary> |
| 18 | + /// <param name="applicationContext"> |
| 19 | + /// The client side context for the data portal. |
| 20 | + /// </param> |
| 21 | + /// <param name="wcfProxyOptions"> |
| 22 | + /// The options that are used to configure the data portal proxy. |
| 23 | + /// </param> |
| 24 | + /// <param name="dataPortalOptions"> |
| 25 | + /// The options that are used to configure the client side data portal. |
| 26 | + /// </param> |
| 27 | + /// <exception cref="ArgumentNullException"> |
| 28 | + /// <paramref name="wcfProxyOptions"/> is <see langword="null"/>. |
| 29 | + /// </exception> |
| 30 | + public class WcfProxy(ApplicationContext applicationContext, WcfProxyOptions wcfProxyOptions, DataPortalOptions dataPortalOptions) : DataPortalProxy(applicationContext) |
| 31 | + { |
| 32 | + protected WcfProxyOptions _options = wcfProxyOptions ?? throw new ArgumentNullException(nameof(wcfProxyOptions)); |
| 33 | + protected string? _versionRoutingTag = dataPortalOptions.VersionRoutingTag; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the URL address for the data portal server used by this proxy instance. |
| 37 | + /// </summary> |
| 38 | + public override string DataPortalUrl => _options.DataPortalUrl; |
| 39 | + |
| 40 | + protected override async Task<byte[]> CallDataPortalServer(byte[] serialized, string operation, string? routingToken, bool isSync) |
| 41 | + { |
| 42 | + var client = new WcfPortalClient(_options.Binding, new EndpointAddress(_options.DataPortalUrl)); |
| 43 | + |
| 44 | + var wcfRequest = new WcfRequest |
| 45 | + { |
| 46 | + Operation = CreateOperationTag(operation, _versionRoutingTag, routingToken), |
| 47 | + Body = serialized |
| 48 | + }; |
| 49 | + |
| 50 | + try |
| 51 | + { |
| 52 | + var response = isSync ? client.Invoke(wcfRequest) : await client.InvokeAsync(wcfRequest); |
| 53 | + |
| 54 | + client.Close(); |
| 55 | + |
| 56 | + return response.Body; |
| 57 | + } |
| 58 | + catch (Exception) |
| 59 | + { |
| 60 | + client.Abort(); |
| 61 | + throw; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + internal async Task<WcfResponse> RouteMessage(WcfRequest request) |
| 66 | + { |
| 67 | + var client = new WcfPortalClient(_options.Binding, new EndpointAddress(_options.DataPortalUrl)); |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + var response = await client.InvokeAsync(request); |
| 72 | + |
| 73 | + client.Close(); |
| 74 | + |
| 75 | + return response; |
| 76 | + } |
| 77 | + catch (Exception) |
| 78 | + { |
| 79 | + client.Abort(); |
| 80 | + throw; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private string CreateOperationTag(string operation, string? versionToken, string? routingToken) |
| 85 | + { |
| 86 | + if (!string.IsNullOrWhiteSpace(versionToken) || !string.IsNullOrWhiteSpace(routingToken)) |
| 87 | + return $"{operation}/{routingToken}-{versionToken}"; |
| 88 | + return operation; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments