![]() |
|
Blazor 0.5.0 experimental release now available - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: C#, Visual Basic & .NET (https://sickgaming.net/forum-79.html) +--- Thread: Blazor 0.5.0 experimental release now available (/thread-85793.html) |
Blazor 0.5.0 experimental release now available - xSicKxBot - 07-26-2018 Blazor 0.5.0 experimental release now available Blazor 0.5.0 is now available! This release explores scenarios where Blazor is run in a separate process from the rendering process. Specifically, Blazor 0.5.0 enables the option to run Blazor on the server and then handle all UI interactions over a SignalR connection. This release also adds some very early support for debugging your Blazor .NET code in the browser! New features in this release:
A full list of the changes in this release can be found in the Blazor 0.5.0 release notes. Get Blazor 0.5.0 To get setup with Blazor 0.5.0:
You can find getting started instructions, docs, and tutorials for Blazor at https://blazor.net. Upgrade an existing project to Blazor 0.5.0 To upgrade an existing Blazor project from 0.4.0 to 0.5.0:
What is server-side Blazor? Blazor is principally a client-side web framework intended to run in a browser where the component logic and DOM interactions all happen in the same process. However, Blazor was built to be flexible enough to handle scenarios where the Blazor app runs apart from the rendering process. For example, you might run Blazor in a Web Worker thread so that it runs separately from the UI thread. Events would get pushed from the UI thread to the Blazor worker thread, and Blazor would push UI updates to the UI thread as needed. This scenario isn’t supported yet, but it’s something Blazor was designed to handle. Another potential use case for running Blazor in a separate process is writing desktop applications with Electron. The Blazor component logic could run in a normal .NET Core process, while the UI updates are handled in the Electron rendering process. We have a working prototype that you can try out of using Blazor with Electron in this way. Blazor 0.5.0 takes the out-of-process model for Blazor and streeeetches it over a network connection so that you can run Blazor on the server. With Blazor 0.5.0 you can run your Blazor components server-side on .NET Core while UI updates, event handling, and JavaScript interop calls are handled over a SignalR connection. There are a number of benefits to running Blazor on the server in this way:
Of course there are some downsides too:
While our primary goal for Blazor remains to provide a rich client-side web development experience, enough developers expressed interest in the server-side model that we decided to experiment with it. And because server-side Blazor uses the exact same component model as running Blazor on the client, it is well aligned with our client-side efforts. Get started with server-side Blazor To create your first server-side Blazor app use the new server-side Blazor project template. Code: dotnet new blazorserverside -o BlazorApp1Build and run the app from the BlazorApp1.Server directory to see it in action: Code: cd BlazorApp1.Server
dotnet runYou can also create a server-side Blazor app from Visual Studio. When you run the Blazor server-side app it looks like a normal Blazor app, but the download size is significantly smaller (under 100KB), because there is no need to download a .NET runtime, the app assembly, or any of its dependencies. You’re also free to run the app under the debugger (F5) as all the .NET logic is running on .NET Core on the server. The template creates a solution with two projects: an ASP.NET Core host project, and a project for your server-side Blazor app. In a future release we hope to merge these two projects into one, but for now the separation is necessary due to the differences in the Blazor compilation model. The server-side Blazor app contains all of your component logic, but instead of running client-side in the browser the logic is run server-side in the ASP.NET Core host application. The Blazor app uses a different bootstrapping script (blazor.server.js instead of blazor.webassembly.js), which establishes a SignalR connection with the server and handles applying UI updates and forwarding events. Otherwise the Blazor programming model is the same. The ASP.NET Core app hosts the Blazor app and sets up the SignalR endpoint. Because the Blazor app runs on the server, the event handling logic can directly access server resources and services. For example, the Code: FetchDataCode: protected override async Task OnParametersSetAsync()
{ forecasts = await ForecastService.GetForecastAsync(StartDate);
}The Code: WeatherForecastServiceStartup model All Blazor projects in 0.5.0 now use a new startup model that is similar to the startup model in ASP.NET Core. Each Blazor project has a Code: StartupCode: ConfigureServicesCode: ConfigureCode: public class Startup
{ public void ConfigureServices(IServiceCollection services) { }
public void Configure(IBlazorApplicationBuilder app) { app.AddComponent<App>("app"); }
}The app entry point in Program.cs creates a Blazor host that is configured to use the Code: StartupCode: public class Program
{ public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); }
public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) => BlazorWebAssemblyHost.CreateDefaultBuilder() .UseBlazorStartup<Startup>();
}In server-side Blazor apps the entry point comes from the host ASP.NET Core app, which references the Blazor Code: StartupCode: public class Startup
{ public void ConfigureServices(IServiceCollection services) { ... services.AddServerSideBlazor<App.Startup>(); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { ... app.UseServerSideBlazor<App.Startup>(); }
}While the server-side Blazor project may also have a Program class, it is not used when running on the server. However it would be used if you switched to client-side (WebAssembly) execution just by changing the Code: <script>The Blazor app and the ASP.NET Core app share the same service provider. Services added in either Code: ConfigureServicesState management When running Blazor on the server the UI state is all managed server-side. The initial state is established with the client connects to the server and is maintained in memory as the user interacts with the app. If the client connection is lost then the server-side app state will be lost, unless it is otherwise persisted and restored by the app. For example, you could maintain your app state in an Code: AppStateJavaScript interop changes You can use JavaScript interop libraries when using server-side Blazor. The Blazor runtime handles sending the JavaScript calls to the browser and then sending the results back to the server. To accommodate out-of-process usage of JavaScript interop the JavaScript interop model was significantly revised and expanded upon in this release. Calling JavaScript from .NET To call into JavaScript from .NET use the new Code: IJSRuntimeCode: JSRuntime.CurrentCode: InvokeAsync<T>Code: IJSRuntimeCode: windowCode: window.someScope.someFunctionCode: someScope.someFunctionCode: TexampleJsInterop.js Code: window.exampleJsFunctions = { showPrompt: function (message) { return prompt(message, 'Type anything here'); }
};ExampleJsInterop.cs Code: using Microsoft.JSInterop;
public class ExampleJsInterop
{ public static Task<string> Prompt(string message) { // Implemented in exampleJsInterop.js return JSRuntime.Current.InvokeAsync<string>( "exampleJsFunctions.showPrompt", message); }
}The Code: IJSRuntimeCode: IJSInProcessRuntimeCode: Invoke<T>Calling .NET from JavaScript To invoke a static .NET method from JavaScript use the Code: DotNet.invokeMethodCode: DotNet.invokeMethodAsyncCode: [JSInvokable]Code: JSInvokableAttributeJavaScriptInteroperable.cs Code: public class JavaScriptInvokable
{ [JSInvokable] public static Task<int[]> ReturnArrayAsync() { return Task.FromResult(new int[] { 1, 2, 3 }); }
}dotnetInterop.js Code: DotNet.invokeMethodAsync(assemblyName, 'ReturnArrayAsync').then(data => ...)New in Blazor 0.5.0, you can also call .NET instance methods from JavaScript. To invoke a .NET instance method from JavaScript you first pass the .NET instance to JavaScript by wrapping it in a Code: DotNetObjectRefCode: invokeMethodCode: invokeMethodAsyncExampleJsInterop.cs Code: public class ExampleJsInterop
{ public static Task SayHello(string name) { return JSRuntime.Current.InvokeAsync<object>( "exampleJsFunctions.sayHello", new DotNetObjectRef(new HelloHelper(name))); }
}exampleJsInterop.js Code: window.exampleJsFunctions = { sayHello: function (dotnetHelper) { return dotnetHelper.invokeMethodAsync('SayHello') .then(r => console.log(r)); }
};HelloHelper.cs Code: public class HelloHelper
{ public HelloHelper(string name) { Name = name; }
public string Name { get; set; }
[JSInvokable] public string SayHello() => $"Hello, {Name}!";
}Output Code: Hello, Blazor!In previous Blazor releases the project build modified index.html to replace the Code: blazor-bootIn Blazor 0.5.0 this mechanism has been replaced. For client-side projects add a script tag that references the _framework/blazor.webassembly.js script (which is generated as part of the build). For server-side projects you reference _framework/blazor.server.js. You can add this script to any HTML file, including server generated content. For example, instead of using the static index.html file from the Blazor client project you could add a Razor Page to your ASP.NET Core host project and then add the Blazor script tag there along with any server-side rendering logic. Render raw HTML Blazor normally renders strings using DOM text nodes, which means that any markup they may contain will be ignored and treated as literal text. This new feature lets you render special Code: MarkupStringWARNING: Rendering raw HTML constructed from any untrusted source is a major security risk! Use the Code: MarkupStringCode: @((MarkupString)myMarkup)
@functions { string myMarkup = "<p class='markup'>This is a <em>markup string</em>.</p>";
}Thanks to a community contribution from Benjamin Vertonghen (vertonghenb) we now have a Visual Studio snippet for adding component parameters. Just type Code: paraCode: TabDebugging Blazor 0.5.0 introduces some very basic debugging support in Chrome for client-side Blazor apps running on WebAssembly. While this initial debugging support is very limited and unpolished it does show the basic debugging infrastructure coming together. To debug your client-side Blazor app in Chrome:
You need to run Chrome with remote debugging enabled to debug your Blazor app. If you don’t, you will get an error page with instructions for running Chrome with the debugging port open so that the Blazor debugging proxy can connect to it. You will need to close all Chrome instances and then restart Chrome as instructed. Once you have Chrome running with remote debugging enabled, hitting the debugging hotkey will open a new debugger tab. After a brief moment the Sources tab will show a list of the .NET assemblies in the app. You can expand each assembly and find the .cs/.cshtml source files you want to debug. You can then set breakpoints, switch back to your app’s tab, and cause the breakpoints to be hit. You can then single-step (F10) or resume (F8). How does this work? Blazor provides a debugging proxy that implements the Chrome DevTools Protocol and augments the protocol with .NET specific information. When you hit the debugging hotkey, Blazor points the Chrome DevTools at the proxy, which in turn connects to the browser window you are trying to debug (hence the need for enabling remote debugging). You might be wondering why we don’t just use browser source maps. Source maps allow the browser to map compiled files back to their original source files. However, Blazor does not map C# directly to JS/WASM (at least not yet). Instead, Blazor does IL interpretation within the browser, so source maps are not relevant. NOTE: The debugger capabilities are very limited. You can currently only:
That’s it! You cannot step into child methods (i.e., F11), observe the values of any locals that aren’t an Code: intCode: stringCode: boolOur friends on the Mono team have done some great work tackling some of the hardest technical problems to enable source viewing, breakpoints, and stepping, but please be patient as completing the long tail of debugger features remains a significant ongoing task. Community The Blazor community has produced a number of great Blazor extensions, libraries, sample apps, articles, and videos. You can find out about these community projects on the Blazor Community page. Recent additions include a Blazor SignalR client, Redux integration, and various community authored samples (Toss, Clock, Chat). If you have a Blazor related project that you’d like to share on the community page let us know by sending us a pull request to the Blazor.Docs repo. Give feedback We hope you enjoy this latest preview release of Blazor. As with previous releases, your feedback is important to us. If you run into issues or have questions while trying out Blazor please file issues on GitHub. You can also chat with us and the Blazor community on Gitter if you get stuck or to share how Blazor is working for you. After you’ve tried out Blazor for a while please also let us know what you think by taking our in-product survey. Click the survey link shown on the app home page when running one of the Blazor project templates: Thanks for trying out Blazor! |