我是 Service Fabric 的新手,目前我正在努力找出如何从 WEB API(即也位于服务中)访问可靠集合(在有状态服务上下文中定义和初始化)中的数据结构集群,作为单独的应用程序)。这个问题非常基本,我确信我遗漏了一些非常明显的东西。如果这听起来很蹩脚,请向社区道歉。
我有一个大型 XML,我想通过 WEB API 端点公开其中的一部分作为各种查询的结果。搜索了类似的问题,但没有找到合适的答案。
很高兴看到经验丰富的 SF 开发人员如何完成此类任务。
编辑我发布了我想出的解决方案
最佳答案
在阅读并观察其他问题和 Azure 示例后,我实现了一个解决方案。在这里发布我遇到的问题,希望能够帮助其他刚接触 Azure Service Fabric 的开发人员(免责声明:我仍然是 Service Fabric 的新手,因此非常感谢评论和建议):
首先,非常简单 - 我最终在 Azure Service Fabric 应用程序中获得了有状态服务和 WEB Api 无状态服务:
DataStoreService
- 读取大型 XML 并将其存储到 Reliable 字典中的有状态服务(发生在 RunAsync 方法中)。- Web Api 提供了一个
/api/query
端点,用于过滤出存储在 rteliable 字典中的 XElement 集合,并将其序列化回请求者
3 个陷阱
1)如何从无状态服务中获取可靠的字典数据,即如何从无状态服务中获取有状态服务的实例:
ServiceUriBuilder builder = new ServiceUriBuilder("DataStoreService");
IDataStoreService DataStoreServiceClient = ServiceProxy.Create<IDataStoreService>(builder.ToUri(), new ServicePartitionKey("Your.Partition.Name"));
上面的代码已经为您提供了实例。即 - 您需要使用服务代理。为此,您需要:
定义有状态服务将实现的接口(interface),并在调用 ServiceProxy (
IDataStoreService
) 的 Create 方法时使用它将正确的分区键传递给 Create 方法。 This article对 Azure 服务总线分区进行了很好的介绍
2)注册副本监听器 - 以避免出现错误
The primary or stateless instance for the partition 'a67f7afa-3370-4e6f-ae7c-15188004bfa1' has invalid address, this means that right address from the replica/instance is not registered in the system
,您需要按照此 post 中所述注册副本监听器:
public DataStoreService(StatefulServiceContext context)
: base(context)
{
configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
}
3) 服务结构名称间距和引用服务 - 我从 service-fabric-dotnet-web-reference-app 获取的 ServiceUriBuilder
类。基本上你需要一些东西来生成以下形式的 Uri:
new Uri("fabric:/"+ this.ApplicationInstance + "/"+ this.ServiceInstance);
,
其中 ServiceInstance
是您想要获取实例的服务的名称(在本例中为 DataStoreService
)
关于azure - Service Fabric - 集群中的 Web api,唯一的工作是提供来自可靠集合的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628900/