Пример #1
0
        protected T ExecStoredProcedure <T>(params object[] arguments)
        {
            using (_logger.BeginScope(nameof(ExecStoredProcedure)))
            {
                try
                {
                    _logger.LogDebug("Searching for calling method");
                    var stackFrames = new StackTrace().GetFrames();

                    var caller = stackFrames.Select(x => (MethodInfo)x.GetMethod())
                                 .Skip(1)
                                 .FirstOrDefault(x => typeof(DbContext).IsAssignableFrom(x.DeclaringType));

                    if (caller == null)
                    {
                        throw new MissingMethodException("Caller procedure could not be found, probably it was removed by optimizer. Check if caller method is marked by [MethodImpl(MethodImplOptions.NoInlining)]");
                    }

                    var fullName = $"{caller.DeclaringType.FullName}.{caller.Name}";
                    lock (SyncRoot.Get(fullName))
                    {
                        _logger.LogDebug($"Found: {fullName}");

                        if (arguments.Length != caller.GetParameters().Length)
                        {
                            throw new InvalidOperationException("ExecStoredProcedure is executed with invalid number of arguments");
                        }

                        var logger            = _parameters.LoggerFactory.CreateLogger($"{nameof(DbContextProxyHelper)}.{nameof(DbContextProxyHelper.CreateProxy)}");
                        var callerProxyMethod = _dbContextHelper.CreateProxy(caller, logger);

                        _logger.LogTrace("Calling proxy method");
                        var result = (T)callerProxyMethod.Invoke(null, arguments.Prepend(this).ToArray());
                        return(result);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, e.Message);
                    throw;
                }
            }
        }