Example #1
0
 private static void AddExceptionInfo(Exception e, DiagTestResult result)
 {
     if (IsLocalRequest)
     {
         result.ExceptionDetails = WebUtil.GetExceptionMessage(e);
     }
 }
Example #2
0
        /// <summary>Create a instance of <see cref="Message" /> from an exception. This is sent as the response.</summary>
        /// <param name="exception">Exception to process</param>
        /// <returns>An instance of <see cref="Message" /> class.</returns>
        private Message CreateMessageFromUnhandledException(Exception exception)
        {
            string exceptionMessage = WebUtil.GetExceptionMessage(exception);

            SyncServiceTracer.TraceError(exceptionMessage);

            return(_syncConfiguration.UseVerboseErrors
                                   ? CreateExceptionMessage(HttpStatusCode.InternalServerError, exceptionMessage)
                                   : CreateExceptionMessage(HttpStatusCode.InternalServerError, Strings.InternalServerError));
        }
Example #3
0
        private void ProcessSyncServiceException(SyncServiceException syncServiceException)
        {
            string exceptionMessage = WebUtil.GetExceptionMessage(syncServiceException);

            SyncServiceTracer.TraceWarning(exceptionMessage);

            _outgoingMessage = _syncConfiguration.UseVerboseErrors
                                   ? CreateExceptionMessage((HttpStatusCode)syncServiceException.StatusCode, exceptionMessage)
                                   : CreateExceptionMessage((HttpStatusCode)syncServiceException.StatusCode, syncServiceException.Message);

            // Add the "Allow" HTTP header if present._outgoingMessage.Properties[HttpResponseMessageProperty.Name].
            if (!String.IsNullOrEmpty(syncServiceException.ResponseAllowHeader) &&
                null != _outgoingMessage.Properties[HttpResponseMessageProperty.Name])
            {
                ((HttpResponseMessageProperty)_outgoingMessage.Properties[HttpResponseMessageProperty.Name]).
                Headers.Add("Allow", syncServiceException.ResponseAllowHeader);
            }
        }
        //Note: This method changes the state of a static class. So results may not be as expected in multi-threaded scenarios.
        internal void InvokeTestHookInitializeMethod(Type type)
        {
            if (WebUtil.IsFriendClass(type))
            {
                const BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly;
                MethodInfo         info        = type.GetMethod("InitializeConfigurationTestHook", bindingAttr, null, new[] { typeof(ISyncServiceConfiguration) }, null);

                if ((info != null) && (info.ReturnType == typeof(void)))
                {
                    ParameterInfo[] parameters = info.GetParameters();
                    if ((parameters.Length == 1) && !parameters[0].IsOut)
                    {
                        var objArray = new object[] { this };
                        try
                        {
                            info.Invoke(null, objArray);
                        }
                        catch (TargetInvocationException exception)
                        {
                            SyncTracer.Warning("Exception invoking the TestHookInitialization method. Details {0}", WebUtil.GetExceptionMessage(exception));
                            ErrorHandler.HandleTargetInvocationException(exception);
                            throw;
                        }
                        return;
                    }
                }
            }
        }
        /// <summary>
        /// Invokes the InitializeService user method.
        /// </summary>
        /// <param name="type">service type (used for reflection)</param>
        private void InvokeStaticInitialization(Type type)
        {
            // Search for the InitializeService method going from most-specific to least-specific type.

            const BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly;

            while (type != null)
            {
                MethodInfo info = type.GetMethod("InitializeService", bindingAttr, null, new[] { typeof(ISyncServiceConfiguration) }, null);

                if ((info != null) && (info.ReturnType == typeof(void)))
                {
                    ParameterInfo[] parameters = info.GetParameters();

                    if ((parameters.Length == 1) && !parameters[0].IsOut)
                    {
                        var objArray = new object[] { this };

                        try
                        {
                            info.Invoke(null, objArray);

                            return;
                        }
                        catch (TargetInvocationException exception)
                        {
                            SyncTracer.Warning("Exception invoking the static InitializeService method. Details {0}", WebUtil.GetExceptionMessage(exception));

                            ErrorHandler.HandleTargetInvocationException(exception);

                            throw;
                        }
                    }
                }

                type = type.BaseType;
            }

            // We should never exit from here when the InitializeService method is implemented.
            throw SyncServiceException.CreateInternalServerError(Strings.InitializeServiceMethodNotImplemented);
        }
 /// <summary>
 /// Delegate passed into the custom body writer to form the outgoing response.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="syncWriter"></param>
 private static void WriteResponse(XmlDictionaryWriter writer, SyncWriter syncWriter)
 {
     try
     {
         syncWriter.WriteFeed(writer);
     }
     catch (Exception exception)
     {
         // An exception at this point seems to be unrecoverable but ideally we should not hit exceptions since we are only
         // writing to the XmlDictionaryWriter.
         SyncServiceTracer.TraceError("Exception in WriteResponse method. Details: {0}", WebUtil.GetExceptionMessage(exception));
     }
 }
