Are objects shared by different worker roles in Az

2019-08-23 04:22发布

问题:

I am working on Asp.Net core with Azure service fabric. We have stateless service too. On the Azure portal we have 5 nodes and for each node we have 2 instances. I have implemented logging mechanism. I am using Dependency injection too.

Whenever worker role pick up the record from database,there are few values till the time that record is processed because I need to log those values in my logging framework. I am using this value to track telemetry across all systems.

Currently I have created an object for such values and assigning this object to logging class whenever I picked up the document.

It's working fine when I run in local environment because I have one node and one instance. Once I moved to Azure, it starts overwriting values.

How to avoid it? How can I make sure that value should not change till it gets processed?

For the Asp.Net we have session, do we have anything such like for worker role?

Thanks for your inputs and guidance.

回答1:

I think your problem is related to the Service Fabric hosting model

Service fabric has two models, Shared Process and Exclusive process.

The Shared Model is the default when you don't specify a hosting model when creating the service, this model will use a single process to host multiple replicas from same CodePackage(executable). That means, when you start multiple replicas on same node, it will create one process and initialise any new replica in this process. This is a big problem when you use static and singleton objects, because all replicas will see the same object, but on you code logic it supposed to be different for each instance of your service.

The Exclusive Process will create a new process every time a new replica is placed on same node. So if you are using static or singleton objects, this should be the model to use.