Why use ASP.Net Web Api instead of SignalR for an internal project

I know the ASP.NET Web API is designed to create a quiet APIS, while SignalR is designed for real-time exchange. Therefore, they do not compete with technology.

Imagine the following: you are creating a client / server application, you are writing a desktop client that will connect to the server to perform some actions. Actions are triggered by the client, not the server, so both of them work.

If this is an internal application and you are not exposing the API, why are you using Asp.Net Web Api instead of SignalR?

You have methods on the server that will run when the client is called. In Web Api, as actions in controllers, in the R signal in hubs. Both allow you to send parameters to methods and get the result in the client.

Knowing that the traffic in SignalR is slightly lower than in web Api (because in websocket an HTTP connection is established constantly and is not created for each request), I would go for SignalR. Did I miss something?

+6
source share
5 answers

The most pressing reason for working with an infrastructure such as web api is convenience. One advantage is content matching based on the request header. If you ask for json, it will automatically return you json. Same with xml or other standard formats. It also has an excellent formatting system that allows you to support individual needs. It also illuminates the configuration and is easy to configure.

You can create your own framework or even use MVC, WebForms or any other way to set a web endpoint, but usually you will hard-code the format in the response (json, xml, html, etc.)

In any case, at the end of the day, you just need something that will say in terms of http-request-> response.

+1
source

Why not both?

You can use WebAPI to provide bulk data, and SignalR as an extra thing to provide data updates. Thus, you would provide both functionality, the first REST, to allow third-party consumers, and also offer push technology, such as SignalR, or WebSockets directly, to allow subscribers to subscribe to changes in specific data sets.

Please keep in mind that SignalR is not only WebSockets, if so, you need to use Windows 8 or Windows 2012 as a server to use them. Otherwise, it will return to another transport, which may not work as well as you think. In addition, as Daniel noted, SignalR's scalability ... is visible or limited, and even their own documents indicate that you should not use it for real-time scenarios or very segmented data. SignalR is intended for general broadcasting only, I prefer to switch directly to WebSockets using the native Windows API if you are in Windows 8/2012 or a third-party component.

If the client is always the initiator of the action, and the frequency of requests is irregular or low, then, probably, the REST request / response approach simplifies a lot. Otherwise, the client makes requests very often and / or at a constant speed, then go to WebSocket, but you will need to work a little more.

+6
source

SignalR in most cases overruns Request / Response, I would go with REST. And then use SignalR for push updates.

For push updates you can abstract SignalR with this library (Im author) https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy

+2
source

In the pros and cons, you must add the limitation and scalability of each solution. I don’t remember the numbers, but SigalR requires a lot of resources to support the connection, especially with the old browser ( 5000 clients is the default restriction for IIS ). Whereas with WebApi, you focus on how many requests you will have, and not on the number of connected clients (even if they do nothing).

WebApi is also easier to scale. With SignalR, you will need to create a backplane that can become a bottleneck.

In SignalR, if you map users and connections , it’s best to choose a solution that meets future requirements if you plan on adding additional servers.

+1
source

From SignalR Official Page :

ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functions to applications. Real-time web functionality is the ability to instantly access the push content server code for connected clients when it becomes available , and not so that the server expects the client to request new data .

As you describe your problem, you do not need these features. Given that SignalR, in order to provide such functions, causes you to lose some useful HTTP features (caching, content negotiation, ...), you could use the problem for yourself, I would go with the WebAPI.

+1
source

Source: https://habr.com/ru/post/971738/


All Articles