aspnet / SignalR-samples

Samples for ASP.NET Core SignalR
753 stars 398 forks source link

Show .NET Core Server Example #82

Open hawkerm opened 5 years ago

hawkerm commented 5 years ago

I want to run a SignalR .NET Core server without an ASP.NET website backing it (like the self-host examples of the old SignalR).

It'd be nice to see an example here of how to do this?

davidfowl commented 5 years ago

I want to run a SignalR .NET Core server without an ASP.NET website backing it (like the self-host examples of the old SignalR).

ASP.NET Core SignalR is built on ASP.NET Core. ASP.NET Core is self hostable, not sure what you mean. The old SignalR used Katana (which was OWIN based), it has no self hosting story outside of that. The moral equivalent is what already works today.

What exactly are you looking for?

hawkerm commented 5 years ago

Thanks @davidfowl, I guess I'm looking for an example that shows setup from a .NET Core Project process self hosting ASP.NET then vs. starting from a ASP.NET Core project. (i.e. how do I set this up from a new VS project done from .NET Core vs. ASP.NET Core).

davidfowl commented 5 years ago

Why?

hawkerm commented 5 years ago

I'm trying to setup a server/client relationship from a .NET Core process, and I wanted to leverage SignalR's client from multiple languages to connect to the .NET core server.

davidfowl commented 5 years ago

You can do that by just making an ASP.NET Core project, I don't understand what you need outside of that.

hawkerm commented 5 years ago

I want the server to be a contained exe vs. needing to be served.

davidfowl commented 5 years ago

I'm not sure what you mean, you can self host ASP.NET Core applications.

hawkerm commented 5 years ago

I guess I don't know much about the differences between how an exe for .NET Core works and how self-hosting an ASP.NET Core application works?

davidfowl commented 5 years ago

Ok that makes more sense. ASP.NET Core is basically a console application (unless you’re running in process on IIS). You can get an exe for your ASP.NET Core application

hawkerm commented 5 years ago

Thanks @davidfowl, that's good to know. Is there any info on the extra overhead in terms of dependencies/package-size when using an ASP.NET Core application over a .NET Core application?

bradygaster commented 5 years ago

Or were you thinking of something like a static site "hosted" using [for example] blobs in a storage account, the HTML of which would just make a connection to a SignalR Hub hosted somewhere (in an EXE, the Azure SignalR Service, whatever), and make use of the SignalR connection as the thing connecting the "website" to the "server?"

bradygaster commented 5 years ago

NM, looks like your desires are more like the self-host examples we had for OWIN (which as @davidfowl points out are done using things like BackgroundService) or Kestrel-hosted apps. The static-file scenario is another one about which I've been asked.

tylerhartwig commented 5 years ago

Is it possible to start a SignalR Core Hub from a console app? It seems like that would be possible according to this thread. My end goal is to setup a hub under a dotnet CLI tool. Is this possible?

If there is another place I should ask about this, I will move my question, here just seemed like the most relevant place.

EDIT: I assume this is as simple as just building an ASP.NET Core host and calling Run(). Does anyone has any experience with this or know any pitfalls in this?

davidfowl commented 5 years ago

Sure, add a reference to ASP.NET Core and party on.

CenturionYun commented 5 years ago

I also encountered the same problem, according to this threads, it seems that we can self host the asp .net core project to work around, but i’m not sure whether asp.net core app can call all windows natives APIs, anyone know this?

davidfowl commented 5 years ago

Yes you can call native windows APIs when running an ASP.NET Core application on windows.

CenturionYun commented 5 years ago

Yes you can call native windows APIs when running an ASP.NET Core application on windows.

That's cool! Thanks for your comments.

devhl-labs commented 4 years ago

An example showing two .net core console apps communicating with SignalR would be fantastic.

bradygaster commented 4 years ago

@devhl-labs - how about an example in which a microservice (a worker-template app) running in Kubernetes, using the .NET SignalR client, and a second client, being a Blazor web assembly client as outlined in this blog post. if that'd suffice, i'll have a demonstration soon of this working.

parad74 commented 4 years ago

Thanks for great examples, But I can’t be sure how to connect Сlient SignalR in WebAPI dotnet Core. Without Server-SignalR on WebAPI Where should be do

`hubConnection = new HubConnectionBuilder()
  .WithUrl(NavigationManager.ToAbsoluteUri(SignalRHub.ChatHub))
               .Build();
    hubConnection.On<string, string>(SignalRHubFunction.ReceiveMessage, (user, message) =>
                {
                    var encodedMsg = user + " says " + message;
                    messages.Add(encodedMsg);
                    StateHasChanged();
                });

                await hubConnection.StartAsync();

as Service in Startup and then use hubConnection get from DI as Service in controller

or hubConnection = new HubConnectionBuilder() in one per controller or each call Action in Contoroller createhubConnection = new HubConnectionBuilder()

I'm not sure if it should be one hubConnection, that starts at startup or the only one on the controller or one per Action.

And how to use DI to use

hubConnection.SendAsync("SendMessage", userInput, messageInput)

in the controller / any Action

What are the best practices? If you ll add a simple example with WebAPI using Client SignalR, that would be great

For example, the Task is : after execute Action in the Controller to send Message to all Client SignalR.

davidfowl commented 4 years ago

What type of application is it? Are you hosting SignalR in a different application or in the same application as the API?

parad74 commented 4 years ago

These are 2 applications Server and WebAPI : My architecture has 1) remote WebAPI application and 2) Server that calls WebAPI. They are located at different addresses and perform different tasks. Hub SignalR is located on Server. I need to configure Client on WebAPI so that it informs everyone about the intermediate steps performed so that the server knows. The whole day I was looking for how to register Client SignslR as Service (in WebAPI) so that Client SignslR would send messages from Controller on WebAPI to Server (Hub SignslR ). I would like it to be convenient as with IHubContext, but at least correctly, even if it is not convenient.