【问题标题】: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
【解决方案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