c# - Configuring resiliency settings Entity Framework 6.02 -


i'm trying configure resiliency settings ef6.02. have application in 1 point of execution writes log entry database. application not depending on sql-server if server not respond, want application abandon insert query (through savechanges of dbcontext) , continue execution immediately.

using default settings, debug log outputs ten

"a first chance exception of type 'system.data.sqlclient.sqlexception' occurred in system.data.dll"

after ten tries, throws exception , code continues. want 1 try , example 2 s command timeout. according documentation on msdn, default resiliency method sql server is:

defaultsqlexecutionstrategy: this internal execution strategy used default. strategy not retry @ all, however, wrap exceptions transient inform users might want enable connection resiliency.

as documentation mentions, strategy not retry @ all. still have ten retries. have tried create class inherits dbconfiguration have not found documentation on how change this.

can me reduce number of retries?

update: below code based on suggestions

using system; using system.data.entity; using system.data.entity.sqlserver; using system.data.entity.infrastructure; using system.runtime.remoting.messaging;  namespace mydblayer {     public class myconfiguration : dbconfiguration     {         public myconfiguration ()         {            this.setexecutionstrategy("system.data.sqlclient", () => suspendexecutionstrategy           ? (idbexecutionstrategy)new defaultexecutionstrategy()           : new sqlazureexecutionstrategy());         }          public static bool suspendexecutionstrategy         {                         {                 return (bool?)callcontext.logicalgetdata("suspendexecutionstrategy") ?? false;             }             set             {                 callcontext.logicalsetdata("suspendexecutionstrategy", value);             }         }      } } 

and code writing sql

try     {         using (myentities context = new myentities ())         {             log logentry = new log();             logentry.ts = datetime.now;              myconfiguration.suspendexecutionstrategy = true;             context.log.add(logentry);             context.savechanges();         }     }     catch (exception ex)     {         logger.warn("connection error database server.", ex);     }         {         //enable retries again...         myconfiguration.suspendexecutionstrategy = false;     } 

did try execution strategy suspension?

your own db configuration one:

public class myconfiguration : dbconfiguration  {      public myconfiguration()      {          this.setexecutionstrategy("system.data.sqlclient", () => suspendexecutionstrategy            ? (idbexecutionstrategy)new defaultexecutionstrategy()            : new sqlazureexecutionstrategy());      }       public static bool suspendexecutionstrategy      {                   {              return (bool?)callcontext.logicalgetdata("suspendexecutionstrategy") ?? false;          }          set          {              callcontext.logicalsetdata("suspendexecutionstrategy", value);          }      }  }  

then may define non-retriable command this:

public static void execwithoutretry(system.action action) {     var restoreexecutionstrategystate = ebgdbconfiguration.suspendexecutionstrategy;     try     {         myconfiguration.suspendexecutionstrategy = true;         action();     }     catch (exception)     {         // ignore exception if want     }         {         myconfiguration.suspendexecutionstrategy = restoreexecutionstrategystate;     } } 

and regular db code retriable , non-retriable commands may this:

using (var db = new mycontext()) {     execwithoutretry(() => db.writeevent("my event without retries"));     db.doanyoperationwithretrystrategy(); } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -