Masood Bhat
Masood Bhat

Reputation: 104

Azure SignalR service Implementation .NET 6 Azure Function Isolated process

I am trying to implement the Azure SignalR service in the Azure function with an Isolated process. So far it's working Ok for me. I read the Microsoft docs, there it mentioned that for registering any output binding, I need to create the azure function for it. Below is my code:

[Function("BroadcastToAll")]
[SignalROutput(HubName = "demohub", ConnectionStringSetting = "AzureSignalRConnectionString")]
public SignalRMessageAction BroadcastToAll([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
    try
    {
        using var bodyReader = new StreamReader(req.Body);
        var d = bodyReader.ReadToEnd();
      return  new SignalRMessageAction("newMessage")
        {
            // broadcast to all the connected clients without specifying any connection, user or group.
            Arguments = new[] { d },
        };
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

My question is it mandatory to have the return type as SignalRMessageAction. How can I let its return type like HttpResponseData.

Upvotes: 3

Views: 692

Answers (1)

Harshitha
Harshitha

Reputation: 7347

is it mandatory to have the return type as SignalRMessageAction

It is not mandatory to have the return type as SignalRMessageAction.

  • We can have any of the return type as mentioned in the MSDoc based on our requirement.

How can I let its return type like HttpResponseData.

When we create Azure Function with SignalR, the default function code will be created with HttpResponseData.

enter image description here

My sample Code:

[Function("negotiate")]
 public HttpResponseData Negotiate(
     [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
     [SignalRConnectionInfoInput(HubName = "HubValue")] MyConnectionInfo connectionInfo)
 {
     _logger.LogInformation($"SignalR Connection URL = '{connectionInfo.Url}'");

     var response = req.CreateResponse(HttpStatusCode.OK);
     response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
     response.WriteString($"Connection URL = '{connectionInfo.Url}'");
     
     return response;
 }
  • The above default code returns HttpResponseData.

Add the SignalRConnectionString in local.settings.json file.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "AzureSignalRConnectionString": "Endpoint=https://****.service.signalr.net;AccessKey=****;Version=1.0;"
  }
}

My .csproj file:

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.12" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.SignalRService" Version="1.2.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.10.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
  </ItemGroup>

Upvotes: 0

Related Questions