【问题标题】:Host WepAPI on Service Fabric在 Service Fabric 上托管 Web API
【发布时间】:2017-03-26 16:30:01
【问题描述】:

我们刚刚建立了一个本地 MS Service Fabric 集群。我有一些我想在其中托管的 WebAPI。我正在寻找有关如何采用我们的标准 4.5 WebAPI 并将它们托管在 Service Fabric 中的资源,而无需创建 Service Fabric 项目并进行迁移;这似乎太复杂了。

我查看了一些 Service Fabric 示例项目,似乎所有项目都与 Service Fabric 紧密耦合。我的目标是让这些应用不知道 Service Fabric。

非常感谢任何信息链接,谢谢!

【问题讨论】:

    标签: c# asp.net-web-api microservices azure-service-fabric devops


    【解决方案1】:

    我们是这样做的:

    • 在与我们的 WebAPI 相同的解决方案中创建了服务结构项目。
    • 在 ServiceFabricHost 项目中,我们创建了 HttpListenerService 以使用服务结构公开端口,您可以对 SelfHosted WebApi 执行相同操作。
    • 配置 ServiceFabricHost 以打开所需的端点。
    • 添加对 WebApi 项目的引用
    • 使用 WebApi Startup 发送 IAppBuilder 并使用我们的外部 (SF) 配置(如端口和 URL)配置 api。

    秘诀是:当您创建一个自托管 Web api 时,您通常会创建一个控制台应用程序,例如 asp.net 文档。使用服务结构,您可以将控制台替换为 ServiceFabricService,它类似于 ConsoleApplication,但在这种情况下将是无状态服务。

    在这种情况下,我们为 HttpListenerService 使用了无状态服务,如果您需要 StateFull 服务,则需要重构您的 Api 以使用可靠的集合。

    Vaclac 的同事为此创建了一个很好的教程: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-communication-webapi/

    【讨论】:

      【解决方案2】:

      如果它是具有自托管 Web 服务器(例如 Katana,而不是 IIS)的独立 Web 应用程序,那么您可以简单地将其作为 Guest Executable 运行。

      如果它不是自托管的并且需要单独的 Web 服务器来运行,例如 IIS,那么您可以考虑在 Container 中运行它。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,并通过在 Startup() 类中的 HttpConfiguration 对象上使用 DependencyResolver.GetService() 方法解决了它。创建一个新的 Service Fabric Stateless/Statefull WebAPI 项目。在 Startup() 类中,添加如下代码:

         public static class Startup
        {
            // This code configures Web API. The Startup class is specified as a type
            // parameter in the WebApp.Start method.
            public static void ConfigureApp(IAppBuilder appBuilder)
            {
                // Configure Web API for self-host.
                HttpConfiguration config = new HttpConfiguration();
                // Allow custom routes in controller attributes.
                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute(
                    name: "Default",
                    routeTemplate: "{controller}/{action}/{id}",
                    defaults: new { controller = "API", action = "HealthCheck", id = RouteParameter.Optional }
                );
                //inject controllers here
                config.DependencyResolver.GetService(typeof({{YourWebAPIRootNamespace}}.Controllers.APIController));
        
                appBuilder.UseWebApi(config);
            }
        }
        

        这允许您将现有 API 部署到 Service Fabric,而无需将整个代码库迁移到新项目。不要忘记使用 web.config 中的所有适用设置更新新项目中的 app.config。

        完整的博文在这里http://thenameisirrelevant.com/hosting-an-existing-webapi-in-service-fabric

        【讨论】:

          猜你喜欢
          • 2016-05-29
          • 2016-05-20
          • 2017-04-29
          • 1970-01-01
          • 2018-11-13
          • 1970-01-01
          • 2016-09-27
          • 2020-01-05
          • 2018-01-31
          相关资源
          最近更新 更多