'How ASP.NET Core 6 Web API controller uses signalr server call the client and get return value?

I'm having trouble to get return value from SignalR server-to-client call inside a Web API controller. There are many similar questions for getting return value from server-to-client call. I followed this solution where .NET Core 2 is used.

However I am using .NET 6, just couldn't make it work. Could you please help with it, any help will be appreciated.

This is my Web API controller with SignalR server:

[Route("api/[controller]")]
[ApiController]
public class MiniAppController : ControllerBase
{
    private readonly IHubContext<RpcHub> _rpcHubContext;
    private readonly ILogger<RpcHub> _logger;

    public MiniAppController(IHubContext<RpcHub> rpcHubContext, ILogger<RpcHub> logger)
    {
        _rpcHubContext = rpcHubContext;
        _logger = logger;   
    }

    [HttpGet]
    public async Task<ActionResult<MethodResponse>> Checkout(string comID, string parkServerID, string parkLotID, string parkID, string miniAppID, string miniUserID, string sign)
    {
        string userId = comID + parkServerID + parkLotID;
        MethodResponse response = await _rpcHubContext.Clients.Client("1-1-010").MethodCall(new MethodParams
        {
            MethodCallId = new Guid(),
            Name = "Walker",
            Age = 30
        });

        return Ok(response);
    }
}

I'm getting compilation error:

Error: CS1061 “IClientProxy” doesn't include “MethodCall”definition,couldn't find the first class “IClientProxy” parameter accessiable extension method “MethodCall”(lacking using statement or assembly)



Solution 1:[1]

You could modify your codes follow the RpcCallerBackgroundService class.

dependency injection:

private readonly IRpcCaller<RpcHub> _rpcCaller;

controller:

[HttpPost]

public async Task<ActionResult> Checkout(string comID, string parkServerID, string parkLotID, string parkID, string miniAppID, string miniUserID, string sign)
        {
            .........
           
            MethodResponse response = await _rpcCaller.MethodCall(null, new MethodParams
            {
               MethodCallId= new Guid(),
            });
            ..........

            return Ok(response);
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1