public static string ToQueryString(this HubConnectionExternalModel hubConnection) { var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty); queryString.Add("prepare", hubConnection.TaskId); queryString.Add("method", hubConnection.OnLogger); return($"&{queryString}"); }
/// <summary> /// Build a connection to the shield hub for the current protection process. /// </summary> /// <param name="externalConnection"></param> /// <param name="withLogger"></param> /// <returns></returns> private HubConnection InstanceHubConnector(HubConnectionExternalModel externalConnection, bool withLogger) { try { Parent.CustomLogger?.LogDebug("Starting the negotiation with the notification server."); var request = new RestRequest("/logger/negotiate"); var negotiation = _client.Get <NegotiateModel>(request); if (!negotiation.IsSuccessful) { throw new Exception("Can't negotiate"); } //Creates logger hub var connection = new HubConnectionBuilder() .WithUrl($"{negotiation?.Data.Url}{externalConnection.ToQueryString()}", options => { options.SkipNegotiation = true; //Negotiation previously done. options.Transports = HttpTransportType.WebSockets; options.AccessTokenProvider = () => Task.FromResult(negotiation?.Data.AccessToken); //Add bearer token for current connection }) #if !DEBUG .ConfigureLogging(builder => builder.SetMinimumLevel(LogLevel.None)) #endif .WithAutomaticReconnect() .Build(); //Build hub Parent.CustomLogger?.LogDebug("The connection to the shield notification server has been created."); if (!withLogger) { return(connection); } if (Parent.CustomLogger is null) { throw new Exception("The \"WithLogger\" method cannot be called if a custom logger has not been provided in the shield client."); } Parent.CustomLogger?.LogDebug("The current logger has been configured as the output of the connection logs."); connection.OnLog(externalConnection.OnLogger, OnCustomLog); return(connection); } catch (Exception ex) { Parent.CustomLogger?.LogCritical("An error occurred while creating the connection to the shield server for the current process."); throw new Exception($"An error occurred while creating the connection to the shield server for the current process: {ex.Message}"); } }
/// <summary> /// Build a Started Connection with the hub of the current process to handle the service from .NET Framework applications. /// </summary> /// <param name="externalConnection"></param> /// <param name="started"></param> /// <returns></returns> public bool InstanceAndStartConnector(HubConnectionExternalModel externalConnection, out StartedConnection started) { try { started = Task.Run(async() => { var instanceConnectorAsync = await InstanceHubConnectorAsync(externalConnection); await instanceConnectorAsync.StartAsync(); return(new StartedConnection(instanceConnectorAsync)); }).Result; return(true); } catch { started = null; return(false); } }
public ProtectionResult ProtectSingleFile(string projectKey, string fileBlob, HubConnectionExternalModel hubConnection, ApplicationConfigurationDto configuration) => ProtectSingleFile(projectKey, fileBlob, hubConnection.TaskId, configuration);
/// <summary> /// Build a connection to the shield hub for the current protection process. /// </summary> /// <param name="externalConnection"></param> /// <returns></returns> public HubConnection InstanceHubConnector(HubConnectionExternalModel externalConnection) => InstanceHubConnector(externalConnection, false);
/// <summary> /// Build a connection to the shield hub for the current protection process async. /// </summary> /// <param name="externalConnection"></param> /// <returns></returns> public async Task <HubConnection> InstanceHubConnectorAsync(HubConnectionExternalModel externalConnection) => await InstanceHubConnectorAsync(externalConnection, false);
/// <summary> /// Build a connection to the shield hub for the current protection process with current logger. /// </summary> /// <param name="externalConnection"></param> /// <returns></returns> public HubConnection InstanceHubConnectorWithLogger(HubConnectionExternalModel externalConnection) => InstanceHubConnector(externalConnection, true);
/// <summary> /// Build a connection to the shield hub for the current protection process with current logger async. /// </summary> /// <param name="externalConnection"></param> /// <returns></returns> public async Task <HubConnection> InstanceHubConnectorWithLoggerAsync(HubConnectionExternalModel externalConnection) => await InstanceHubConnectorAsync(externalConnection, true);