Page 52 - MSDN Magazine, June 2019
P. 52

At that point, you’re welcome to populate the familiar Control- lersfolderandeventheViewsfolderifyouintendtoserveHTML. Note that in ASP.NET Core you can also use POCO controllers, which are plain C# classes decorated to be recognized as control- lers and disconnected from the HTTP context.
The MVC machinery is another great example of terminating middleware. Once the request is captured by the MVC middle- ware, everything goes under its control and the pipeline is abruptly terminated.
It’s interesting to notice that internally the MVC machinery runs its own custom pipeline. It’s not middleware-centric, but it’s nonetheless a full-fledged runtime pipeline that controls how requests are routed to the controller action method, with the generated action result finally rendered to the output stream. The MVC pipeline is made of various types of action filters (action name selectors, authorization filters, exception handlers, custom action result managers) that run beforeandaftereachcontrollermethod.InASP.NETCorecontent negotiation is also buried in the runtime pipeline.
Atamoreinsightfullook,thewholeASP.NETMVCmachinery looks like it’s bolted on top of the newest and redesigned middle- ware-centric pipeline of ASP.NET Core. It’s like the ASP.NET Core pipeline and the MVC machinery are entities of different types just connected together in some way. The overall picture is not much dif- ferent from the way MVC was bolted on top of the now-dismissed Web Forms runtime. In that context, in fact, MVC kicked in through a dedicated HTTP handler if the processing request couldn’t be matched to a physical file (most likely an ASPX file).
Is this a problem? Probably not. Or probably not yet!
Putting SignalR in the Loop
When you add SignalR to an ASP.NET Core application, all you need to do is create a hub class to expose your endpoints. The interesting thing is that the hub class can be completely unrelated to controllers. You don’t need MVC to run SignalR, yet the hub class behaves like a front-end controller for external requests. A method exposed from a hub class can perform any work—even work unrelated to the cross-app notification nature of the frame- work, as shown in Figure 4.
Can you see the picture?
The SignalR hub class can be seen as a controller class, without the whole MVC machinery, ideal for UI-less (or, rather, Razor-less) responses.
Figure 4 Exposing a Method from a Hub Class
Putting gRPC in the Loop
Inversion3.0,ASP.NETCorealsoprovidesnativesupportforthe gRPC framework. Designed along with the RPC guidelines, the framework is a shell of code around an interface definition language that fully defines the endpoint and is able to trigger communication between the connected parties using Protobuf binary serialization over HTTP/2. From the ASP.NET Core 3.0 perspective, gRPC is yet another invokable façade that can make server-side calculations and return values. Here’s how to enable an ASP.NET Core server application to support gRPC:
public void ConfigureServices(IServiceCollection services) {
services.AddGrpc();
}public void Configure(IApplicationBuilder app) {
app.UseRouting(routes => {
routes.MapGrpcService<GreeterService>(); });
Note also the use of global routing to enable the application to sup- port routes without the MVC machinery. You can think of UseRout- ing as a more structured way of defining app.Map middleware blocks.
The net effect of the previous code is to enable RPC-style calls from a client application to the mapped service—the Greeter- Service class. Interestingly, the GreeterService class is conceptually equivalent to a POCO controller, except that it has no need to be recognized as a controller class, as shown here:
public class GreeterService : Greeter.GreeterBase {
public GreeterService(ILogger<GreeterService> logger) {
}
}
The base class—GreeterBase is an abstract class wrapped in a static class—contains the necessary plumbing to carry out the request/response traffic. The gRPC service class is fully integrated with the ASP.NET Core infrastructure and can be injected with external references.
The Bottom Line
Especially with the release of ASP.NET Core 3.0, there will be two more scenarios in which having an MVC-free controller-style façade would be helpful. SignalR has hub classes and gRPC has a service class, but the point is that they’re conceptually the same thing that has to be implemented in different ways for different scenarios. TheMVCmachinerywasportedtoASP.NETCoremoreorlessas it was originally devised for classic ASP.NET, and it maintains its own internal pipeline around controllers and action results. At the same time, as ASP.NET Core is more and more used as a plain pro- vider of back-end services, with no support for views, the need for a possibly unified, RPC-style façade for HTTP endpoints grows.n
Dino Esposito has authored more than 20 books and 1,000-plus articles in his 25-year career. Author of “The Sabbatical Break,” a theatrical-style show, Esposito is busy writing software for a greener world as the digital strategist at BaxEnergy. Follow him on Twitter: @despos.
thanks to the following technical expert for reviewing this article: Marco Cecconi
}
public class SomeHub : Hub {
public void Method1() {
// Some logic
... Clients.All.SendAsync("...");
}
public void Method2() {
// Some other logic
... Clients.All.SendAsync("...");
} }
48 msdn magazine
Cutting Edge


































































































   50   51   52   53   54