Using SIGNALR in ASP. NET 5

Source: Internet
Author: User

(This article also published in my public number "dotnet daily Essence article", Welcome to the right QR code to pay attention to. )

Preface: As a component of Web real-time bidirectional communication in ASP. SIGNALR, it has been developed synchronously in ASP. NET 5. However, the usage is different from the details before, and the information is relatively scarce. This article is a simple guide to getting started.

With SIGNALR, developers can implement bidirectional real-time communication between servers and clients in a Web application developed by ASP. The server can instantly push content to the online client. SIGNALR preferred web sockets as the underlying implementation, and fallback to other compatible technologies for non-modern browsers. It is rich in features, supports link management, group connections, and authorization control.

In the 5 era, SIGNALR also upgraded to SIGNALR 3.x, but JavaScript's client-side library is still a 2.x version. At the same time, the usage is slightly different from the previous one in the ASP. NET 4.x era (such as in ASP. 5), so the reference to the existing document may encounter errors. So how do I use SIGNALR 3 in an ASP. NET 5 Web App, here's a quick Walkthrough:

1, needless to say, start with a new ASP. NET 5 Web Application project

2, after the new success. Open the "Project.json" file in the Solution Explorer and add "Microsoft.AspNet.SignalR.Server" to "dependencies": "3.0.0-rc1-final", To reference SIGNALR's service-side function library

3, expand "Dependencies", right-click the "Bower" node, select "Manage Bower Packages ..." option, in the Bower package installation interface, switch to "Browse", search "SignalR" and install. It is important to note that this writing can only install signalr 2.2.0 JS Library, but does not affect the use of.

4, create a folder (for example, named "Hubs") to include all the hub classes, it is recommended that the specification is not mandatory. Add a class named Chathub to the Hubs folder and inherit the Microsoft.AspNet.SignalR.Hub.

5, in theory, if you only want to push messages from the server to the client, the hub class can not write anything. But in order to achieve a simple chat function, we still write something, write two methods send and join for the client to call. The code is as follows:

public class chathub:hub{public    void Send (String message)    {        var name = Context.Request.HttpContext.Session.GetString ("name");        Clients.All.addNewMessageToPage (name, message);    }    public void Join (string name)    {        Context.Request.HttpContext.Session.SetString ("name", name);}    }

6, and then add 2 lines of code services to the "configureservices" in the Startup.cs file. ADDSIGNALR (); services. Addtransient<chathub> ();. (The purpose of the second line of code is to later invoke the client method outside the hub, it is convenient to do dependency injection.) Next, add 1 lines of code to the "Configure" app. USESIGNALR ();

7, in the view file to implement the interface to join, send messages, display the message, the Code basic Reference (http://www.asp.net/signalr/overview/getting-started/ TUTORIAL-GETTING-STARTED-WITH-SIGNALR-AND-MVC) in the. I added the ability to join the chat. In addition, the address of the automatically generated hub proxy JS file is changed from "Signalr/hubs" to "Signalr/js". This address, can be configured in startup, but the default value is this and different from before.

8, to send a message from the hub to the client, you need to use a dependency injection reference Ihubcontext<chathub> in the server's code, and call the dynamic method directly. The code is as follows:

Private readonly ihubcontext<chathub> _hub;public HomeController (ihubcontext<chathub> hub) {    _hub = Hub;} Public Iactionresult sendmessagetoclients (String message) {    _hub. Clients.All.sendMessageFromServer (message);    Return Content ("

Complete project code share address: Https://zyg.blob.core.windows.net/share/ASP.NET%205%20SignalR.zip

Using SIGNALR in ASP. NET 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.