(MVC) MVC (2018)

<<назад TransientErrorsLogging.vb (Contoso University).

   1:  Imports System.Data.Common
   2:  Imports System.Data.Entity.Infrastructure.Interception
   3:  Imports System.Data.SqlClient
   4:  Imports System.Reflection
   5:   
   6:  Namespace Log1
   7:   
   8:      Public Class InterceptorTransientErrors
   9:          Inherits DbCommandInterceptor
  10:   
  11:          Private _counter As Integer = 0
  12:          Private _logger As ILogger = New Logger1()
  13:   
  14:          Public Overrides Sub ReaderExecuting(ByVal command As DbCommand, ByVal interceptionContext As DbCommandInterceptionContext(Of DbDataReader))
  15:              Dim throwTransientErrors As Boolean = False
  16:              If command.Parameters.Count > 0 AndAlso command.Parameters(0).Value.ToString() = "%Throw%" Then
  17:                  throwTransientErrors = True
  18:                  command.Parameters(0).Value = "%an%"
  19:                  command.Parameters(1).Value = "%an%"
  20:              End If
  21:   
  22:              If throwTransientErrors AndAlso _counter < 4 Then
  23:                  _logger.Information("Returning transient error for command: {0}", command.CommandText)
  24:                  _counter += 1
  25:                  interceptionContext.Exception = CreateDummySqlException()
  26:              End If
  27:          End Sub
  28:   
  29:          Private Function CreateDummySqlException() As SqlException
  30:              Dim sqlErrorNumber = 20
  31:              Dim sqlErrorCtor = GetType(SqlError).GetConstructors(BindingFlags.Instance Or BindingFlags.NonPublic).Single(Function(c) c.GetParameters().Count() = 7)
  32:              Dim sqlError = sqlErrorCtor.Invoke(New Object() {sqlErrorNumber, CByte(0), CByte(0), "", "", "", 1})
  33:              Dim errorCollection = Activator.CreateInstance(GetType(SqlErrorCollection), True)
  34:              Dim addMethod = GetType(SqlErrorCollection).GetMethod("Add", BindingFlags.Instance Or BindingFlags.NonPublic)
  35:              addMethod.Invoke(errorCollection, {sqlError})
  36:              Dim sqlExceptionCtor = GetType(SqlException).GetConstructors(BindingFlags.Instance Or BindingFlags.NonPublic).Single(Function(c) c.GetParameters().Count() = 4)
  37:              Dim sqlException = CType(sqlExceptionCtor.Invoke(New Object() {"Dummy", errorCollection, Nothing, Guid.NewGuid()}), SqlException)
  38:              Return sqlException
  39:          End Function
  40:      End Class
  41:   
  42:  End Namespace


Comments ( )
Link to this page: //www.vb-net.com/EF-missing-FAQ/Code/TransientErrorsLogging.vb.htm
< THANKS ME>