/// <summary> /// Append the event to the end of the event stream /// </summary> /// <param name="eventInstance"> /// The event to append to the end of the event stream /// </param> /// <param name="expectedTopSequenceNumber"> /// if this is set to > 0 and the event stream is further on then a consistency issue has arisen and the /// event should not be written but rather throw an error /// </param> /// <param name="eventVersionNumber"> /// The version number to add to the event wrapper /// </param> /// <param name="streamConstraint"> /// An additional constrain that must be satisfied by the event stream in order to persist the event /// </param> /// <returns></returns> public async Task <IAppendResult> AppendEvent(IEvent eventInstance, int expectedTopSequenceNumber = 0, int eventVersionNumber = 1, EventStreamExistenceConstraint streamConstraint = EventStreamExistenceConstraint.Loose) { if (base.EventStreamBlob != null) { // acquire a lease for the blob.. string writeStreamLeaseId = null; if (await Exists()) { writeStreamLeaseId = await base.EventStreamBlob.AcquireLeaseAsync(TimeSpan.FromSeconds(15)); } int nextSequence = await base.GetSequenceNumber() + 1; if (expectedTopSequenceNumber > 0) { // check against actual top sequence number if ((expectedTopSequenceNumber + 1) < nextSequence) { throw new EventStreamWriteException(this, (nextSequence - 1), message: $"Out of sequence write - expected seqeunce number {expectedTopSequenceNumber }", source: "Blob Event Stream Writer"); } } string eventName = ""; if (null != eventInstance) { eventName = EventNameAttribute.GetEventName(eventInstance.GetType()); } // create an access condition AccessCondition condition = AccessCondition.GenerateEmptyCondition(); if (streamConstraint == EventStreamExistenceConstraint.MustBeNew) { condition = AccessCondition.GenerateIfNotExistsCondition(); } if (streamConstraint == EventStreamExistenceConstraint.MustExist) { condition = AccessCondition.GenerateIfExistsCondition(); } if (!string.IsNullOrWhiteSpace(writeStreamLeaseId)) { condition.LeaseId = writeStreamLeaseId; } // default the writer context if it is not already set if (null == _writerContext) { _writerContext = WriteContext.DefaultWriterContext(); } BlobBlockJsonWrappedEvent evtToWrite = BlobBlockJsonWrappedEvent.Create(eventName, nextSequence, eventVersionNumber, null, eventInstance, _writerContext); try { // Create it if it doesn't exist and initialsie the metadata await base.Refresh(); Microsoft.Azure.Storage.OperationContext context = new Microsoft.Azure.Storage.OperationContext() { }; await EventStreamBlob.AppendBlockAsync(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(evtToWrite.ToJSonText())), "", condition, null, // use the default blob request options context ); } catch (Microsoft.Azure.Storage.StorageException exBlob) { throw new EventStreamWriteException(this, (nextSequence - 1), message: "Failed to save an event to the event stream", source: "Blob Event Stream Writer", innerException: exBlob); } await IncrementSequence(writeStreamLeaseId); if (!string.IsNullOrWhiteSpace(writeStreamLeaseId)) { // and release the lease await base.EventStreamBlob.ReleaseLeaseAsync(condition); } int sequence = await base.GetSequenceNumber(); return(new AppendResult((sequence == 0), sequence)); } else { return(null); } }