示例#1
0
        public async Task <TResponse> Deserialize <TResponse>(CancellationToken ct, HttpContent content)
        {
            if (content is null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            using (var stream = await content.ReadAsStreamAsync())
            {
                return((TResponse)_objectSerializer.FromStream(stream, typeof(TResponse)));
            }
        }
        public async Task <T> DeserializeAsync <T>(HttpContent content)
        {
            if (content is null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            using (var stream = await content.ReadAsStreamAsync())
            {
                return((T)_serializer.FromStream(stream, typeof(T)));
            }
        }
        /// <summary>
        /// Constructor using an <see cref="IObjectSerializer"/> for persistence.
        /// </summary>
        public LockedFileDataPersister(
            string fullFilename,
            IObjectSerializer serializer,
            int numberOfRetries = 3,
            int retryDelay      = 100)
        {
            _committedFile   = new FileInfo(fullFilename /*.Validation().NotNull(nameof(fullFilename))*/).FullName;
            _read            = async(ct, stream) => (T)serializer.FromStream(stream, typeof(T));
            _write           = async(ct, entity, stream) => serializer.WriteToStream(entity, typeof(T), stream, canDisposeStream: true);
            _numberOfRetries = numberOfRetries;
            _retryDelay      = retryDelay;

            // Create other working file names
            _oldFile  = _committedFile + ".old";
            _newFile  = _committedFile + ".new";
            _lockFile = _committedFile + ".lck";
        }