简体   繁体   中英

.NET Blazor server running on IIS returning empty collection?

I am experiencing bindings with .NET Blazor server, and trying to list all displays available on the system.

Starting from a new project, keeping it as simple as possible, I have a model:

using CommunityToolkit.Mvvm.ComponentModel;

namespace BindingTest.Data;

public class DisplayInfo : ObservableObject
{
    private string deviceName = string.Empty;
    public string DeviceName
    {
        get { return deviceName; }
        set { deviceName = value; OnPropertyChanged(nameof(DeviceName)); }
    }

    private bool primary;
    public bool Primary
    {
        get { return primary; }
        set { primary = value; OnPropertyChanged(nameof(Primary)); }
    }
}

a class:

using System.Collections.ObjectModel;
using WindowsDisplayAPI;

namespace BindingTest.Data;

public class DisplayService
{
    public ObservableCollection<DisplayInfo> ScreenCollection { get; set; } = new();

    public DisplayService()
    {
        foreach (Display display in Display.GetDisplays())
        {
            ScreenCollection.Add(new DisplayInfo
            {
                DeviceName = display.DisplayName,
                Primary = display.IsGDIPrimary,
            });
        }
    }
}

a page:

@page "/displays"
@using BindingTest.Data;
@using System.Collections.ObjectModel;

<PageTitle>Displays</PageTitle>

@inject DisplayService display

<h1>Display Service</h1>

@if (display.ScreenCollection == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <p>Total items in collection: @display.ScreenCollection.Count</p>

    <table class="table">
        <thead>
            <tr>
                <th>Device Name</th>
                <th>Primary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var display in display.ScreenCollection)
            {
                <tr>
                    <td>@display.DeviceName</td>
                    <td>@display.Primary.ToString()</td>
                </tr>
            }
        </tbody>
    </table>
}

a program.cs:

using BindingTest.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<DisplayService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

Websocket protocol is enabled, site is published.. This runs very well if either:
1 - I run from within visual studio using iis express
2 - within the publish folder I start the.exe file, then navigate to the address provided

在此处输入图像描述

However, it does not work and the collection is empty if I run using IIS

在此处输入图像描述

GitHub Repo: here

Anything incorrect with the process / code? Limitation from IIS? Apologies for the long post, not sure what I am doing incorrectly. Maybe someone can suggest a good tutorial as well?

In short, this is expected behavior and cannot be achieved in your code.

Why

  1. WindowsDisplayAPI is used for client side app, like winform and wpf.

  2. I know it's works in IISExpress and by clicking the.exe file, when in the development mode, it also belongs to client model.

  3. Even it works in iis production environment, but this code not make sense. Because the compile file in the server, it will show the server side screen, right?

  4. IIS as web server, it not allow show more device info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM