2

I'm trying to create a Azure function with a Cosmos Trigger (in c#) that monitors changes to a CosmosDB, this seems relatively simple and I have managed to do this without trouble, I have an Azure function that logs changes to the DB to the log console. I am trying to write an output binding to send the changes to Azure SignalR but when I try this, I am met with isolated process doesn't support the assemblies required to do this. Does anyone have a very simple example of a c# Azure Cosmos Trigger function that sends the changes detected to a Cosmos DB to an Azure SignalR servicee so I can subscribe to this and report these to a client app. Any help would be greatly appreciated.

The code I found on the web for what I want to do (this is just a test function) is below:

    [Function("CosmosTriggerSigR")]
    public void Run([CosmosDBTrigger(
        databaseName: "test",
        collectionName: "testCollection",
        ConnectionStringSetting = "cosmos_DOCUMENTDB",
        LeaseCollectionName = "leases")] 
        IReadOnlyList<MyDocument> input,
        [SignalR(HubName = "events", ConnectionStringSetting = "SignalRConnectionString")] 
            IAsyncCollector<SignalRMessage> signalRMessages,
        ILogger log)
    {
        if (input != null && input.Count > 0)
        {
            _logger.LogInformation("Documents modified: " + input.Count);
            _logger.LogInformation("First document Id: " + input[0].Id);
        }
    }

and the when trying to deploy it, it shows this error:

C:\Users\CosmosSigr1204\CosmosTriggerSigR.cs(29,14): error AZFW0001: The attribute 'SignalRAttribute' is a WebJobs attribute and not supported in the .NET Worker (Isolated Process). [C:\Users\CosmosSigr1204\CosmosSigr1204.csproj] The terminal process "C:\Program Files\dotnet\dotnet.exe 'publish', '--configuration', 'Release', '/property:GenerateFullPaths=true', '/consoleloggerparameters:NoSummary'" terminated with exit code: 1.

I know pretty much nothing about Azure SignalR and I'm trying to muddle through so apologies if the code above isn't what it should be for what I'm trying to do.

  • Welcome! Please edit to include relevant code, actual errors, etc. Currently, your question is just a high level description of the issue, along with a request for sample code. – David Makogon Apr 13 '22 at 10:05
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 13 '22 at 12:36

1 Answers1

0

Here is a full solution that uses the combination of services you specify: https://github.com/ealsur/serverlessazurefriday

Particularly this Function: https://github.com/ealsur/serverlessazurefriday/blob/master/src/DistributedDashboard/NotificationsTrigger.cs which ties the CosmosDBTrigger and SignalR output like so:

[FunctionName("Notifications")]
        public static async Task Run(
            [CosmosDBTrigger(
                databaseName: "eventsDb",
                collectionName: "events",
                LeaseCollectionPrefix = "Notifications",
                ConnectionStringSetting = "CosmosDBAzureFriday",
                PreferredLocations = "%REGION%",
                LeaseCollectionName = "leases")]
                IReadOnlyList<Document> events,
            [SignalR(HubName = "events", ConnectionStringSetting = "SignalRAzureFriday")] 
                IAsyncCollector<SignalRMessage> signalRMessages,
            ILogger log)
        {
            await signalRMessages.AddAsync(new SignalRMessage()
            {
                Target = "console",
                Arguments = new[] {
                    events.Select(singleEvent => JsonConvert.DeserializeObject<ConsoleLog>(singleEvent.ToString()))
                }
            });
        }

In this case, the code will send 1 SignalR message containing all the documents that were received on the Trigger, you could opt to send 1 SignalR message per Trigger document, that is up to your application design.

In the case of this solution, the client application (browser) connects to the SignalR hub using the JS library and receives the SignalR message that contain all the Cosmos DB documents and consumes the array (reference https://github.com/ealsur/serverlessazurefriday/blob/master/src/ClientApp/scripts/site.js):

// Assuming connection is a SignalR connection created by SignalR library
// https://learn.microsoft.com/aspnet/core/signalr/javascript-client?#connect-to-a-hub

connection.on('console', function(messages){
                angular.forEach(messages, function(message){
                    term.writeln('>> Device '+message.deviceId +' in ' + message.region + ' reports value of ' + message.value);
                });
            });

Where console is just the name that matches the Target on the SignalR output message.

Matias Quaranta
  • 13,907
  • 1
  • 22
  • 47
  • This is pretty much what I have tried but I get the error: The attribute 'SignalRAttribute' is a WebJobs attribute and not supported in the .NET Worker (Isolated Process). when trying to deploy it. I have looked at the example you have stated but can't get it to work – Peter Bennett Apr 13 '22 at 14:18
  • See https://stackoverflow.com/a/67996069/5641598 – Matias Quaranta Apr 13 '22 at 14:57