Search code examples
c#uwpasp.net-core-signalr

How to receive message from the asp.net core using signalR core to UWP


SignalR core was demo with javascript client or Angular My case is using UWP to render the front-end. While Microsoft only tell how to Invoke the message from client to server, It's docs didn't show how to receive message [https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2][1]

Here is my server:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddSingleton<IInventoryServices, InventoryServices>();
        services.AddSignalR();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseSignalR(route =>
        {
            route.MapHub<MessageHub>("/hub");
        });

        app.UseMvc();
    }
}

this is the controller:

[Route("api/hub")]
[ApiController]
public class MessController : Controller
{
    private IHubContext<MessageHub> _messhubContext;

    public MessController(IHubContext<MessageHub> messhubContext)
    {
        _messhubContext = messhubContext;
    }

    public ActionResult Post()
    {
        _messhubContext.Clients.All.SendAsync("send", "Strypper", "Howdy");
        System.Diagnostics.Debug.WriteLine("I'm here");
        return Ok();
    }

And here is the hub:

public class MessageHub : Hub
{
    public Task Send(string user ,string message)
    {
        return Clients.All.SendAsync("Send", user, message);
    }
}

My "PostMan" is messed up and I don't wanna discuss about it. Is there anyone here work with uwp framework could show me the way to receive the message from the server I made ?


Solution

  • Sorry, I originally misunderstood and turned it around.

    For communication for server to client, you have to follow the documentation here.

    You need to define a listener in UWP like this:

    connection.On<string, string>("ReceiveMessage", (user, message) =>
    {
       //do something
    });
    

    And send the message on server side like this:

    await Clients.All.SendAsync("ReceiveMessage", user,message);
    

    Previous answer

    To invoke a Hub method from client, you can use the InvokeAsync method:

    await connection.InvokeAsync("MyMethod", "someparameter");
    

    Then you just create the method in the Hub class

    public class MessageHub : Hub
    {
        public Task Send(string user ,string message)
        {
            return Clients.All.SendAsync("Send", user, message);
        }
    
        public Task MyMethod(string parameter)
        {
            //do something here
        }
    }
    

    There is also an overload of InvokeAsync<TResult> that allows you to create a method with a return type.