<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
	<channel>
		<title>Refresh Data in Real Time with Blazor WebAssembly and SignalR | Infinum</title>
		<atom:link href="https://infinum.com/blog/real-time-data-blazor-webassembly-signalr/feed/" rel="self" type="application/rss+xml" />
		<link>https://infinum.com/blog/real-time-data-blazor-webassembly-signalr/</link>
		<description>Building digital products</description>
		<lastBuildDate>Thu, 30 Apr 2026 13:44:05 +0000</lastBuildDate>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>

					<item>
				<image>
					<url>32893https://infinum.com/uploads/2023/01/How-to-Refresh-Data-in-Real-Time-with-Blazor-WebAssembly-and-SignalR-hero.webp</url>
				</image>
				<title>Refresh Data in Real Time with Blazor WebAssembly and SignalR</title>
				<link>https://infinum.com/blog/real-time-data-blazor-webassembly-signalr/</link>
				<pubDate>Tue, 31 Jan 2023 14:30:59 +0000</pubDate>
				<dc:creator>Marko Hrnčić</dc:creator>
				<guid isPermaLink="false">https://infinum.com/?p=32893</guid>
				<description>
					<![CDATA[<p>If you're looking to equip your app with real-time data updates, SignalR and Blazor WebAssembly are a perfect combination.</p>
<p>The post <a href="https://infinum.com/blog/real-time-data-blazor-webassembly-signalr/">Refresh Data in Real Time with Blazor WebAssembly and SignalR</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</description>
				<content:encoded>
					<![CDATA[<div
	class="wrapper"
	data-id="es-130"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-92">
	</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-95"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-93">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-94'
	>
	More and more applications today rely on real-time technologies. Data is a critical aspect of every product, and users want to know what’s happening with their data now, not what was happening a minute ago.&nbsp;</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-98"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-96">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-97'
	>
	Countless chat, dashboard, and other applications base their success on the speed and reliability of their data updates. If you’re looking to equip your app with these types of functionalities, this article explains how to use SignalR and Blazor WebAssembly – a perfect combination for displaying real-time data with little effort.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-101"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-99">
	<h2	class='typography typography--size-52-default js-typography block-typography__typography'
	data-id='es-100'
	>
	What is Blazor?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-104"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-102">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-103'
	>
	<a href="https://dotnet.microsoft.com/en-us/apps/aspnet/web-apps/blazor" target="_blank" rel="noreferrer noopener">Blazor</a> is an open-source framework developed by Microsoft used for creating web applications with C# and .NET. This approach makes it easier for .NET developers to enter the world of client web applications. </p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-107"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-105">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-106'
	>
	It might seem unusual to use C# for developing web applications since browsers understand JavaScript, but it can be achieved by compiling a .NET runtime into the WebAssembly bytecode. </p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-110"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-108">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-109'
	>
	In fact, it’s not just C#; we can run any language in the browser through WebAssembly. With Blazor, we can share client and server codes. On top of that, integration with <a href="https://www.docker.com/" target="_blank" rel="noreferrer noopener">Docker</a> and other hosting platforms is available.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-113"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-111">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-112'
	>
	Blazor offers multiple hosting options:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-116"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-114">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-115'
	>
	Blazor Server</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-119"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-117">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-118'
	>
	Blazor WebAssembly</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-122"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-120">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-121'
	>
	Blazor Server</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-125"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-123">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-124'
	>
	This option hosts Razor components in the ASP.NET Core Web App, which means that the application is 100% run on the server. Blazor Server uses <a href="https://dotnet.microsoft.com/en-us/apps/aspnet/signalr" target="_blank" rel="noreferrer noopener">SignalR</a> to communicate between the server and the client via the <a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-7.0" target="_blank" rel="noreferrer noopener">WebSockets protocol</a>. It allows for full .NET API compatibility. </p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-128"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-126">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-127'
	>
	With Blazor Server, each new browser window requires a new instance of a server-managed component state. Because of that, there is a constant bi-directional connection between the client and server, so it would be smart to consider scaling the application.</p></div>	</div>
</div>
</div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-133"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-131"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-132">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2023/01/image1.webp"
					class="image__img block-media__image-img"
					alt=""
										height="403"
															width="636"
										loading="lazy"
					 />
					</picture>

			<figcaption class="image__figcaption block-media__image-figcaption">
			Source: <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/?view=aspnetcore-7.0" target="_blank" rel="noreferrer noopener">Microsoft documentation</a>		</figcaption>
	</figure></div></div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-160"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-134">
	

</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-137"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-135">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-136'
	>
	Blazor WebAssembly</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-140"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-138">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-139'
	>
	Blazor WebAssembly (WASM) allows us to build SPA (Single Page Application) client applications. Opening the application in a new browser window will download the source code.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-143"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-141">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-142'
	>
	However, this is not the only reason to use Blazor WebAssembly. Standard libraries and NuGet packages are still available with Blazor WASM. Another essential aspect is its performance, which is close to native.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-146"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-144">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-145'
	>
	Since WebAssembly runs in the JavaScript sandbox, we can use the following:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-149"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-147">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-148'
	>
	File API</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-152"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-150">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-151'
	>
	DOM</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-155"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-153">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-154'
	>
	Web Sockets</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-158"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-156">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-157'
	>
	Utilizing C# and JavaScript capabilities at the same time? Not bad at all. </p></div>	</div>
</div>
</div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-163"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-161"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-162">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2023/01/image4-1.webp"
					class="image__img block-media__image-img"
					alt=""
										height="402"
															width="477"
										loading="lazy"
					 />
					</picture>

			<figcaption class="image__figcaption block-media__image-figcaption">
			Source: <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/?view=aspnetcore-7.0" target="_blank" rel="noreferrer noopener">Microsoft documentation</a>		</figcaption>
	</figure></div></div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-204"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-164">
	

</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-167"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-165">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-166'
	>
	Blazor WebAssembly ASP.NET Core Hosted</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-170"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-168">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-169'
	>
	There is a new hosting option that comes with Blazor WebAssembly. It is an upgraded version of the standard Blazor WASM, which works well with browsers. </p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-172"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-highlighted-text">
	<p	class='typography typography--size-24-text js-typography block-highlighted-text__typography'
	data-id='es-171'
	>
	<strong>If we choose Blazor WASM ASP.NET Core Hosted, the Blazor client and the backend code are hosted on the same website. On the other hand, with the standard Blazor WASM option, the backend app doesn’t have to be ASP.NET Core. It can be anything, hosted anywhere.</strong></p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-175"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-173">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-174'
	>
	If you’re a C# lover who wants to use C# and .NET for everything, this hosting option is your best bet.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-178"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-176">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-177'
	>
	In our example, we will use .NET Web API for the backend and Blazor WASM for the frontend. In our case, the communication happens through SignalR, but there are other possibilities with this hosting option. On the other hand, Blazor Server communication is restricted to SignalR only.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-181"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-179">
	<h2	class='typography typography--size-52-default js-typography block-typography__typography'
	data-id='es-180'
	>
	Differences between Blazor hosting options</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-184"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-182">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-183'
	>
	All Blazor hosting options have a specific purpose and can be useful in different cases. Here’s an overview of the pros and cons of each option.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-187"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-185">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-186'
	>
	Blazor Server pros and cons</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-190"
	 data-animation-target='inner-items'>
		
			<div class="block-group" data-id=es-189>
	
<div
	class="wrapper"
	data-id="es-188"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			
<figure class="wp-block-table is-style-regular table-body-white-space--pre-wrap" style="font-size:28px"><table><tbody><tr><td><strong>Pros</strong></td><td><strong>Cons</strong></td></tr><tr><td>Faster initial load; HTML content is pre-rendered, and application libraries are not downloaded</td><td>An internet connection is required</td></tr><tr><td>Support for older browsers that don’t support WebAssembly</td><td>Potential scalability issues because each client requires a connection to the server</td></tr><tr><td>Better security because the source code isn’t sent to the client</td><td>Higher latency because the data is sent from the server to the client</td></tr></tbody></table></figure>
		</div>
	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-193"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-191">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-192'
	>
	Blazor WASM pros and cons</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-196"
	 data-animation-target='inner-items'>
		
			<div class="block-group" data-id=es-195>
	
<div
	class="wrapper"
	data-id="es-194"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			
<figure class="wp-block-table is-style-regular table-body-white-space--pre-wrap" style="font-size:28px"><table><tbody><tr><td><strong>Pros</strong></td><td><strong>Cons</strong></td></tr><tr><td>Offline support is enabled since the source code is downloaded</td><td>Older browsers without WebAssembly support are not available</td></tr><tr><td>Servers are not required</td><td>The initial download size is much larger</td></tr><tr><td>Once the source code is loaded, the performance is close to native.<br>The only thing that needs to be re-rendered is the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" target="_blank" rel="noreferrer noopener">DOM (Document Object Model)</a>, without external API calls</td><td>Limitations regarding .NET tools and debugging capabilities</td></tr></tbody></table></figure>
		</div>
	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-199"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-197">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-198'
	>
	Blazor WASM ASP.NET Core Hosted pros and cons</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-202"
	 data-animation-target='inner-items'>
		
			<div class="block-group" data-id=es-201>
	
<div
	class="wrapper"
	data-id="es-200"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			
<figure class="wp-block-table is-style-regular table-body-white-space--pre-wrap" style="font-size:28px"><table><tbody><tr><td><strong>Pros</strong></td><td><strong>Cons</strong></td></tr><tr><td>Complete solution deployed in one place with one tech-stack</td><td>An internet connection is required</td></tr><tr><td>Works well with database connections since the Web API is the backend</td><td>Client-side debugging is still in its early stages</td></tr><tr><td>Suitable for solutions entirely written in ASP.NET Core</td><td></td></tr></tbody></table></figure>
		</div>
	</div>
</div>	</div>
</div>
</div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-242"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-205">
	

</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-208"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-206">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-207'
	>
	Choosing the hosting option</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-211"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-209">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-210'
	>
	Deciding what would be the best hosting option for our case, we need to ask ourselves what the goal is. We want to create a solution for refreshing data in real time. Because dynamic data is involved, we need a server that will fetch data from the database, and we want to implement all the aspects with .NET and C#.&nbsp;&nbsp;</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-214"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-212">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-213'
	>
	Our best bet is the Blazor WASM ASP.NET Core Hosted option.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-217"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-215">
	<h2	class='typography typography--size-52-default js-typography block-typography__typography'
	data-id='es-216'
	>
	Sending data with Hosted Service and SignalR</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-220"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-218">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-219'
	>
	To send data from the backend Web API to the Blazor Client, we need to create a solution first.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-223"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-221">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-222'
	>
	Creating Blazor WASM ASP.NET Core Hosted solution</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-226"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-224">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-225'
	>
	We can create the Blazor solution in two different ways:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-229"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-227">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-228'
	>
	Command Prompt (.NET CLI)</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-232"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-230">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-231'
	>
	IDE (usually <a href="https://visualstudio.microsoft.com/" target="_blank" rel="noreferrer noopener">Visual Studio</a>)</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-235"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-233">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-234'
	>
	To create a Blazor WASM ASP.NET Core Hosted solution with the command prompt, we can use:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-237"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">em</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token" style="color: #005cc5;">dotnet</span><span class="token"> </span><span class="token" style="color: #d73a49;">new</span><span class="token"> </span><span class="token" style="color: #005cc5;">blazorwasm</span><span class="token"> </span><span class="token" style="color: #d73a49;">-</span><span class="token" style="color: #005cc5;">–hosted</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #d73a49;">/</span><span class="token" style="color: #005cc5;">em</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-240"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-238">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-239'
	>
	We can also create a new solution in Visual Studio:</p></div>	</div>
</div>
</div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-245"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-243"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-244">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2023/01/image3.webp"
					class="image__img block-media__image-img"
					alt=""
										height="285"
															width="630"
										loading="lazy"
					 />
					</picture>

	</figure></div></div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-254"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-246">
	

</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-249"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-247">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-248'
	>
	It is important to select the “ASP.NET Core Hosted” option.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-252"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-250">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-251'
	>
	After the scaffolding is done, we can check the solution structure:</p></div>	</div>
</div>
</div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-257"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-255"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-256">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2023/01/image2.webp"
					class="image__img block-media__image-img"
					alt=""
										height="195"
															width="415"
										loading="lazy"
					 />
					</picture>

	</figure></div></div>		</div>
	</div>

<div
	class="wrapper"
	data-id="es-356"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-258">
	

</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-261"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-259">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-260'
	>
	By default, this hosting option consists of three different projects:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-264"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-262">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-263'
	>
	ReportApp.Client (Blazor WebAssembly)</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-267"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-265">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-266'
	>
	ReportApp.Server (.NET Core Web API)</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-270"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="bullet bullet--left bullet__type--dot bullet__color--black block-bullet__bullet" data-id="es-268">
			<div class="bullet__dot"></div>
		<div class="bullet__content">
		<p	class='typography typography--size-20-text-roman js-typography bullet__paragraph'
	data-id='es-269'
	>
	ReportApp.Shared</p>	</div>
</div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-273"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-271">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-272'
	>
	SignalR server configuration</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-276"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-274">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-275'
	>
	To send data to the client, we need to configure SignalR on the server. First, we need to create a SignalR Hub, which enables communication between the client and the server:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-278"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">public</span><span class="token"> </span><span class="token" style="color: #d73a49;">class</span><span class="token"> </span><span class="token" style="color: #6f42c1;">ReportHub</span><span class="token"> : Hub&lt;List&lt;Report&gt;&gt; &lt;br&gt;</span><span class="token">{</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">br</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">}</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-281"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-279">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-280'
	>
	Our hub is now configured to send a list of reports to the client. However, before the configuration is ready, we also need to add SignalR to the service collection:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-283"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #005cc5;">builder</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">Services</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">AddSignalR</span><span class="token">(</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-286"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-284">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-285'
	>
	Lastly, we need to register SignalR to the request pipeline:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-288"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #005cc5;">app</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">MapHub</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">ReportHub</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">(</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">/reports</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-291"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-289">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-290'
	>
	With this in place, the client can communicate with the server through the specified path.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-294"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-292">
	<h2	class='typography typography--size-36-text js-typography block-typography__typography'
	data-id='es-293'
	>
	Sending data to the client with Background service</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-297"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-295">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-296'
	>
	Great, we are now ready to start sending data to the client.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-300"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-298">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-299'
	>
	We want to send our data to the client continuously in the background, and <a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-7.0&amp;tabs=visual-studio" target="_blank" rel="noreferrer noopener">Hosted Service</a> is the way to go. That way, we can do our job without offering an interface to interact with because we don’t need it. Our background worker already knows what to do. It’s pretty simple; it only needs to send data.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-303"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-301">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-302'
	>
	In our case, we use the <code>BackgroundService</code> base class, which implements <code>IHostedService</code>:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-305"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">public</span><span class="token"> </span><span class="token" style="color: #d73a49;">class</span><span class="token"> </span><span class="token" style="color: #6f42c1;">TimeWorker</span><span class="token"> : BackgroundService
</span></span><span class="line"><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    private</span><span class="token"> </span><span class="token" style="color: #005cc5;">readonly</span><span class="token"> </span><span class="token" style="color: #005cc5;">IHubContext</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">ReportHub</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token"> </span><span class="token" style="color: #005cc5;">_reportHub</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    private</span><span class="token"> </span><span class="token" style="color: #005cc5;">readonly</span><span class="token"> </span><span class="token" style="color: #005cc5;">IServiceProvider</span><span class="token"> </span><span class="token" style="color: #005cc5;">_serviceProvider</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    public</span><span class="token"> </span><span class="token" style="color: #6f42c1;">TimeWorker</span><span class="token">(</span><span class="token" style="color: #6f42c1;">IHu</span><span class="token" style="color: #005cc5;">bContext</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">ReportHub</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token"> </span><span class="token" style="color: #005cc5;">reportHub</span><span class="token">,</span><span class="token"> </span><span class="token" style="color: #005cc5;">IServiceProvider</span><span class="token"> </span><span class="token" style="color: #005cc5;">serviceProvider</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">        _reportHub</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">reportHub</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">        _serviceProvider</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">serviceProvider</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    </span><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    protected</span><span class="token"> </span><span class="token" style="color: #005cc5;">override</span><span class="token"> </span><span class="token" style="color: #005cc5;">async</span><span class="token"> </span><span class="token" style="color: #005cc5;">Task</span><span class="token"> </span><span class="token" style="color: #6f42c1;">ExecuteAsync</span><span class="token">(</span><span class="token" style="color: #6f42c1;">Can</span><span class="token" style="color: #005cc5;">cellationToken</span><span class="token"> </span><span class="token" style="color: #005cc5;">stoppingToken</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">        while</span><span class="token"> (!</span><span class="token" style="color: #6f42c1;">stopp</span><span class="token" style="color: #005cc5;">ingToken</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">IsCancellationRequested</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">        </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">            using</span><span class="token"> </span><span class="token" style="color: #d73a49;">var</span><span class="token"> </span><span class="token" style="color: #005cc5;">scope</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">_serviceProvider</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">CreateScope</span><span class="token">();
</span></span><span class="line"><span class="token" style="color: #005cc5;">            var</span><span class="token"> </span><span class="token" style="color: #005cc5;">reportDataService</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">scope</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">ServiceProvider</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">GetRequiredService</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">IReportDataService</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">(</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">            var</span><span class="token"> </span><span class="token" style="color: #005cc5;">reportData</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">await</span><span class="token"> </span><span class="token" style="color: #005cc5;">reportDataService</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">GetReports</span><span class="token">();
</span></span><span class="line"><span class="token" style="color: #005cc5;">            var</span><span class="token"> </span><span class="token" style="color: #005cc5;">methodName</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">TransferReportData</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">            await</span><span class="token"> </span><span class="token" style="color: #005cc5;">_reportHub</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">Clients</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">All</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">SendAsync</span><span class="token">(
</span></span><span class="line"><span class="token" style="color: #005cc5;">                methodName</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">                reportData</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">                stoppingToken</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">            await</span><span class="token"> </span><span class="token" style="color: #005cc5;">Task</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">Delay</span><span class="token">(</span><span class="token" style="color: #6f42c1;">TimeSpan</span><span class="token">.</span><span class="token" style="color: #6f42c1;">Fr</span><span class="token" style="color: #6f42c1;">omSeconds</span><span class="token">(1), </span><span class="token" style="color: #6f42c1;">stoppin</span><span class="token" style="color: #005cc5;">gToken</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">        </span><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    </span><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-308"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-306">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-307'
	>
	First, the method gets data from the database or any other data source and then sends it to all SignalR clients that are listening. The method is executed every second.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-311"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-309">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-310'
	>
	Next, we need to register the background service in our <code>Program class</code>:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-313"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #005cc5;">builder</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">Services</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">AddHostedService</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">TimeWorker</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">(</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-316"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-314">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-315'
	>
	Paying attention to how we use scoped services in background services is vital. Since a background service is registered as a singleton, all constructor injections will become singletons. To avoid issues, it’s better to use <a href="https://learn.microsoft.com/en-us/dotnet/api/system.iserviceprovider?view=net-7.0" target="_blank" rel="noreferrer noopener"><code>IServiceProvider</code></a> when working with scoped-type instances.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-319"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-317">
	<h2	class='typography typography--size-52-default js-typography block-typography__typography'
	data-id='es-318'
	>
	Listening for SignalR updates on the Blazor client</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-322"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-320">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-321'
	>
	The data is ready to be fetched; there’s no point in waiting. In our client Blazor app, we need to install the <a href="https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client" target="_blank" rel="noreferrer noopener"><code>SignalR.Client</code></a> NuGet package:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-324"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">em</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token" style="color: #005cc5;">dotnet</span><span class="token"> </span><span class="token" style="color: #005cc5;">add</span><span class="token"> </span><span class="token" style="color: #005cc5;">package</span><span class="token"> </span><span class="token" style="color: #005cc5;">Microsoft</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">AspNetCore</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">SignalR</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">Client</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #d73a49;">/</span><span class="token" style="color: #005cc5;">em</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-327"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-325">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-326'
	>
	The first step to get the data is to set up the connection in the Blazor component:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-329"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">private</span><span class="token"> </span><span class="token" style="color: #005cc5;">async</span><span class="token"> </span><span class="token" style="color: #005cc5;">Task</span><span class="token"> </span><span class="token" style="color: #6f42c1;">StartHubConnection</span><span class="token">(</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    var</span><span class="token"> </span><span class="token" style="color: #005cc5;">apiAddress</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">https://localhost:7077</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    var</span><span class="token"> </span><span class="token" style="color: #005cc5;">sensorDataUrl</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token">(</span><span class="token">$</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">{apiAddress}/reports</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    _reportDataHubConnection</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">new</span><span class="token"> </span><span class="token" style="color: #005cc5;">HubConnectionBuilder</span><span class="token">(</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">                    </span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">WithUrl</span><span class="token">(</span><span class="token" style="color: #6f42c1;">sensorDataUrl</span><span class="token">)
</span></span><span class="line"><span class="token" style="color: #005cc5;">                    </span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">Build</span><span class="token">();
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    await</span><span class="token"> </span><span class="token" style="color: #005cc5;">_reportDataHubConnection</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #6f42c1;">StartAsync</span><span class="token">();
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-332"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-330">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-331'
	>
	Here we specify the server URL with the SignalR endpoint, which has to be the same as the one configured on the server. Next, we create a new <a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.signalr.client.hubconnection?view=aspnetcore-7.0" target="_blank" rel="noreferrer noopener"><code>HubConnection</code></a> object and start the connection.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-335"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-333">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-334'
	>
	Our next step is to set a listener to fetch the data from the server:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-337"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">private</span><span class="token"> </span><span class="token" style="color: #005cc5;">void</span><span class="token"> </span><span class="token" style="color: #6f42c1;">SetRefreshDataListener</span><span class="token">(</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    var</span><span class="token"> </span><span class="token" style="color: #005cc5;">methodName</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">TransferReportData</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    _reportDataHubConnection</span><span class="token" style="color: #d73a49;">.</span><span class="token" style="color: #005cc5;">On</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">List</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">Report</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">(</span><span class="token" style="color: #005cc5;">methodName</span><span class="token">,</span><span class="token"> </span><span class="token">(</span><span class="token" style="color: #005cc5;">data</span><span class="token">)</span><span class="token"> </span><span class="token" style="color: #d73a49;">=&gt;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">        reports</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #005cc5;">data</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">        StateHasChanged</span><span class="token">();
</span></span><span class="line"><span class="token" style="color: #005cc5;">     </span><span class="token">}</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-340"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-338">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-339'
	>
	Here we specify the method name that corresponds to the one on the server and store the data in the variable. Later, we can use this variable to generate HTML data on the page. </p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-343"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-341">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-342'
	>
	Finally, the last step is to call the methods from the <a href="https://learn.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-7.0#component-initialization-oninitializedasync" target="_blank" rel="noreferrer noopener"><code>OnInitalizeAsync</code></a> method inside the Blazor component:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-345"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">private</span><span class="token"> </span><span class="token" style="color: #005cc5;">List</span><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #005cc5;">Report</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token" style="color: #d73a49;">?</span><span class="token"> </span><span class="token" style="color: #005cc5;">reports</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">private</span><span class="token"> </span><span class="token" style="color: #005cc5;">HubConnection</span><span class="token" style="color: #d73a49;">?</span><span class="token"> </span><span class="token" style="color: #005cc5;">_reportDataHubConnection</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">protected</span><span class="token"> </span><span class="token" style="color: #005cc5;">override</span><span class="token"> </span><span class="token" style="color: #005cc5;">async</span><span class="token"> </span><span class="token" style="color: #005cc5;">Task</span><span class="token"> </span><span class="token" style="color: #6f42c1;">OnInitializedAsync</span><span class="token">(</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">    await</span><span class="token"> </span><span class="token" style="color: #6f42c1;">StartHubConnection</span><span class="token">();
</span></span><span class="line"><span class="token" style="color: #6f42c1;">    SetRefreshDataListener</span><span class="token">();
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-348"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-346">
	<p	class='typography typography--size-20-text-roman js-typography block-typography__typography'
	data-id='es-347'
	>
	And this is it, success.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-351"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-349">
	<h2	class='typography typography--size-52-default js-typography block-typography__typography'
	data-id='es-350'
	>
	Summarizing our real-time journey</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-354"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-typography" data-id="es-352">
	<p	class='typography typography--size-16-text-roman js-typography block-typography__typography'
	data-id='es-353'
	>
	Step by step, we’ve made our way to successful implementation. You are now ready to start creating your own fantastic real-time solutions with Blazor and SignalR. As you can see, real-time solutions like this are actually not hard to implement. At the same time, they are in high demand for many different scenarios and can definitely come in handy in the future.</p></div>	</div>
</div>
</div>		</div>
	</div><p>The post <a href="https://infinum.com/blog/real-time-data-blazor-webassembly-signalr/">Refresh Data in Real Time with Blazor WebAssembly and SignalR</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</content:encoded>
			</item>
		
	</channel>
</rss>