Distributed .NET with Microsoft Orleans by Bhupesh Guptha Muthiyalu Suneel Kumar Kunani

Distributed .NET with Microsoft Orleans by Bhupesh Guptha Muthiyalu Suneel Kumar Kunani

Author:Bhupesh Guptha Muthiyalu, Suneel Kumar Kunani
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2022-04-29T00:00:00+00:00


In the preceding code snippet, we are checking for the existence of the directory with the name of the grain type and then checking for the existence of the file with the grain ID as the name. If the file is present, then we compare the ETag of the incoming state with the storage provider ETag. If the ETag matches, we will overwrite the state on the provider with the incoming state by calling WriteToFileAsync. WriteToFileAsync writes the grain state to Azure Files and returns the updated ETag. For the implementation of WriteToFileAsync, please refer to the sample code of this book on GitHub. If the ETag does not match, that means the state is not consistent. We will throw the InconsistentStateException exception to the caller so that the caller can refresh the state and retry the write operation.

Let's now implement the ReadStateAsync method. ReadStateAsync will be called at the time of grain activation to load the state or when explicitly called, ReadStateAsync will read the state. Add the ReadStateAsync method to the AzureFileGrainStorage class as shown in the following code snippet:public async Task ReadStateAsync(string grainType,

GrainReference grainReference, IgrainState

grainState)

{

string id = this.GetFileName(grainReference);

try

{

var directory =

this._shareClient.GetDirectoryClient(grainType);

if (await directory.ExistsAsync())

{

var file = directory.GetFileClient(id);

if (await file.ExistsAsync())

{

ShareFileDownloadInfo download = await

file.DownloadAsync();

using (StreamReader reader = new

StreamReader(download.Content))

using (JsonTextReader jsonReader = new

JsonTextReader(reader))

{

JsonSerializer ser = new

JsonSerializer();

grainState.State =

ser.Deserialize(jsonReader,

grainState.State.GetType());

}

grainState.RecordExists = true;

grainState.ETag =

download.Details.ETag.ToString();

}

}

if (grainState.State == null)

{

grainState.State =

Activator.CreateInstance

(grainState.State.GetType());

grainState.RecordExists = true;

}

}

catch (Exception exc)

{

throw;

}

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.