Explore the essentials of SignalR in ASP.NET Core with this quiz, designed to reinforce understanding of real-time web communication, hubs, connections, and message handling. Perfect for beginners interested in real-time app features and signal-based messaging concepts.
Which type of communication does SignalR primarily enable between clients and servers in an ASP.NET Core application?
Explanation: SignalR is designed to enable real-time bidirectional communication, allowing data to flow instantly between the client and server. One-way asynchronous communication does not support two-way interactivity that SignalR provides. Offline batch processing and static file transfer do not involve live or interactive communication, making them incorrect options.
In SignalR, what is the primary component used for handling messaging between clients and servers?
Explanation: The 'Hub' is the main SignalR component for managing communication between clients and servers, making it the correct answer. A Handler typically refers to request or event handling in other contexts. Policy and Router are unrelated terms in the context of basic SignalR real-time communication.
When building a chat application, which signal should a client listen to in order to receive real-time messages from other users?
Explanation: Clients receive live updates by listening for specific server-to-client methods defined on the hub, enabling real-time interaction in apps like chat rooms. Client-to-server HTTP requests are typically one-way from client to server, not for receiving messages. A background poll is less efficient and not real-time, and static endpoint queries do not support live updates.
What must a custom hub class inherit from to be used with SignalR in ASP.NET Core?
Explanation: A custom hub class must inherit from 'Hub' to properly function with SignalR, as this exposes the necessary infrastructure for messaging. Signal, Receiver, and Broadcaster are not the correct base classes in this scenario. Using any of these incorrect options would result in improper configuration or runtime errors.
Which protocol is used by default for communication between SignalR clients and servers when set up without customization?
Explanation: By default, SignalR uses JSON format, and if supported, runs over a WebSockets connection for efficiency and real-time communication. XML over FTP, YAML over HTTP, and CSV over TCP are not supported or commonly used with SignalR, and these protocols would not provide the required features for real-time web messaging.
What SignalR feature allows you to send messages to a subset of connected clients, such as all users in a specific chat room?
Explanation: SignalR groups enable you to broadcast messages to selected clients, such as those in the same chat room, which is essential for targeted communication. Channels and Queues are not inherent features of SignalR, and Collections refer to data structures, not messaging features in this context.
Which method in a SignalR hub is typically overridden to perform actions when a new client connects?
Explanation: The OnConnectedAsync method is called when a new client connects to the hub, enabling developers to execute logic such as welcoming users or logging connections. OnJoinGroup and ConnectClient are not SignalR lifecycle methods, and InitializeSession does not relate to the connection process in SignalR hubs.
Which service method is used to register SignalR in the service collection during application startup?
Explanation: SignalR is registered with the dependency injection system using the AddSignalR method during application setup. EnableSignal, RegisterMessenger, and UseHub are not standard service methods, and using them would not properly initialize SignalR services.
How do you map a SignalR hub to a URL endpoint in the HTTP request pipeline?
Explanation: The MapHub method is used in the endpoint configuration to associate a hub with a specific URL path. Defining in web.config is unrelated to SignalR endpoint mapping. Static files middleware does not handle hubs, and ConfigureGroup is not a relevant method for this setup.
What is the purpose of a connection ID in SignalR during a real-time session?
Explanation: Each connection in SignalR receives a unique identifier, allowing the server to manage and send messages to specific clients. Connection IDs do not play a role in encrypting data, selecting protocols, or configuring memory—all of which are handled by other processes or settings in real-time web communication.