Introducing Uno WebAssembly Projects and Debugging
Tags: underthehood
Published: 2019-10-11 15:21:23

---
title: Introducing Uno WebAssembly Projects and Debugging
published: true
tags: Under The Hood
canonical_url: https://platform.uno/introducing-uno-webassembly-projects-and-debugging/
---

<section>
		<div>
					<div>
				<div id="thz-text-block-414ca6ce">
<p id="d7bc">The progress on the Uno Platform support for WebAssembly has been steady, along with the mono runtime making large improvements in the support for a debugging experience.</p>
<p id="da39">We’ve added support for a better Visual Studio project integration as well as an experimental debugging experience.</p>
<p id="9659">The next release of the <a href="https://marketplace.visualstudio.com/items?itemName=nventivecorp.uno-platform-addin" target="_blank" rel="nofollow noopener" data-href="https://marketplace.visualstudio.com/items?itemName=nventivecorp.uno-platform-addin">Uno Visual Studio add-in</a> will contain the new project format and the debugger, but you can try it today using the <a href="https://github.com/nventive/Uno/releases" target="_blank" rel="noopener" data-href="https://github.com/nventive/Uno/releases">latest experimental release</a> of the Uno Platform.</p>
<h3 id="9d9d">WebAssembly Project Support for Visual Studio</h3>
<p id="2ef0">Originally, the support for WebAssembly was done with Uno through the creation of a .NET Standard 2.0 project, relying on the use of mono-provided python server. The sole purpose of this server is to serve files with the <code>application/wasm</code>mime type, which can be done through a normal WebApp project.</p>
<p id="2a58">As the mono-wasm debugger has progressed a lot, we decided to look into the first step of improving the Visual Studio support for Uno WebAssembly projects. The newer experience is what a developer would expect in terms of integration. It is similar to a normal Web Project: Pressing Ctrl+F5 now starts the WebAssembly project in your preferred browser.</p>
<p id="37e8">As for most web projects, refreshing the web page is enough for the application to pick up the new changes.</p>
<p id="833f">Using the standard <code>Microsoft.NET.Sdk.Web</code> in project definitions gives all the integrated features such as publishing to Azure or other supported targets :</p>
</div>
<div>
	<div id="thz-column-c8c980208">
	<div>
        <div>
                        <div>	
                <div id="thz-code-snippet-c073a898">
    <pre>&lt;Project Sdk="Microsoft.NET.Sdk.Web"&gt;
  &lt;PropertyGroup&gt;
    &lt;OutputType&gt;Exe&lt;/OutputType&gt;
    &lt;TargetFramework&gt;netstandard2.0&lt;/TargetFramework&gt;
    &lt;WasmHead&gt;true&lt;/WasmHead&gt;
    &lt;DefineConstants&gt; __WASM__ &lt;/DefineConstants&gt;
  &lt;/PropertyGroup&gt;

  &lt;ItemGroup&gt;
    &lt;PackageReference Include="Uno.UI" Version="1.42.0-dev.395" /&gt;
    &lt;DotNetCliToolReference Include="Uno.Wasm.Bootstrap.Cli" Version="1.0.0-dev.112" /&gt;
  &lt;/ItemGroup&gt;</pre>
</div> </div>
                                            </div>
    </div>
