Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
Background:
I'm writing an application that in some cases will have an Blazor UI.
Due to this I want to deliver the Blazor UI part as a plug-in. I already have a few plug-ins that are non UI and that works fine.
Since I'm a newbie when it comes to Blazor the question is probably fairly trivial.

Consider the "out of the box" Blazor demo application from VS. It has the counter and the weather. It works nicely.

What if I would like to compile the demo program as an assembly instead of the default Console application and then have another Console application starting it?

Should be trivial enough, or?
I've learned by trial and error that the second (Console starter app) needs to have the project configured as this

XML
<pre><Project Sdk="Microsoft.NET.Sdk.Web">


instead of the regular
XML
<pre><Project Sdk="Microsoft.NET.Sdk">



in order to work.
However the web contents look awful (since I assume that the wwwroot etc. is not found) and I've tried different ways of setting the some folder to where the web root and pages are but I have failed.

My question is:
What do I need to do to make a secondary console application execute my Blazor application (now compiled as an assembly) and make it look and behave just as it normally would if it was started directly?

What I have tried:

I've tried

C#
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
    {
        ContentRootPath = @"c:\path_to_where_my_contents_is"
    });

but this does not seem to make a difference.
Posted
Updated 9-Sep-22 8:50am

1 solution

You are wanting to have a Blazor Hybrid app. Here is Microsoft's explanation and examples: ASP.NET Core Blazor Hybrid | Microsoft Docs[^]
 
Share this answer
 
Comments
Magnus Sydoff 11-Sep-22 6:11am    
Thank you for your answer @Graeme_Grant

I've tried to read up on Blazor Hybrid, but I can't really see how I should go about it.

Basically the Blazor app looks like this

-----------------------------

public class MyProgram
{
public static void Main(string[] args)
{
new OurTestBlazorApp().Start();
}
}


public class OurTestBlazorApp
{
public void Start()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<weatherforecastservice>();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
}
}

-----------------------------

and it works just as expected (great that is) whereas the regular console application looks like this

-----------------------------

new OurTestBlazorApp().Start();

-----------------------------

When I start the alternative console application (which as you see call the Blazor server app) the functionality of weather forecast (fetch data) and the counter works but it looks awkward with no graphics.


I'm still under the impression that there is just a small setting / tweak somewhere that needs to be done but I might be totally wrong here.

King regards
Magnus
Magnus Sydoff 11-Sep-22 6:25am    
(Since a reply can't seem to contain formatting, I've put an image here instead.
Hopefully it says more than the 1000 words.... ;-) )

https://imgur.com/a/vHrrY5j
Graeme_Grant 11-Sep-22 7:36am    
Watch this: Native client apps with Blazor Hybrid | OD109 - YouTube[^] ... shows you the code and the app running.
Magnus Sydoff 17-Sep-22 8:01am    
Thanks @Graeme_Grant
I've watched the video and really tried hard to apply the ideas mentioned (even though they did not use a console application as starting point). However I still fail to get the UI to show up properly. It is just shown as poor html page without any css or equivalent at all.

I hope that you would be able to give just a short push in the right direction.
Thanks in advance,
Magnus
Graeme_Grant 17-Sep-22 8:13am    
As you saw in the video, it works. I can't help you further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900