【问题标题】:Error when calling Web API with self-hosted Azure Service Fabric使用自托管 Azure Service Fabric 调用 Web API 时出错
【发布时间】:2024-10-02 05:20:01
【问题描述】:

我正在使用一个新的 Azure Service Fabric 项目。该项目包含 2 个有状态的服务,如下图所示:

当我运行应用程序时,资源管理器成功显示如下:

但是当我尝试访问我的 API http://localhost:19080/api/values 时,我收到以下 Invalid argument 错误。它没有击中控制器:

请告知如何解决此错误。

【问题讨论】:

    标签: azure asp.net-web-api azure-service-fabric service-fabric-stateful


    【解决方案1】:

    正如 LoekD 所述,Service Fabric 资源管理器使用端口 19080。您可以在 API 项目的 ServiceManifest.xml 中找到应用程序的实际端口。应该有带有端点的资源部分,例如:

    <Resources>
     <Endpoints>
       <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8315" />
     </Endpoints>
    </Resources>
    

    【讨论】:

    • 使用 Kestrel 不需要端点配置。它使用文章中提到的动态端口。要对 Kestrel 使用动态端口分配,只需完全省略 ServiceManifest.xml 中的 Endpoint 配置,并且不要将端点名称传递给 KestrelCommunicationListener。那么我怎样才能得到这个动态端口号
    • @HanyHabib 为什么需要动态端口?
    • 我不需要我只是在这个框架中挖掘并查看它的功能。正如你所说,我使用了静态端口,除非我将 ServiceFabricIntegrationOptions.UseUniqueServiceUrl 更改为 ServiceFabricIntegrationOptions.None ,否则它不起作用。
    • 现在我明白你的意思了。要获取动态端口,您需要使用命名服务来解析服务的端点。虽然我怀疑它是否可以从 SF 外部访问。它应该看起来像 var resolver = ServicePartitionResolver.GetDefault(); var cancellationToken = new System.Threading.CancellationToken(); var endpoint = await resolver.ResolveAsync(new Uri(“&lt;name of your service&gt;”), new ServicePartitionKey(), cancellationToken);
    【解决方案2】:

    Service Fabric explorer 使用端口 19080。 你应该选择一个different port 来运行你自己的服务。

    【讨论】:

    • 使用 Kestrel 不需要端点配置。它使用文章中提到的动态端口。要在 Kestrel 中使用动态端口分配,只需完全省略 ServiceManifest.xml 中的 Endpoint 配置,并且不要将端点名称传递给 KestrelCommunicationListener。
    • 那么我怎样才能得到这个动态端口号?
    最近更新 更多