【问题标题】:Is rapid development possible - (portal) Webapplication inside Service Fabric project是否可以进行快速开发 - Service Fabric 项目中的(门户)Web 应用程序
【发布时间】:2018-04-04 16:50:28
【问题描述】:

我添加了一个 WebApplication 来将我们的 Web 门户构建到现有的 ServiceFabric 集群 VS sln 设置中。但是,我很快意识到这不适用于快速开发/调试周期。为了对 CSHTML 文件进行更改,然后在浏览器中快速试用它们,我需要能够在 VSTS 中按 F5 并快速运行 portal.exe 并提供页面;所以我切换到“门户”项目作为启动项目(它是粗体)并使用 IISExpress 或门户配置文件(来自launchSettings.json)。

但是,失败并出现异常:

System.Fabric.FabricException: 'An error occurred during this operation.  Please check the trace logs for more details.'

从这一行开始:

ServiceRuntime.RegisterServiceAsync("PortalApp",
     context => new APIGateway(context)).GetAwaiter().GetResult();

这是因为我没有在 SF 集群内运行。这是有道理的。

那么如何让这个场景发挥作用?是否有办法进行我的开发而不必每次都将解决方案部署到集群?即使在单节点本地集群中,这也非常慢(最多 30-45 秒)并且严重影响生产力。

我是否可以添加一个“RunLocal”代码路径来规避和创建一个不依赖于 SF 运行时但仍使用我的项目的其余部分并在集群外运行的 WebHost?

【问题讨论】:

    标签: asp.net-mvc azure-service-fabric


    【解决方案1】:

    好的,我(大部分)为我的场景解决了这个问题。

    这就是我所做的。基本上,我不是使用 Service FabricRuntime 在 program.cs 中创建 WebHost(基本上只在 SF 集群内工作),而是检查我们是否在集群内运行(从这里借用),然后实例化我自己的 WebHost 实例使用相同的 Startup 类,因此 webHost(在我的例子中是 Kestrel)配置了相同的设置。从那时起,全部由原始代码接管。

    这适用于托管 WebHost(http 侦听器)的 ServiceFabric 项目。但是,我还有另一个 SF 项目没有,并且由其他人通过 Service Remoting 调用。不幸的是,我怀疑可以像上面那样轻松地工作。

    进行这些更改后,您可以更改解决方案的启动项目以包含 1 个或所有网络主机 EXE。然后按 F5,它将在集群外部本地启动 (1..N) 个 EXE,您可以在那里调试它们。这样,我的工作流程是让 webhost EXE 运行,更改我的 CSHTML 文件,保存,然后仅刷新网页。可以立即看到和测试更改!对其他编译代码的更改也可以非常快速地进行测试。

    (从这个帖子中得到了一些上述想法:How to see if running under service fabric)

    代码如下:

    // program.cs
    
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.ServiceFabric.Services.Runtime;
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Web.Portal
    {
        internal static class Program
        {
            /// <summary>
            /// This is the entry point of the service host process.
            /// </summary>
            private static void Main(string[] args)
            {
                if (Environment.GetEnvironmentVariable("Fabric_ApplicationName") == null)
                {
                    ServiceEventSource.Current.Message("Detected as running locally OUTSIDE the Fabric Cluster");
                    BuildWebHost(args).Run();
                }
                else
                {
                    ServiceEventSource.Current.Message("Detected as running INSIDE the Fabric Cluster");
                    try
                    {
                        ServiceRuntime.RegisterServiceAsync("Web.PortalType",
                            context => new Portal(context)).GetAwaiter().GetResult();
    
                        // The ServiceManifest.XML file defines one or more service type names.
                        // Registering a service maps a service type name to a .NET type.
                        // When Service Fabric creates an instance of this service type,
                        // an instance of the class is created in this host process.
    
                        ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Portal).Name);
    
                        // Prevents this host process from terminating so services keeps running. 
                        Thread.Sleep(Timeout.Infinite);
                    }
                    catch (Exception e)
                    {
                        ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                        throw;
                    }
                }
            }
    
            public static IWebHost BuildWebHost(string[] args)
            {
                // for some reason, the appSetting.json values arent getting loaded by default config builder
                // even though, per MSDN, they should be in ASPNET Core 2.0. I am adding them in manually for now.
                IConfiguration config = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
                    .AddJsonFile($"appSettings.Development.json", optional: true, reloadOnChange: true)
                    .AddEnvironmentVariables()
                    .Build();
    
                return new WebHostBuilder()
                        .UseConfiguration(config)
                        .UseKestrel()
                        .UseContentRoot(Directory.GetCurrentDirectory())
                        .UseStartup<Startup>()
                        .Build();
            }
        }
    }

    【讨论】:

      【解决方案2】:

      在 Service Fabric SDK 上,您拥有名为“Refresh Application”的应用程序调试模式,部署使用此功能将创建一个从 SF 部署到您的开发路径的符号链接,您对文件所做的每一次更改都会反映所做的更改关于 SF 运行应用程序

      要使用此部署模式,您可以通过以下方式更改此设置:右键单击您的 SF 项目 > 属性 > 属性窗口将显示设置(如下) > 从“删除应用程序”切换到“刷新应用程序”

      此模式有一些限制:

      • 它仅适用于单节点开发集群。
      • 在 ASP.NET 的情况下只支持更改代码、配置或数据包的内容和项目目录内容 核心项目。应用程序结构更改 - 例如更改 ServiceManifest.xml 或 ApplicationManifest.xml - 不支持 因为部署的应用程序的结构可能会随着 不需要的结果。您仍然可以在 刷新应用程序调试模式,但如果这样做,应用程序将 重新部署。
      • 如果您重新编译,有状态服务不会保持其状态。为了让您的新二进制文件运行,必须删除该服务 停止该进程,以便可以将新的二进制文件写入磁盘,其中 当然意味着你所有的状态都被消灭了。这通常适用于 快速开发,但要真正测试您的有状态服务,您将 仍然需要在 5 节点集群中运行它们。

      【讨论】:

      • 谢谢,就我而言,它已经像这样设置了。但是,正如我在最初的问题中提到的那样,我的目标是完全避免使用 Azure Service Fabric 本地集群,因为对于这个特定的用例,它仍然是我可以做的很多开销。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多