Пример #1
0
        public void TestExtractAllInnerExceptions()
        {
            // the implementation only concatenates exception type and message (no stacktrace)
            // format is: excp.GetType().Name + " : " + excp.Message + "\n" and this same is repeated as long there are innerexceptions

            ArgumentNullException nex   = new ArgumentNullException("nex", "Parameter cannot be null.");
            ArgumentException     argEx = new ArgumentException("Invalid argument supplied.", nex);
            Exception             ex    = new Exception("An error occured when calling typography service.", argEx);

            string extractedMessage = CoreExtensions.ExtractAllInnerExceptions(ex);

            extractedMessage.Should().NotBeNullOrWhiteSpace();

            // create the expected message parts separated with the \n
            // remove \r\n so that we can split with \n easily
            string partOne   = $"{ex.GetType().Name} : {ex.Message}".Replace("\r\n", string.Empty);
            string partTwo   = $"{argEx.GetType().Name} : {argEx.Message}".Replace("\r\n", string.Empty);
            string partThree = $"{nex.GetType().Name} : {nex.Message}".Replace("\r\n", string.Empty);

            // remove \r\n also from the result (when parameter name is given it will be part of the ex.Message)
            extractedMessage = extractedMessage.Replace("\r\n", string.Empty);

            // don't remove empty entries
            var splittedMsgs = extractedMessage.Split(new string[] { "\n" }, StringSplitOptions.None);

            splittedMsgs.Length.Should().Be(4, "Count should be 4 because there are 3 exceptions and after last exception there will be '\n' which will produce the extra string in the array.");

            splittedMsgs[0].Should().Be(partOne);
            splittedMsgs[1].Should().Be(partTwo);
            splittedMsgs[2].Should().Be(partThree);
        }
Пример #2
0
        public MapServiceProvider(IHostingEnvironment env, ApplicationConfiguration configuration, IOptions <ProxyServerSettings> proxySettings, ILogger <MapServiceProvider> logger)
        {
            this.configuration = configuration;
            this.proxySettings = proxySettings.Value;
            try
            {
                //requestBody = XDocument.Load(env.GetFilePath(frameworkProjectPath, requestBodyFileName));
                using (var streamBodyContent = new MemoryStream(Convert.FromBase64String(requestBodyContent)))
                {
                    requestBody = XDocument.Load(streamBodyContent);
                }
            }
            catch (Exception e)
            {
                logger.LogCritical(CoreExtensions.ExtractAllInnerExceptions(e));
                return;
            }

            query = requestBody.Descendants(wfs + "Query").First();
            query.Remove();
            initialized = true;
        }
Пример #3
0
        /// <summary>
        /// Invoke operation on context in transaction according to selected unit of work
        /// </summary>
        /// <param name="isolationLevel">Desired isolation level of transaction</param>
        /// <param name="action">Set of operations invoked on DB Context</param>
        private void Perform <TUnitOfWork>(IsolationLevel?isolationLevel, Action <TUnitOfWork> action)
        {
            lock (this)
            {
                if (contextInvoked)
                {
                    throw new Exception(CoreMessages.DbContextInUseAlready);
                }
                contextInvoked = true;
                var userName = userIdentification.UserName;
                try
                {
                    int operationRetries = 0;
                    while (true)
                    {
                        using (var serviceScope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
                        {
                            PtvDbContext         dbContext           = serviceScope.ServiceProvider.GetService <PtvDbContext>();
                            TUnitOfWork          unitOfWorkWritable  = serviceScope.ServiceProvider.GetService <TUnitOfWork>();
                            IThreadUserInterface threadUserInterface = serviceScope.ServiceProvider.GetService <IUserIdentification>() as IThreadUserInterface;
                            threadUserInterface?.SetUserName(userName);
                            IDbContextTransaction transaction = isolationLevel != null?dbContext.Database.BeginTransaction(isolationLevel.Value) : null;

                            try
                            {
                                logger.LogDebug($"DBContext called, thread ID {Thread.CurrentThread.ManagedThreadId}");
                                dbContext.Database.OpenConnection();
                                action(unitOfWorkWritable);
                                if (isolationLevel == WriterIsolationLevel)
                                {
                                    CommitTransaction(transaction);
                                }
                                else
                                {
                                    RollBackTransaction(transaction);
                                }
                            }
                            catch (Exception e)
                            {
                                RollBackTransaction(transaction);
                                PrintDbException(e);
                                var ioException = e as IOException;
                                if (HasDeadLockOccured(e) || (ioException != null) || HasTemporaryOccured(e))
                                {
                                    if (operationRetries++ < RetriesOnTemporaryFail)
                                    {
                                        CloseContext(dbContext);
                                        logger.LogInformation(e.Message + Environment.NewLine + "Repeating DBContext operation...");
                                        Random randomGen = new Random(Thread.CurrentThread.ManagedThreadId);
                                        Thread.Sleep((randomGen.Next() % 500) + 10); // random wait
                                        continue;
                                    }
                                    else
                                    {
                                        var errorCode = GetPosgreErrorCode(e);
                                        if (errorCode == PostgreTooManyConnections)
                                        {
                                            throw new PtvDbTooManyConnectionsException();
                                        }
                                    }
                                }
                                if (e is PtvAppException)
                                {
                                    throw;
                                }
                                logger.LogError(CoreExtensions.ExtractAllInnerExceptions(e));
                                throw;
                            }
                            finally
                            {
                                CloseContext(dbContext);
                            }
                            break;
                        }
                    }
                }
                finally
                {
                    contextInvoked = false;
                }
            }
        }
Пример #4
0
        public void TestExtractAllInnerExceptionsWithNull()
        {
            string message = CoreExtensions.ExtractAllInnerExceptions(null);

            message.Should().BeEmpty();
        }