internal bool TryWriteback()

in src/WebJobs.Extensions.DurableTask/ContextImplementations/DurableEntityContext.cs [339:400]


        internal bool TryWriteback(out ResponseMessage serializationErrorMessage, out Exception exception, string operationName = null, string operationId = null)
        {
            serializationErrorMessage = null;
            exception = null;

            if (this.CurrentStateAccess == StateAccess.Deleted)
            {
                this.Config.TraceHelper.EntityStateDeleted(
                    this.Config.Options.HubName,
                    this.Name,
                    this.InstanceId,
                    operationName ?? "",
                    operationId ?? "",
                    isReplay: false);

                this.State.EntityState = null;
                this.State.EntityExists = false;
                this.CurrentStateAccess = StateAccess.NotAccessed;
            }
            else if (this.CurrentStateAccess == StateAccess.Accessed)
            {
                try
                {
                    this.State.EntityState = this.messageDataConverter.Serialize(this.CurrentState);

                    if (!this.State.EntityExists)
                    {
                        this.Config.TraceHelper.EntityStateCreated(
                            this.Config.Options.HubName,
                            this.Name,
                            this.InstanceId,
                            operationName ?? "",
                            operationId ?? "",
                            isReplay: false);
                    }

                    this.State.EntityExists = true;
                    this.CurrentStateAccess = StateAccess.Clean;
                }
                catch (Exception e)
                {
                    // we cannot serialize the entity state - this is an application error.
                    var serializationException = new EntitySchedulerException(
                        $"Failed to serialize state of '{this.FunctionName}' entity: {e.Message}", e);

                    this.CaptureApplicationError(serializationException);

                    serializationErrorMessage = new ResponseMessage();
                    serializationErrorMessage.SetExceptionResult(serializationException, operationName, this.errorDataConverter);
                    exception = serializationException;

                    this.CurrentStateAccess = StateAccess.NotAccessed;
                    this.CurrentState = null;
                }
            }
            else
            {
                // the state was not accessed, or is clean, so we don't need to write anything back
            }

            return serializationErrorMessage == null;
        }