SignalR-samples icon indicating copy to clipboard operation
SignalR-samples copied to clipboard

Show .NET Core Server Example

Open hawkerm opened this issue 6 years ago • 22 comments

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?

hawkerm avatar Feb 18 '19 05:02 hawkerm

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?

davidfowl avatar Feb 18 '19 06:02 davidfowl

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).

hawkerm avatar Feb 18 '19 21:02 hawkerm

Why?

davidfowl avatar Feb 18 '19 21:02 davidfowl

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.

hawkerm avatar Feb 18 '19 23:02 hawkerm

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

davidfowl avatar Feb 19 '19 00:02 davidfowl

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

hawkerm avatar Feb 19 '19 03:02 hawkerm

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

davidfowl avatar Feb 19 '19 04:02 davidfowl

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?

hawkerm avatar Feb 19 '19 04:02 hawkerm

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

davidfowl avatar Feb 19 '19 05:02 davidfowl

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?

hawkerm avatar Feb 20 '19 04:02 hawkerm

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 avatar Feb 20 '19 04:02 bradygaster

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.

bradygaster avatar Feb 20 '19 04:02 bradygaster

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?

tylerhartwig avatar Apr 19 '19 17:04 tylerhartwig

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

davidfowl avatar Apr 19 '19 18:04 davidfowl

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?

CenturionYun avatar Jul 06 '19 00:07 CenturionYun

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

davidfowl avatar Jul 06 '19 04:07 davidfowl

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

That's cool! Thanks for your comments.

CenturionYun avatar Jul 08 '19 00:07 CenturionYun

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

devhl-labs avatar Nov 17 '19 16:11 devhl-labs

@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.

bradygaster avatar Feb 07 '20 20:02 bradygaster

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.

parad74 avatar Feb 11 '20 08:02 parad74

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

davidfowl avatar Feb 11 '20 15:02 davidfowl

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.

parad74 avatar Feb 11 '20 16:02 parad74