简体   繁体   English

Azure Service Fabric:使用无状态服务将数据存储到有状态服务(C#编程)

[英]Azure Service Fabric : Use stateless service to store data into stateful service (c# programming)

Objective: Want to create a product with some entities in a stateless service and the created product should get stored in a stateful service ( IReliableDictionary ) 目标:想要在stateless服务中创建带有某些实体的产品,并且所创建的产品应存储在有stateful服务中( IReliableDictionary

// Task done: I am working with Azure Service Fabric. Firstly, I created a 
// stateful service which does the job of AddProduct and GetAllProducts and the 
// data is stored in IReliableDictionary. For this I have declared, and passed 
// the object in a constructor.

private readonly IReliableStateManager _stateManager;
public ServiceFabricRepository(IReliableStateManager stateManager)
{
   _stateManager = stateManager;
}

If I am adding a product within a stateful service the product is being added into the database ( IReliableDictionary ). 如果我要在有stateful服务中添加产品,则该产品将被添加到数据库( IReliableDictionary )中。 This is tested and its working. 这已经过测试并且可以正常工作。

The next task, what I did is add a new service ( stateless ) in the solution. 我要做的下一个任务是在解决方案中添加新服务( stateless )。 The stateless should create a product with some entities ( Id , Name ). stateless应使用某些实体( IdName )创建产品。 The product created should be added in my database ( stateful service) 创建的产品应添加到我的数据库中( stateful服务)

Problem: I am able to create the product with some entities but the product is not being added in my database, because it is asking me to create an instance of IReliableStateManager in the stateless service and the stateManager is always null . 问题:我能够使用某些实体创建产品,但是没有将产品添加到我的数据库中,因为它要求我在stateless服务中创建IReliableStateManager的实例,并且stateManager始终为null

How to create an instance of IReliableStateManager in a stateless service since IReliableStateManager is provided by the stateful service inheritance hierarchy. 由于IReliableStateManagerstateful服务继承层次结构提供,因此如何在stateless服务中创建IReliableStateManager的实例。 I am creating an instance of my repository in a stateless service 我正在stateless服务中创建存储库的实例

//(stateManager is never assigned to and will always have its dafult value null) 
private static IReliableStateManager stateManager; 
private ServiceFabricRepository = new ServiceFabricRepository(stateManager)

I have done some search and found out that stateful service is used for data storage( Example interfaces: AddProduct or GetProduct ) and stateless Web Api is used to expose the interfaces of stateful service using service remoting or Http for communication between the services. 我进行了一些搜索,发现stateful服务用于数据存储(示例接口: AddProductGetProduct ), stateless Web Api用于通过服务远程处理或Http公开stateful服务的接口,以在服务之间进行通信。 But was unable to find any example for my scenario. 但是找不到适合我的方案的任何示例。

Will be very helpful for any help or suggestions 对于任何帮助或建议都会非常有帮助

Thanks & Regards 感谢和问候

You do not use IReliableStateManager directly in Stateless service, rather call Stateful service from Stateless and pass object needs to be saved. 您不直接在无状态服务中使用IReliableStateManager,而是从无状态调用有状态服务,并且需要保存传递​​对象。

For example: Create service proxy of stateful service in stateless service and call its method: 例如:在无状态服务中创建有状态服务的服务代理,并调用其方法:

IProductService = ServiceProxy.Create<IProductService>(new Uri("fabric:/SfSample/ProductService"), new ServicePartitionKey(0));
var newProduct = new Product()
{
    Name = product.Name,
    Id = product.Id
};
await _productService.AddProduct(newProduct);

And Stateful service: 和状态服务:

public class ProductService : StatefulService, IProductService
    {

        public async Task AddProduct(Product product)
        {
            var products = await StateManager.GetOrAddAsync<IReliableDictionary<Guid, Product>>("products");

            using (var tx = StateManager.CreateTransaction())
            {
                await products.AddOrUpdateAsync(tx, product.Id, product, (id, value) => product);

                await tx.CommitAsync();
            }
        }
...........
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM