Arash
Arash

Reputation: 4260

Raising multiple signalR triggers from the same Azure Http-triggered Function in isolated process context

The following code snippet has been taken from this Microsoft document:

[Function("SignalRFunction")]
[SignalROutput(HubName = "chat", ConnectionStringSetting = "SignalRConnectionString")]
public static MyMessage Run([SignalRTrigger("SignalRTest", "messages", "SendMessage", parameterNames: new string[] { "message" },
    ConnectionStringSetting = "SignalRConnectionString")] string item,
    [SignalRConnectionInfoInput(HubName = "chat")] MyConnectionInfo connectionInfo,
    FunctionContext context)
{
    var logger = context.GetLogger("SignalRFunction");

    logger.LogInformation(item);
    logger.LogInformation($"Connection URL = {connectionInfo.Url}");

    var message = $"Output message created at {DateTime.Now}";

    return new MyMessage()
    {
        Target = "newMessage",
        Arguments = new[] { message }
    };
}

public class MyMessage
{
    public string Target { get; set; }

    public object[] Arguments { get; set; }
}

What I'd like to do is to return multiple instances of MyMessage simultaneously to send a message to different targets. Also, I'd like to return a JSON payload from Run function comprising some other info e.g. a database record, etc.

How can the Run function be augmented to support such a case?

Upvotes: 1

Views: 291

Answers (1)

Mohit Ganorkar
Mohit Ganorkar

Reputation: 2069

  • You can return multiple instances of a class as a list of objects. For that you will have to change the data type of the Run to the list of objects.

  • Here I have used an HTTP trigger, but the concept should also work of SignalIr trigger as well. Where we would add List<TestCLass> as a data type of the Run function where TestClass is the class those instances we are returning.


public class TestClass
    {
        public string Target { get; set; }
  }  

public static List<TestClass> Run()
{
            List<TestClass> tests = new List<TestClass>();
            TestClass t = new TestClass();
            TestClass q = new TestClass();
            t.Target = "Hello World";
            q.Target = "Another Hello World";
            tests.Add(t);
            tests.Add(q);
            return tests;
}

output of the HTTP trigger: enter image description here

  • Also, If you want to connect to other for e.g.: azure service Bus then you will have to create a client for that particular service. for e.g.: Here in this MSDOC the ServieBusClient to connect to azure service Bus. Similarly, you can find clients for other azure services as well

Upvotes: 0

Related Questions