简体   繁体   English

.NET Blazor 在 IIS 上运行的服务器返回空集合?

[英].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.我正在经历与 .NET Blazor 服务器的绑定,并试图列出系统上所有可用的显示。

Starting from a new project, keeping it as simple as possible, I have a model:从一个新项目开始,尽可能简单,我有一个 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:一个 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:一个程序.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: Websocket 协议已启用,网站已发布。如果出现以下任一情况,则运行良好:
1 - I run from within visual studio using iis express 1 - 我使用 iis express 从 visual studio 中运行
2 - within the publish folder I start the.exe file, then navigate to the address provided 2 - 在发布文件夹中,我启动 .exe 文件,然后导航到提供的地址

在此处输入图像描述

However, it does not work and the collection is empty if I run using IIS但是,如果我使用 IIS 运行,它不起作用并且集合为空

在此处输入图像描述

GitHub Repo: here GitHub 回购:这里

Anything incorrect with the process / code?过程/代码有什么不正确的地方吗? Limitation from IIS?来自 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. WindowsDisplayAPI 用于客户端应用程序,如 winform 和 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.我知道它在 IISExpress 中工作,通过单击 .exe 文件,在开发模式下,它也属于客户端 model。

  3. Even it works in iis production environment, but this code not make sense.即使它在 iis 生产环境中工作,但这段代码没有意义。 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. IIS 作为 web 服务器,不允许显示更多设备信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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