예제 #1
0
    public static void SetCustomStatus(this IDurableOrchestrationContext durableOrchestrationContext, object customStatusObject, ILogger log, Exception exception = null)
    {
        if (durableOrchestrationContext is null)
        {
            throw new ArgumentNullException(nameof(durableOrchestrationContext));
        }

        durableOrchestrationContext.SetCustomStatus(customStatusObject);

        var customStatusMessage = customStatusObject is string customStatusString
            ? customStatusString
            : TeamCloudSerialize.SerializeObject(customStatusObject, Formatting.None);

        var message = $"{durableOrchestrationContext.InstanceId}: CUSTOM STATUS -> {customStatusMessage ?? "NULL"}";
        var safeLog = durableOrchestrationContext.CreateReplaySafeLogger(log ?? NullLogger.Instance);

        if (exception is null)
        {
            safeLog.LogInformation(message);
        }
        else
        {
            safeLog.LogError(exception, message);
        }
    }
    protected async Task EnqueueCommandAsync(ICommand command)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

        try
        {
            var commandQueue = await commandQueueInstance
                               .Value
                               .ConfigureAwait(false);

            var commandMessage = TeamCloudSerialize.SerializeObject(command);

            await commandQueue
            .SendMessageAsync(commandMessage)
            .ConfigureAwait(false);
        }
        catch
        {
            commandQueueInstance.Reset();

            throw;
        }
    }
예제 #3
0
    public async Task Serialize_WithErrorsAsync()
    {
        var command = new MockCommand(this.user, MockPayload.Instance);

        var result1 = command.CreateResult();

        result1.Errors = new List <CommandError>
        {
            await CreateFlurlHttpExceptionAsync().ConfigureAwait(false),
            new NotSupportedException()
        };

        var json1 = TeamCloudSerialize.SerializeObject(result1);

        Assert.NotEmpty(json1);
        Assert.Contains("$type", json1);

        var result2 = TeamCloudSerialize.DeserializeObject <ICommandResult>(json1);

        var json2 = TeamCloudSerialize.SerializeObject(result2);

        Assert.NotEmpty(json2);
        Assert.Contains("$type", json2);

        Assert.Equal(json1, json2);
    }
예제 #4
0
    public static Exception ToException(this CommandError error)
    {
        if (error is null)
        {
            throw new ArgumentNullException(nameof(error));
        }

        return(new Exception(error.Message)
        {
            Source = TeamCloudSerialize.SerializeObject(error)
        });
    }
예제 #5
0
    public void Serialize()
    {
        var command = new MockCommand(this.user, MockPayload.Instance);

        var json = TeamCloudSerialize.SerializeObject(command);

        Assert.NotEmpty(json);
        Assert.Contains("$type", json);

        var resultObj = TeamCloudSerialize.DeserializeObject <ICommand>(json);

        Assert.IsType <MockCommand>(resultObj);
    }
예제 #6
0
    public void LinksMaterializedOnDeserializedInstances()
    {
        Assert.NotNull(ReferenceLink.BaseUrl);

        var mockOwnerJson = TeamCloudSerialize.SerializeObject(new MockOwner());
        var mockOwner     = TeamCloudSerialize.DeserializeObject <MockOwner>(mockOwnerJson);

        var mockOwnerId = mockOwner.Id;

        mockOwner.Id = Guid.NewGuid(); // this must not affect the self link

        Assert.StartsWith(ReferenceLink.BaseUrl, mockOwner.Links.Self.ToString());
        Assert.EndsWith(mockOwnerId.ToString(), mockOwner.Links.Self.ToString());
    }
예제 #7
0
    public virtual object Clone(bool reset)
    {
        var json = TeamCloudSerialize
                   .SerializeObject(this);

        var clone = (ContainerDocument)TeamCloudSerialize
                    .DeserializeObject(json, GetType());

        if (reset)
        {
            clone.Id = Guid.NewGuid().ToString();
        }

        return(clone);
    }
    public void DeSerialize_NameValueCollection()
    {
        var nvc = new NameValueCollection();

        for (int i = 1; i <= 10; i++)
            nvc.Add("foo", $"bar{i}");

        var json = TeamCloudSerialize.SerializeObject(nvc);

        for (int i = 1; i <= 10; i++)
            Assert.Contains($"bar{i}", json);

        var nvc2 = TeamCloudSerialize.DeserializeObject<NameValueCollection>(json);

        for (int i = 1; i <= 10; i++)
            Assert.Contains($"bar{i}", nvc2.GetValues("foo"));
    }
예제 #9
0
    public async Task <T> SetSecretAsync <T>(string secretName, T secretValue)
        where T : class, new()
    {
        if (secretValue is null)
        {
            await SetSecretAsync(secretName, null)
            .ConfigureAwait(false);

            return(secretValue);
        }
        else
        {
            var secretJson = await SetSecretAsync(secretName, TeamCloudSerialize.SerializeObject(secretValue))
                             .ConfigureAwait(false);

            return(TeamCloudSerialize.DeserializeObject <T>(secretJson));
        }
    }
예제 #10
0
    public static async Task RunActivity(
        [ActivityTrigger] IDurableActivityContext activityContext,
        [DurableClient] IDurableOrchestrationClient orchestrationClient,
        ILogger log)
    {
        if (activityContext is null)
        {
            throw new ArgumentNullException(nameof(activityContext));
        }

        if (orchestrationClient is null)
        {
            throw new ArgumentNullException(nameof(orchestrationClient));
        }

        var functionInput = activityContext.GetInput <Input>();

        try
        {
            await orchestrationClient
            .RaiseEventAsync(functionInput.InstanceId, functionInput.EventName, functionInput.EventData)
            .ConfigureAwait(false);

            log.LogInformation($"Raised event '{functionInput.EventName}' for instance {functionInput.InstanceId}: {SerializeEventData()}");
        }
        catch (Exception exc)
        {
            log.LogError(exc, $"Failed to raise event '{functionInput.EventName}' for instance {functionInput.InstanceId}: {exc.Message}");
        }

        string SerializeEventData()
        {
            if (functionInput.EventData is null)
            {
                return(null);
            }

            var eventDataType = functionInput.EventData.GetType();

            return((eventDataType.IsValueType || eventDataType.Equals(typeof(string)))
                ? functionInput.EventData.ToInvariantString()
                : TeamCloudSerialize.SerializeObject(functionInput.EventData));
        }
    }
        static async Task <string> ReadContentAsync(HttpContent httpContent)
        {
            if ((httpContent?.Headers.ContentLength.GetValueOrDefault(0) ?? 0) == 0)
            {
                return(string.Empty);
            }

            var content = await httpContent
                          .ReadAsStringAsync()
                          .ConfigureAwait(false);

            //TODO: why ist this here needed
            if (httpContent.Headers.ContentType.MediaType.Equals("application/json", StringComparison.OrdinalIgnoreCase))
            {
                var jsonContent = TeamCloudSerialize.DeserializeObject(content);

                return(TeamCloudSerialize.SerializeObject(jsonContent, Formatting.Indented));
            }

            return(content);
        }
