运行 Web 应用与 Service Fabric 分开

Run Web app separately from Service Fabric

我里面有 Service Fabric 和 Web 服务。如果我运行本地的Service Fabric,从Visual Studio,我可以调试我的服务,非常方便。

但是,将我的代码更改部署到本地结构需要花费很多时间。 我确信应该有一个选项可以独立于 Service Fabric 启动我的服务。 看来我需要更新我的方法 'Main',以便它在开发环境中以不同方式启动服务。

知道应该更改哪些内容吗?

您的服务是否使用任何 Service Fabric 功能(远程处理?自定义侦听器?等)。

如果,那么如果没有 Service Fabric 运行时间.[=19,那么您的服务不能运行 =]

如果您的服务是通过 WebHost 配置的 ASP.NET 核心服务,那么您可以尝试以下方法:

  1. 将配置 WebHost 的代码分离到单独的静态方法中。使用此方法在 Service Fabric 服务中初始化 WebHost
  2. Program.Main 中检查 Fabric_ApplicationName environment variable (this can be done using Environment.GetEnvironmentVariable 方法 )。

    This variable is defined by Service Fabric runtime so if it is defined then you code is running inside Service Fabric.

    如果定义了 Fabric_ApplicationName 变量,则只需继续 Service Fabric 服务初始化代码,否则使用先前定义的静态方法直接初始化 WebHost 和 运行 的实例来自 Program.Main.

我不确定这是否是您要找的,所以如果您有任何其他问题 - 请提问。

希望对您有所帮助。

Oleg Karasik 的回应有助于制定解决方案。 我还没有使用任何 Service Fabric 特定的功能,所以它成功了。

我唯一需要修改的代码是 class 程序:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.ServiceFabric.Services.Runtime;
using System;
using System.Diagnostics;
using System.Threading;

namespace MyNameSpace
{
    internal static class Program
    {
        private static void Main()
        {
            if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("Fabric_ApplicationName")))
            {
                StartInIISExpress();
            }
            else
            {
                StartInServiceFabric();
            }
        }

        private static void StartInIISExpress()
        {
            WebHost.CreateDefaultBuilder()
                    .UseStartup<Startup>()
                    .Build().Run();
        }

        private static void StartInServiceFabric()
        {
            < original code of the method Main >                   
        }
    }
}