예제 #1
0
        private static SyncMemoryProvider AddNewProviderToCache(HttpContext context, CoreProvider provider, Action <SyncConfiguration> conf, Action <SyncOptions> options, string sessionId)
        {
            SyncMemoryProvider syncMemoryProvider;
            var cache = context.RequestServices.GetService <IMemoryCache>();

            if (cache == null)
            {
                throw new SyncException("Cache is not configured! Please add memory cache, distributed or not (see https://docs.microsoft.com/en-us/aspnet/core/performance/caching/response?view=aspnetcore-2.2)");
            }

            syncMemoryProvider = new SyncMemoryProvider(provider);

            syncMemoryProvider.SetConfiguration(conf);
            syncMemoryProvider.SetOptions(options);

            cache.Set(sessionId, syncMemoryProvider, TimeSpan.FromHours(1));
            return(syncMemoryProvider);
        }
예제 #2
0
        /// <summary>
        /// Call this method to handle requests on the server, sent by the client
        /// </summary>
        public async Task HandleRequestAsync(HttpContext context, Action <SyncMemoryProvider> action, CancellationToken cancellationToken)
        {
            var httpRequest  = context.Request;
            var httpResponse = context.Response;
            var streamArray  = GetBody(httpRequest);

            var serializationFormat = SerializationFormat.Json;

            // Get the serialization format
            if (TryGetHeaderValue(context.Request.Headers, "dotmim-sync-serialization-format", out var vs))
            {
                serializationFormat = vs.ToLowerInvariant() == "json" ? SerializationFormat.Json : SerializationFormat.Binary;
            }

            if (!TryGetHeaderValue(context.Request.Headers, "dotmim-sync-session-id", out var sessionId))
            {
                throw new SyncException($"Can't find any session id in the header");
            }

            SyncMemoryProvider syncMemoryProvider = null;
            var         syncSessionId             = "";
            HttpMessage httpMessage = null;

            try
            {
                var serializer = BaseConverter <HttpMessage> .GetConverter(serializationFormat);

                httpMessage   = serializer.Deserialize(streamArray);
                syncSessionId = httpMessage.SyncContext.SessionId.ToString();

                if (!httpMessage.SyncContext.SessionId.Equals(Guid.Parse(sessionId)))
                {
                    throw new SyncException($"Session id is not matching correctly between header and message");
                }

                // get cached provider instance if not defined byt web proxy server provider
                if (syncMemoryProvider == null)
                {
                    syncMemoryProvider = GetCachedProviderInstance(context, syncSessionId);
                }

                if (syncMemoryProvider == null)
                {
                    syncMemoryProvider = AddNewProviderToCacheFromDI(context, syncSessionId);
                }

                // action from user if available
                action?.Invoke(syncMemoryProvider);

                // get cache manager
                // since we are using memorycache, it's not necessary to handle it here
                //syncMemoryProvider.LocalProvider.CacheManager = GetCacheManagerInstance(context, syncSessionId);

                var httpMessageResponse =
                    await syncMemoryProvider.GetResponseMessageAsync(httpMessage, cancellationToken);

                var binaryData = serializer.Serialize(httpMessageResponse);
                await GetBody(httpResponse).WriteAsync(binaryData, 0, binaryData.Length);
            }
            catch (Exception ex)
            {
                await WriteExceptionAsync(httpResponse, ex, syncMemoryProvider?.LocalProvider?.ProviderTypeName ?? "ServerLocalProvider");
            }
            finally
            {
                if (httpMessage != null && httpMessage.Step == HttpStep.EndSession)
                {
                    Cleanup(context.RequestServices.GetService(typeof(IMemoryCache)), syncSessionId);
                }
            }
        }