예제 #12
0
    public static async Task <ICommandResult> RunActivity(
        [ActivityTrigger] IDurableActivityContext context,
        [DurableClient] IDurableClient durableClient,
        ILogger log)
    {
        if (context is null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        if (durableClient is null)
        {
            throw new ArgumentNullException(nameof(durableClient));
        }

        try
        {
            var commandResult = context.GetInput <Input>().CommandResult;

            var commandStatus = await durableClient
                                .GetStatusAsync(commandResult.CommandId.ToString())
                                .ConfigureAwait(false);

            if (commandStatus is not null)
            {
                commandResult.ApplyStatus(commandStatus);

                Debug.WriteLine($"Augmented command result: {TeamCloudSerialize.SerializeObject(commandResult)}");
            }

            return(commandResult);
        }
        catch (Exception exc)
        {
            log?.LogError(exc, $"Failed to augment command result with orchestration status: {exc.Message}");

            throw exc.AsSerializable();
        }
    }
    private void AssertException(Exception serializableException, bool serializableExpected = true)
    {
        Assert.Equal(serializableExpected, serializableException.IsSerializable(out _));

        string serializableExceptionJson;

        try
        {
            serializableExceptionJson = TeamCloudSerialize.SerializeObject(serializableException, Formatting.Indented);
        }
        catch
        {
            serializableExceptionJson = default;
        }

        if (serializableExpected)
        {
            Assert.NotNull(serializableExceptionJson);
        }

        if (!string.IsNullOrEmpty(serializableExceptionJson))
        {
            Exception deserializedException;

            try
            {
                deserializedException = (Exception)TeamCloudSerialize.DeserializeObject(serializableExceptionJson, serializableException.GetType());
            }
            catch when(!serializableExpected)
            {
                deserializedException = default;
            }

            if (serializableExpected)
            {
                Assert.NotNull(deserializedException);
            }
        }
    }
예제 #14
0
    protected JToken GeneratePermissionDictionary(JObject json, JsonSerializer serializer)
    {
        var keyValuePairs = json
                            .SelectTokens("$.permissions[*]")
                            .OfType <JObject>()
                            .Select(t =>
        {
            var role       = Enum.Parse <ProjectUserRole>(t.GetValue("role", StringComparison.OrdinalIgnoreCase)?.ToString().Trim(), true);
            var permission = t.GetValue("permission", StringComparison.OrdinalIgnoreCase)?.ToString().Trim();

            return(new KeyValuePair <ProjectUserRole, string>(role, permission));
        });

        var dictionary = keyValuePairs
                         .Where(kvp => !string.IsNullOrEmpty(kvp.Value))
                         .GroupBy(kvp => kvp.Key)
                         .ToDictionary(grp => grp.Key, grp => grp.Select(kvp => kvp.Value).Distinct());

        return(JObject.Parse(TeamCloudSerialize.SerializeObject(dictionary, new TeamCloudSerializerSettings()
        {
            TypeNameHandling = TypeNameHandling.None
        })));
    }
예제 #15
0
    public string GetProjectDefinitionAsJson(string suffix)
    {
        var resourceName = GetType().Assembly
                           .GetManifestResourceNames()
                           .FirstOrDefault(name => name.Equals($"{GetType().FullName}_{suffix}.yaml", StringComparison.OrdinalIgnoreCase));

        if (string.IsNullOrEmpty(resourceName))
        {
            throw new NullReferenceException($"Component definition not found.");
        }

        using var resourceStream = GetType().Assembly
                                   .GetManifestResourceStream(resourceName);

        using var resourceReader = new StreamReader(resourceStream);

        var data = new Deserializer().Deserialize(resourceReader);

        return(TeamCloudSerialize.SerializeObject(data, new TeamCloudSerializerSettings()
        {
            // ensure we disable the type name handling to get clean json
            TypeNameHandling = TypeNameHandling.None
        }));
    }
예제 #16
0
    public void Serialize()
    {
        var broadcastMessage = new BroadcastMessage()
        {
            Action    = "Custom",
            Timestamp = DateTime.UtcNow,
            Items     = new List <BroadcastMessage.Item>()
            {
                new BroadcastMessage.Item()
                {
                    Organization = Guid.NewGuid().ToString(),
                    Project      = Guid.NewGuid().ToString(),
                    Component    = Guid.NewGuid().ToString(),
                    ETag         = Guid.NewGuid().ToString(),
                    Timestamp    = DateTime.UtcNow
                }
            }
        };

        var broadcastJson = TeamCloudSerialize.SerializeObject(broadcastMessage);

        Assert.NotEmpty(broadcastJson);
        Assert.DoesNotContain("$type", broadcastJson);
    }
예제 #17
0
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);

        info.AddValue(nameof(ErrorResult), TeamCloudSerialize.SerializeObject(ErrorResult));
    }
예제 #18
0
    public override async Task <ICommandResult> HandleAsync(ICommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

        var commandResult = command.CreateResult();

        try
        {
            if (CanHandle(command))
            {
                var containerDocument = (IContainerDocument)command.Payload;

                using var loggerFactory  = new PassthroughLoggerFactory(log);
                using var serviceManager = CreateServiceManager();

                var broadcastMessage = new BroadcastMessage()
                {
                    Action    = commandResult.CommandAction.ToString().ToLowerInvariant(),
                    Timestamp = commandResult.LastUpdatedTime.GetValueOrDefault(DateTime.UtcNow),
                    Items     = GetItems(containerDocument)
                };

                var broadcastPayload = TeamCloudSerialize.SerializeObject(broadcastMessage);

                await foreach (var hubName in ResolveHubNamesAsync(containerDocument))
                {
                    var hubContext = await serviceManager
                                     .CreateHubContextAsync(hubName, CancellationToken.None)
                                     .ConfigureAwait(false);

                    var negotiation = await hubContext
                                      .NegotiateAsync()
                                      .ConfigureAwait(false);

                    await hubContext.Clients.All
                    .SendAsync(command.CommandAction.ToString(), broadcastMessage)
                    .ConfigureAwait(false);
                }
            }
            else
            {
                throw new NotImplementedException($"Missing orchestrator command handler implementation ICommandHandler<{command.GetTypeName(prettyPrint: true)}> at {GetType()}");
            }
        }
        catch (Exception exc)
        {
            commandResult.Errors.Add(exc);
        }

        return(commandResult);

        async IAsyncEnumerable <string> ResolveHubNamesAsync(object commandResultPayload)
        {
            yield return(commandResultPayload switch
            {
                IProjectContext projectContext => projectContext.GetHubName(),
                IOrganizationContext organizationContext => organizationContext.GetHubName(),
                Organization organization => organization.GetHubName(),
                _ => throw new NotSupportedException($"Unable to resolve hub name for command result payload of type '{commandResultPayload?.GetType()}'.")
            });

            if (commandResultPayload is User user)
            {
                foreach (var membership in user.ProjectMemberships)
                {
                    var project = await projectRepository
                                  .GetAsync(user.Organization, membership.ProjectId)
                                  .ConfigureAwait(false);

                    yield return(project.GetHubName());
                }
            }
        }