示例#1
0
        /// <summary>
        /// Process an incoming request and return a WCF Message class instance.
        /// </summary>
        /// <param name="messageBody">Message body</param>
        /// <returns>Instance of the WCF Message class</returns>
        public Message ProcessRequestForMessage(Stream messageBody)
        {
            try
            {
                // Intialize the service host first, since most of the logic depends on it.
                _serviceHost = new HttpContextServiceHost(messageBody);
                _serviceHost.ValidateRequestHttpVerbAndSegments();

                // Create the configuration. The first call will initialize the configuration
                // and all other calls will be a no-op.
                CreateConfiguration();

                // Raise event for user code
                InvokeOnSyncRequestStart();

                _requestDescription = new RequestParser(_serviceHost, _syncConfiguration).ParseIncomingRequest();

                if (null == _requestDescription.SyncBlob || 0 == _requestDescription.SyncBlob.Length)
                {
                    InitializeNewClient();
                }

                _requestProcessor = RequestProcessorFactory.GetRequestProcessorInstance(_requestDescription.RequestCommand, _syncConfiguration, _serviceHost);

                _outgoingMessage = _requestProcessor.ProcessRequest(_requestDescription);

                // Add sync properties
                var responseProperties = _outgoingMessage.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                if (null != responseProperties)
                {
                    responseProperties.Headers[SyncServiceConstants.SYNC_SERVICE_VERSION_KEY] = SyncServiceConstants.SYNC_SERVICE_VERSION_VALUE;
                }

                // Raise event for user code
                InvokeOnEndSyncRequest(_outgoingMessage);
            }
            catch (SyncServiceException syncServiceException)
            {
                ProcessSyncServiceException(syncServiceException);
            }
            catch (Exception exception)
            {
                if (WebUtil.IsFatalException(exception))
                {
                    throw;
                }

                _outgoingMessage = CreateMessageFromUnhandledException(exception);
            }

            return(_outgoingMessage);
        }
示例#2
0
        public Stream ProcessRequest(HttpContextServiceHost ctx)
        {
            DateTime  startTime       = DateTime.Now;
            bool      logged          = false;
            Exception raisedException = null;

            try
            {
                try
                {
                    // Intialize the service host first, since most of the logic depends on it.
                    _serviceHost = ctx;// new HttpContextServiceHost(messageBody);
                    //_serviceHost.ValidateRequestHttpVerbAndSegments();

                    // Create the configuration. The first call will initialize the configuration
                    // and all other calls will be a no-op.
                    CreateConfiguration(CurrentScope());

                    // Raise event for user code
                    InvokeOnSyncRequestStart();
                    _requestDescription = new RequestParser(_serviceHost, _syncConfiguration).ParseIncomingRequest();

                    string email;
                    Guid   userId = Logon(ctx, out email);
                    ctx.UserId    = userId.ToString();
                    ctx.UserEMail = email;

                    Common.Logon.CheckLicense(CurrentScope(), ctx.Headers["deviceId"]);
                    Common.Logon.CheckCoreVersion(CurrentScope(), ctx.Headers["coreversion"]);
                    ctx.ResourceVersion = Common.Logon.GetResourceVersion(CurrentScope());

                    //add request parameters
                    _requestDescription.RequestParams = new Dictionary <string, object>();
                    _requestDescription.RequestParams.Add("@UserId", userId);

                    if (null == _requestDescription.SyncBlob || 0 == _requestDescription.SyncBlob.Length)
                    {
                        InitializeNewClient(userId, "Default");
                    }

                    _requestProcessor = RequestProcessorFactory.GetRequestProcessorInstance(_requestDescription.RequestCommand, _syncConfiguration, _serviceHost);

                    _outgoingMessage = _requestProcessor.ProcessRequest(_requestDescription);

                    // Add sync properties
                    var responseProperties = _outgoingMessage.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                    if (null != responseProperties)
                    {
                        responseProperties.Headers[SyncServiceConstants.SYNC_SERVICE_VERSION_KEY] = SyncServiceConstants.SYNC_SERVICE_VERSION_VALUE;
                        responseProperties.Headers[SyncServiceConstants.SYNC_SERVICE_USERID]      = userId.ToString();
                    }

                    // Raise event for user code
                    InvokeOnEndSyncRequest(_outgoingMessage);
                }
                catch (SyncServiceException syncServiceException)
                {
                    raisedException = syncServiceException;
                    ProcessSyncServiceException(ctx, syncServiceException); //_outgoingMessage is set inside
                }
                catch (DbSyncException dbSyncException)
                {
                    raisedException = dbSyncException;
                    if (dbSyncException.Message.StartsWith("Cannot find a valid scope"))
                    {
                        _outgoingMessage = CreateExceptionMessage(ctx, HttpStatusCode.Conflict, dbSyncException.Message);
                    }
                    else
                    {
                        _outgoingMessage = CreateExceptionMessageEx(dbSyncException, ctx);
                    }
                }
                catch (Exception exception)
                {
                    if (WebUtil.IsFatalException(exception))
                    {
                        throw;
                    }

                    raisedException = exception;
                    if (_outgoingMessage == null)
                    {
                        _outgoingMessage = CreateExceptionMessageEx(exception, ctx);
                    }

                    if (_outgoingMessage == null)
                    {
                        _outgoingMessage = CreateMessageFromUnhandledException(ctx, exception);
                    }
                }

                return(MessageToStream(_outgoingMessage, ctx));
            }
            finally
            {
                if (!logged)
                {
                    logged = LogRequestInfo(startTime, ctx.Headers);
                }
                if (raisedException != null)
                {
                    LogException(raisedException);
                }
                LogToDb(_requestDescription.RequestCommand, ctx, startTime, ctx.Headers, raisedException);
            }
        }