</div>
</div>
<div id="thz-text-block-26b4e874">
<p id="c355">For those of you that are already running WebAssembly Uno projects, there are not many changes to make, aside from adding <code>Microsoft.NET.Sdk.Web</code> at the top and add the new <code>DotNetCliToolReference</code> to <code>Uno.Wasm.Bootstrap.Cli</code>, which enables the Visual Studio integration.</p>
<h3 id="924b">Debugging C# in WebAssembly</h3>
<p id="777b">The Mono team has been working for a while on the debugging experience, and recent improvements have made it easier to work with it. It is still experimental, but still gives significant help in debugging WebAssembly projects.</p>
<p id="5995">There is only one important constraint: the debugging only works with Chrome. It is using the <a href="https://chromedevtools.github.io/devtools-protocol/" target="_blank" rel="nofollow noopener" data-href="https://chromedevtools.github.io/devtools-protocol/">Chrome DevTools Protocol</a>.</p>
<p id="de72">The debugger provides access to all local sources, as well as <a href="https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/sourcelink" target="_blank" rel="nofollow noopener" data-href="https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/sourcelink">Source Link enabled assemblies</a> when available, provide single stepping, breakpoints, stack trace walking, local variables and watches. Currently, local variables watching is limited to primitives and strings.</p>
<p id="cc89">To give you an example of the debugging experience, this is a step into the <code>DateTime.Now</code> property in the mono source tree, from a normal user program:</p>
</div>
<div id="thz-media-image-67f4faa8"><div><img width="800" height="306" src="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161550/0-QTf0jNUgFoP5fkty.png" alt="0-QTf0jNUgFoP5fkty" srcset="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161550/0-QTf0jNUgFoP5fkty.png 800w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161550/0-QTf0jNUgFoP5fkty-300x115.png 300w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161550/0-QTf0jNUgFoP5fkty-768x294.png 768w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161550/0-QTf0jNUgFoP5fkty-570x218.png 570w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161550/0-QTf0jNUgFoP5fkty-350x134.png 350w" sizes="(max-width: 800px) 100vw, 800px"></div></div>
<div id="thz-text-block-becc0a4e">
<p id="2f84">The Mono team recently added the ability to get the source files from debugging information for the Mono binaries, making it quite easy for debuggers to pull exact source files for published binaries.</p>
<p id="77cf">One bit of a warning, the debugging experience is very rough on the edges. The debugger may crash frequently (with Chrome saying a WebSocket got disconnected) as there are many non-sunny-path scenarios that have not been tested yet. This means that you may not be able to set breakpoints, step into or step out of some specific code sections (e.g. partial methods).</p>
<h3 id="11e2">Using the WebAssembly C# Debugger</h3>
<ul>
<li id="c52d">Create an Uno Platform Cross-Targeted application from the Uno Solution Template (See at the end of this post for more details)</li>
<li id="d686">Set the Wasm project as a startup project</li>
</ul>
<p id="a600">Ensure that</p>
<ul>
<li id="d9bb">IIS Express is enabled in your debugging toolbar</li>
<li id="1939">Chrome is active WebBrowser</li>
</ul>
</div>
<div id="thz-media-image-8c035354"><div><img width="301" height="135" src="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161642/0-UO4KchjAtosect4d.png" alt="0-UO4KchjAtosect4d" srcset="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161642/0-UO4KchjAtosect4d.png 301w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161642/0-UO4KchjAtosect4d-300x135.png 300w" sizes="(max-width: 301px) 100vw, 301px"></div></div>
<div id="thz-text-block-86c08bdb"><ul>
<li id="b6e4">Press <em>Ctrl+F5</em>
</li>
<li id="5857">Once your application is loaded, press Alt+Shift+D, a new tab will open</li>
<li id="a35f">You will get an error message telling you that Chrome has not been opened</li>
</ul></div>
<div id="thz-media-image-1b1dea5a"><div><img width="570" height="217" src="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161715/0-vqVXcqNmqhxrkt_N-570x217.png" alt="0-vqVXcqNmqhxrkt_N" srcset="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161715/0-vqVXcqNmqhxrkt_N-570x217.png 570w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161715/0-vqVXcqNmqhxrkt_N-300x114.png 300w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161715/0-vqVXcqNmqhxrkt_N-768x292.png 768w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161715/0-vqVXcqNmqhxrkt_N-350x133.png 350w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161715/0-vqVXcqNmqhxrkt_N.png 800w" sizes="(max-width: 570px) 100vw, 570px"></div></div>
<div id="thz-text-block-79c6b653"><ul>
<li id="f03e">Close all your chrome instances and run Chrome again using the provided command line</li>
<li id="a84b">Once again, once your application is loaded, press Alt+Shift+D, a new tab will open.</li>
<li id="eecf">You will now get the Chrome DevTools to open listing all the .NET loaded assemblies on the Sources tab:</li>
</ul></div>
<div id="thz-media-image-5f8c99b4"><div><img width="570" height="338" src="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161746/0-lAv_ov_16-H_qnDM-570x338.png" alt="0-lAv_ov_16-H_qnDM" srcset="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161746/0-lAv_ov_16-H_qnDM-570x338.png 570w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161746/0-lAv_ov_16-H_qnDM-300x178.png 300w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161746/0-lAv_ov_16-H_qnDM-350x208.png 350w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161746/0-lAv_ov_16-H_qnDM.png 644w" sizes="(max-width: 570px) 100vw, 570px"></div></div>
<div id="thz-text-block-899c5d5c"><ul>
<li id="f009">You can now set break points in the available source files</li>
<li id="5f2b">Since the app’s initialization has already started, you can refresh the original website tab, or use the smaller refresh button in the preview section of the Chrome DevTools:</li>
</ul></div>
<div id="thz-media-image-28ed7c66"><div><img width="442" height="166" src="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161818/0-1nOqt-vYfQC6zqw.png" alt="0-1nOqt-vYfQC6zqw" srcset="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161818/0-1nOqt-vYfQC6zqw.png 442w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161818/0-1nOqt-vYfQC6zqw-300x113.png 300w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161818/0-1nOqt-vYfQC6zqw-350x131.png 350w" sizes="(max-width: 442px) 100vw, 442px"></div></div>
<div id="thz-text-block-0b855839">
<h3 id="eb5a">How does it work?</h3>
<p id="387b">The structure of the application is a bit complex, and a drawing is more informative than a thousand words:</p>
</div>
<div id="thz-media-image-2215930f"><div><img width="570" height="237" src="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161846/0-ol-mnHRPvq7UkcSc-570x237.png" alt="0-ol-mnHRPvq7UkcSc" srcset="https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161846/0-ol-mnHRPvq7UkcSc-570x237.png 570w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161846/0-ol-mnHRPvq7UkcSc-300x125.png 300w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161846/0-ol-mnHRPvq7UkcSc-768x319.png 768w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161846/0-ol-mnHRPvq7UkcSc-350x145.png 350w, https://s3.amazonaws.com/uno-website-assets/wp-content/uploads/2019/05/03161846/0-ol-mnHRPvq7UkcSc.png 800w" sizes="(max-width: 570px) 100vw, 570px"></div></div>
<div id="thz-text-block-4513cf36">
<p id="1ecd">The debugger proxy is an ASP.NET Core component from Mono, that is embedded inside the ASP.NET Core static file server used during development. Its role is mainly to provide access to PDB files, interpret the Chrome DevTools debugger commands and translate them back for the mono-wasm runtime to understand. This includes setting breakpoints, getting watch values, getting source files, etc…</p>
<p id="90e4">For the moment, the Uno Platform is using a custom build of the debugging proxy which adds support for Mono sources and source link, as well as other fixes. Those will make their way into the main Mono repository soon.</p>
<p id="6e28">If you’re familiar with Blazor, you’ll notice that the debugger experience is very similar, and it is, the ASP.NET Core integration layer is directly inspired by Blazor’s 0.7.0 debugger support implementation.</p>
<h3 id="dbb7">What’s next?</h3>
<p id="5ead">We’ve been using the debugger for a few days now, and it’s allowed us to improve our troubleshooting velocity. Having a proper stack trace and variable watch is always of great help, where Console. Write can fall short.</p>
<p id="54f8">We’ll continue to work with the mono team to improve the deployment and debugging experience of mono-wasm applications.</p>
<p id="50f1">Let us know what you think!</p>
</div> </div>
				</div>
				</section>

The post [Introducing Uno WebAssembly Projects and Debugging](https://platform.uno/introducing-uno-webassembly-projects-and-debugging/) appeared first on [Uno Platform](https://platform.uno).