Part 04 – Preparing to Deploy to the Cloud Entity Framework and MVC Series Tom Perkins NTPCUG
Environment Change … CURRENT ENVIRONMENT LOCAL MACHINE CONTOSO APP IIS SQL Server Express LocalDB database Web Hosting Provider TARGET ENVIRONMENT CLOUD Database Server Deploy
This tutorial … Two EF 6 Features Connection Resiliency – Automatic retries for transient errors – transient – lasting only for a short time; impermanent Command Interception – Intercept all SQL queries sent to DB to log or change them
Reasons for transient errors App and database may not be in the same datacenter Load balancers may interfere Having many other users may effect responsiveness Service Level Agreement – how frequently you can access the database – throws exceptions (throttling) Contoso App Windows Azure SQL Database Windows Azure SQL Database
Connection Problems May resolve themselves in a short period of time A short wait may lead to a successful operation Connection Resiliency – EF6 feature – Automatically tries transient errors again – Invisible to customer
Connection Resiliency Feature Must be configured for particular database service (EF6 defaults to Windows Azure) Must identify ‘transient’ errors – don’t retry program bugs Wait only an appropriate amount of time before retrying – users are waiting … Try only an appropriate number of times
Contoso App – Enable Connection Resiliency Set SQL database “execution strategy” – EF term for ‘retry policy’ Follow the steps outlined in Part 04 Exercise 1 – Set Retry Policy (Handout)
Command Interception How to test retry policy? Plan: – Intercept SQL queries EF sends to SQL – Replace SQL server response with a transient error exception type EF6’s Interception Feature – Use for logging – Use for simulation of transient errors
Exercise 2 – Logging Interface and Class Perform the steps in Exercise 2 – Create Logging Interface and Implementation Class
Interceptor Classes Called each time EF sends a query to the database Classes derive from DbCommandInterceptor class – Override methods that are called when query is about to be executed – Examine or change the query *** OR *** – Return something to EF without sending it to SQL 2 classes – SchoolInterceptorLogging.cs – does logging – SchoolInterceptorTransientErrors.cs – “Throw” in Search string produces dummy transient errors
Interceptor Classes Follow Exercise 3 – Interceptor Classes
Testing – Exercise 4 To test both logging and transient error handling, follow the steps in Exercise 4 Note that not all the output from the logging and exception has been copied – you should be able to see this on your display as you work through the exercise.
Part 04 – What We Did Enabled Connection Resiliency Log SQL commands from EF to database Next: – Use CodeFirst Migrations to deploy to Azure