Example #7
0
        /// <summary>Check whether a connection can be opened successfully to the database.</summary>
        /// <param name="configuration">Service configuration</param>
        /// <returns>Result of the diagnostic check</returns>
        private static DiagTestResult CheckSqlConnection(SyncServiceConfiguration configuration)
        {
            var result = new DiagTestResult();

            try
            {
                new SqlConnectionStringBuilder(configuration.ServerConnectionString);
            }
            catch (KeyNotFoundException keyNotFoundException)
            {
                result.TestResult = DiagConstants.INVALID_SQL_CONNECTION_STRING;

                if (IsLocalRequest)
                {
                    result.ExceptionDetails = WebUtil.GetExceptionMessage(keyNotFoundException);
                }
            }
            catch (FormatException formatException)
            {
                result.TestResult = DiagConstants.INVALID_SQL_CONNECTION_STRING;

                if (IsLocalRequest)
                {
                    result.ExceptionDetails = WebUtil.GetExceptionMessage(formatException);
                }
            }
            catch (ArgumentException argumentException)
            {
                result.TestResult = DiagConstants.INVALID_SQL_CONNECTION_STRING;

                if (IsLocalRequest)
                {
                    result.ExceptionDetails = WebUtil.GetExceptionMessage(argumentException);
                }
            }
            catch (Exception e)
            {
                result.TestResult = DiagConstants.UNKNOWN_ERROR;

                if (IsLocalRequest)
                {
                    result.ExceptionDetails = WebUtil.GetExceptionMessage(e);
                }
            }

            if (result.TestResult == DiagConstants.NOT_DETERMINED)
            {
                try
                {
                    using (var connection = new SqlConnection(configuration.ServerConnectionString))
                    {
                        connection.Open();
                    }

                    result.TestResult = DiagConstants.SUCCESS;
                }
                catch (InvalidOperationException invalidOperationException)
                {
                    result.TestResult = DiagConstants.ERROR_OPENING_SQL_CONNECTION;

                    if (IsLocalRequest)
                    {
                        result.ExceptionDetails = WebUtil.GetExceptionMessage(invalidOperationException);
                    }
                }
                catch (SqlException sqlException)
                {
                    result.TestResult = DiagConstants.ERROR_OPENING_SQL_CONNECTION;

                    if (IsLocalRequest)
                    {
                        result.ExceptionDetails = WebUtil.GetExceptionMessage(sqlException);
                    }
                }
                catch (ArgumentException argumentException)
                {
                    result.TestResult = DiagConstants.ERROR_OPENING_SQL_CONNECTION;

                    if (IsLocalRequest)
                    {
                        result.ExceptionDetails = WebUtil.GetExceptionMessage(argumentException);
                    }
                }
                catch (Exception e)
                {
                    result.TestResult = DiagConstants.UNKNOWN_ERROR;

                    if (IsLocalRequest)
                    {
                        result.ExceptionDetails = WebUtil.GetExceptionMessage(e);
                    }
                }
            }

            return(result);
        }
Example #8
0
        /// <summary>
        /// Read and parse the incoming request stream for a POST request.
        /// </summary>
        private void ReadIncomingRequestStreamForPost()
        {
            if (null == _serviceHost.RequestStream || !_serviceHost.RequestStream.CanRead)
            {
                SyncTracer.Info("Request stream for HTTP POST is empty, null or cannot be read.");
                return;
            }

            try
            {
                var reader = WebUtil.GetSyncReader(_serviceHost.GetRequestContentSerializationFormat(),
                                                   _serviceHost.RequestStream,
                                                   _configuration.TypeToTableGlobalNameMapping.Keys.ToArray());

                reader.Start();

                while (reader.Next())
                {
                    switch (reader.ItemType)
                    {
                    case ReaderItemType.Entry:
                        IOfflineEntity entity = reader.GetItem();

                        if (entity.ServiceMetadata.IsTombstone)
                        {
                            if (String.IsNullOrEmpty(entity.ServiceMetadata.Id))
                            {
                                throw SyncServiceException.CreateBadRequestError(Strings.TombstoneEntityHasNoId);
                            }

                            WebUtil.ParseIdStringAndPopulateKeyFields(entity, _serviceHost.ServiceBaseUri);
                        }

                        _entityList.Add(entity);

                        bool hasTempId = false;
                        if (reader.HasTempId())
                        {
                            // Save the entity id to tempId mapping for use later when writing response.
                            _idToTempIdMapping.Add(WebUtil.GenerateOfflineEntityId(entity), reader.GetTempId());

                            hasTempId = true;
                        }

                        // Make sure, we have atleast one of Id or TempId
                        if (String.IsNullOrEmpty(entity.ServiceMetadata.Id) && !hasTempId)
                        {
                            throw SyncServiceException.CreateBadRequestError(Strings.BothIdAndTempIdAreMissing);
                        }

                        break;

                    case ReaderItemType.SyncBlob:
                        _syncBlob = reader.GetServerBlob();

                        break;
                    }
                }
            }
            catch (XmlException exception)
            {
                SyncTracer.Warning("XmlException: {0}", WebUtil.GetExceptionMessage(exception));

                throw SyncServiceException.CreateBadRequestError(Strings.BadRequestPayload);
            }
        }