本文整理汇总了C#中IGrainState类的典型用法代码示例。如果您正苦于以下问题:C# IGrainState类的具体用法?C# IGrainState怎么用?C# IGrainState使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IGrainState类属于命名空间,在下文中一共展示了IGrainState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadStateAsync
public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
var tableResult = await _table.ExecuteAsync(TableOperation.Retrieve<DynamicTableEntity>(grainReference.ToKeyString(), grainType));
if (tableResult.Result == null)
{
return;
}
var entity = tableResult.Result as DynamicTableEntity;
var serializer = new JsonSerializer();
using (var memoryStream = new MemoryStream())
{
foreach (var propertyName in entity.Properties.Keys.Where(p => p.StartsWith("d")).OrderBy(p => p))
{
var dataPart = entity.Properties[propertyName];
await memoryStream.WriteAsync(dataPart.BinaryValue, 0, dataPart.BinaryValue.Length);
}
memoryStream.Position = 0;
using (var bsonReader = new BsonReader(memoryStream))
{
var data = serializer.Deserialize<Dictionary<string, object>>(bsonReader);
grainState.SetAll(data);
}
}
}
示例2: ReadStateAsync
public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
if (!(grainState is IAggregateState))
throw new NotAggregateStateException(grainState.GetType());
var stream = this.GetStreamName(grainType, grainReference);
var sliceStart = 0;
StreamEventsSlice currentSlice;
do
{
var sliceCount = sliceStart + ReadPageSize;
currentSlice = await this.Connection.ReadStreamEventsForwardAsync(stream, sliceStart, sliceCount, true);
if (currentSlice.Status == SliceReadStatus.StreamNotFound)
return;
if (currentSlice.Status == SliceReadStatus.StreamDeleted)
throw new StreamDeletedException();
sliceStart = currentSlice.NextEventNumber;
foreach (var @event in currentSlice.Events)
{
dynamic deserialisedEvent = DeserializeEvent(@event.Event);
StateTransformer.ApplyEvent(deserialisedEvent, grainState as IAggregateState);
}
} while (!currentSlice.IsEndOfStream);
}
示例3: ReadStateAsync
public async Task ReadStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
{
try
{
var blobName = BlobStorageProvider.GetBlobName(grainType, grainId);
var blob = container.GetBlockBlobReference(blobName);
var text = await blob.DownloadTextAsync();
if (string.IsNullOrWhiteSpace(text))
{
return;
}
var data = JsonConvert.DeserializeObject(text, grainState.GetType());
var dict = ((IGrainState)data).AsDictionary();
grainState.SetAll(dict);
}
catch (StorageException ex)
{
;
}
catch (Exception ex)
{
Log.Error(0, ex.ToString());
}
}
示例4: WriteStateAsync
public async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
try
{
var collection = await this.EnsureCollection(grainType);
var documents = await this.Client.ReadDocumentFeedAsync(collection.DocumentsLink);
var documentId = grainReference.ToKeyString();
var document = documents.Where(d => d.Id == documentId).FirstOrDefault();
if(document != null)
{
document.State = grainState.AsDictionary();
await this.Client.ReplaceDocumentAsync(document);
}
else
{
await this.Client.CreateDocumentAsync(collection.DocumentsLink,
new GrainStateDocument { Id = documentId, State = grainState.AsDictionary() });
}
}
catch (Exception ex)
{
Log.Error(0, "Error in WriteStateAsync", ex);
}
}
示例5: WriteStateAsync
public Task<string> WriteStateAsync(string stateStore, string grainStoreKey, IGrainState grainState)
{
if (logger.IsVerbose) logger.Verbose("WriteStateAsync for {0} grain: {1} eTag: {2}", stateStore, grainStoreKey, grainState.ETag);
GrainStateStore storage = GetStoreForGrain(stateStore);
storage.UpdateGrainState(grainStoreKey, grainState);
if (logger.IsVerbose) logger.Verbose("Done WriteStateAsync for {0} grain: {1} eTag: {2}", stateStore, grainStoreKey, grainState.ETag);
return Task.FromResult(grainState.ETag);
}
示例6: ReadStateAsync
/// <summary>
/// Reads persisted state from the backing store and deserializes it into the the target
/// grain state object.
/// </summary>
/// <param name="grainType">A string holding the name of the grain class.</param>
/// <param name="grainReference">Represents the long-lived identity of the grain.</param>
/// <param name="grainState">A reference to an object to hold the persisted state of the grain.</param>
/// <returns>Completion promise for this operation.</returns>
public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
if (DataManager == null) throw new ArgumentException("DataManager property not initialized");
var entityData = await DataManager.Read(grainState.GetType().Name, grainReference.ToKeyString());
if (entityData != null)
{
ConvertFromStorageFormat(grainState, entityData);
}
}
示例7: ClearStateAsync
public Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
if (!(grainState is IAggregateState))
throw new NotAggregateStateException(grainState.GetType());
var state = grainState as IAggregateState;
var stream = this.GetStreamName(grainType, grainReference);
return this.Connection.DeleteStreamAsync(stream, state.Version);
}
示例8: WriteStateAsync
public async Task WriteStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
{
try
{
var blobName = BlobStorageProvider.GetBlobName(grainType, grainId);
var storedData = JsonConvert.SerializeObject(grainState.AsDictionary());
var blob = container.GetBlockBlobReference(blobName);
await blob.UploadTextAsync(storedData);
}
catch (Exception ex)
{
Log.Error(0, ex.ToString());
}
}
示例9: ReadStateAsync
public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
var stateName = grainState.GetType().Name;
var key = grainReference.ToKeyString();
var id = string.Format(CultureInfo.InvariantCulture, "{0}/{1}", stateName, key);
using (IAsyncDocumentSession session = this.documentStore.OpenAsyncSession())
{
var state = await session.LoadAsync<RavenJObject>(id);
if (state != null)
{
grainState.SetAll(state.ToDictionary(x => x.Key, x => x.Value.Value<object>()));
}
}
}
示例10: WriteStateAsync
public Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
var entity = new DynamicTableEntity(grainReference.ToKeyString(), grainType) { ETag = "*" };
var serializer = new JsonSerializer();
using (var memoryStream = new MemoryStream())
{
using (var bsonWriter = new BsonWriter(memoryStream))
{
serializer.Serialize(bsonWriter, grainState.AsDictionary());
SplitBinaryData(entity, memoryStream.ToArray());
}
}
return _table.ExecuteAsync(TableOperation.InsertOrReplace(entity));
}
示例11: ReadStateAsync
public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
try
{
var collection = await this.EnsureCollection(grainType);
var documents = await this.Client.ReadDocumentFeedAsync(collection.DocumentsLink);
var documentId = grainReference.ToKeyString();
GrainStateDocument document = documents.Where(d => d.Id == documentId).FirstOrDefault();
if(document != null)
grainState.SetAll(document.State);
}
catch (Exception ex)
{
Log.Error(0, "Error in ReadStateAsync", ex);
}
}
示例12: WriteStateAsync
/// <summary> Write state data function for this storage provider. </summary>
/// <see cref="IStorageProvider.WriteStateAsync"/>
public async Task WriteStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
{
var blobName = GetBlobName(grainType, grainId);
try
{
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_Writing, "Writing: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
var json = JsonConvert.SerializeObject(grainState.State, settings);
var blob = container.GetBlockBlobReference(blobName);
blob.Properties.ContentType = "application/json";
var containerNotFound = false;
try
{
await blob.UploadTextAsync(
json,
Encoding.UTF8,
AccessCondition.GenerateIfMatchCondition(grainState.ETag),
null,
null).ConfigureAwait(false);
}
catch (StorageException exception)
{
var errorCode = exception.RequestInformation.ExtendedErrorInformation.ErrorCode;
containerNotFound = errorCode == BlobErrorCodeStrings.ContainerNotFound;
}
if (containerNotFound)
{
// if the container does not exist, create it, and make another attempt
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_ContainerNotFound, "Creating container: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
await container.CreateIfNotExistsAsync().ConfigureAwait(false);
await blob.UploadTextAsync(
json,
Encoding.UTF8,
AccessCondition.GenerateIfMatchCondition(grainState.ETag),
null,
null).ConfigureAwait(false);
}
grainState.ETag = blob.Properties.ETag;
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_DataRead, "Written: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
}
catch (Exception ex)
{
Log.Error((int)AzureProviderErrorCode.AzureBlobProvider_WriteError,
string.Format("Error writing: GrainType={0} Grainid={1} ETag={2} to BlobName={3} in Container={4} Exception={5}", grainType, grainId, grainState.ETag, blobName, container.Name, ex.Message),
ex);
}
}
示例13: ClearStateAsync
/// <summary> Clear / Delete state data function for this storage provider. </summary>
/// <see cref="IStorageProvider.ClearStateAsync"/>
public async Task ClearStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
{
var blobName = GetBlobName(grainType, grainId);
try
{
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_ClearingData, "Clearing: GrainType={0} Grainid={1} ETag={2} BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
var blob = container.GetBlockBlobReference(blobName);
await blob.DeleteIfExistsAsync(
DeleteSnapshotsOption.None,
AccessCondition.GenerateIfMatchCondition(grainState.ETag),
null,
null).ConfigureAwait(false);
grainState.ETag = blob.Properties.ETag;
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Cleared, "Cleared: GrainType={0} Grainid={1} ETag={2} BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
}
catch (Exception ex)
{
Log.Error((int)AzureProviderErrorCode.AzureBlobProvider_ClearError,
string.Format("Error clearing: GrainType={0} Grainid={1} ETag={2} BlobName={3} in Container={4} Exception={5}", grainType, grainId, grainState.ETag, blobName, container.Name, ex.Message),
ex);
}
}
示例14: ConvertFromStorageFormat
/// <summary>
/// Deserialize from Azure storage format
/// </summary>
/// <param name="grainState">The grain state data to be deserialized in to</param>
/// <param name="entity">The Azure table entity the stored data</param>
internal void ConvertFromStorageFormat(IGrainState grainState, GrainStateEntity entity)
{
Dictionary<string, object> dataValues = null;
try
{
if (entity.Data != null)
{
// Rehydrate
dataValues = SerializationManager.DeserializeFromByteArray<Dictionary<string, object>>(entity.Data);
}
else if (entity.StringData != null)
{
dataValues = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(entity.StringData, jsonSettings);
}
if (dataValues != null)
{
grainState.SetAll(dataValues);
}
// Else, no data found
}
catch (Exception exc)
{
var sb = new StringBuilder();
if (entity.Data != null)
{
sb.AppendFormat("Unable to convert from storage format GrainStateEntity.Data={0}", entity.Data);
}
else if (entity.StringData != null)
{
sb.AppendFormat("Unable to convert from storage format GrainStateEntity.StringData={0}", entity.StringData);
}
if (dataValues != null)
{
int i = 1;
foreach (var dvKey in dataValues.Keys)
{
object dvValue = dataValues[dvKey];
sb.AppendLine();
sb.AppendFormat("Data #{0} Key={1} Value={2} Type={3}", i, dvKey, dvValue, dvValue.GetType());
i++;
}
}
Log.Error(0, sb.ToString(), exc);
throw new AggregateException(sb.ToString(), exc);
}
}
示例15: ReadStateAsync
/// <summary> Read state data function for this storage provider. </summary>
/// <see cref="IStorageProvider.ReadStateAsync"/>
public async Task ReadStateAsync(string grainType, GrainReference grainId, IGrainState grainState)
{
var blobName = GetBlobName(grainType, grainId);
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_Reading, "Reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
try
{
var blob = container.GetBlockBlobReference(blobName);
string json;
try
{
json = await blob.DownloadTextAsync().ConfigureAwait(false);
}
catch (StorageException exception)
{
var errorCode = exception.RequestInformation.ExtendedErrorInformation.ErrorCode;
if (errorCode == BlobErrorCodeStrings.BlobNotFound)
{
if (this.Log.IsVerbose2) this.Log.Verbose2((int)AzureProviderErrorCode.AzureBlobProvider_BlobNotFound, "BlobNotFound reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
return;
}
if (errorCode == BlobErrorCodeStrings.ContainerNotFound)
{
if (this.Log.IsVerbose2) this.Log.Verbose2((int)AzureProviderErrorCode.AzureBlobProvider_ContainerNotFound, "ContainerNotFound reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
return;
}
throw;
}
if (string.IsNullOrWhiteSpace(json))
{
if (this.Log.IsVerbose2) this.Log.Verbose2((int)AzureProviderErrorCode.AzureBlobProvider_BlobEmpty, "BlobEmpty reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
return;
}
grainState.State = JsonConvert.DeserializeObject(json, grainState.State.GetType(), settings);
grainState.ETag = blob.Properties.ETag;
if (this.Log.IsVerbose3) this.Log.Verbose3((int)AzureProviderErrorCode.AzureBlobProvider_Storage_DataRead, "Read: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4}", grainType, grainId, grainState.ETag, blobName, container.Name);
}
catch (Exception ex)
{
Log.Error((int)AzureProviderErrorCode.AzureBlobProvider_ReadError,
string.Format("Error reading: GrainType={0} Grainid={1} ETag={2} from BlobName={3} in Container={4} Exception={5}", grainType, grainId, grainState.ETag, blobName, container.Name, ex.Message),
ex);
}
}