<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jeff Hollan</title>
    <description>The latest articles on DEV Community by Jeff Hollan (@jeffhollan).</description>
    <link>https://dev.to/jeffhollan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F138867%2Fd544b587-16fc-42eb-9e46-bb86bf476662.jpg</url>
      <title>DEV Community: Jeff Hollan</title>
      <link>https://dev.to/jeffhollan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeffhollan"/>
    <language>en</language>
    <item>
      <title>Dapr is like jQuery for microservices</title>
      <dc:creator>Jeff Hollan</dc:creator>
      <pubDate>Mon, 21 Mar 2022 15:04:23 +0000</pubDate>
      <link>https://dev.to/jeffhollan/dapr-is-like-jquery-for-microservices-6ld</link>
      <guid>https://dev.to/jeffhollan/dapr-is-like-jquery-for-microservices-6ld</guid>
      <description>&lt;p&gt;There's an emerging piece of open source tech I'm very excited about. If you haven't heard of &lt;a href="https://dapr.io" rel="noopener noreferrer"&gt;Dapr&lt;/a&gt;, it's one of the newest additions to the Cloud Native Computing Foundation (CNCF) and promises to provide portable and reliable microservices. While on the surface that may sound great, a lot of you may be wondering "ok but what is it? how does it work?"  While analogies and comparisons will never be perfect, I love to use them to help me contextualize.  So while the comparison is imperfect, I personally think of Dapr like jQuery for microservices.&lt;/p&gt;

&lt;p&gt;Ok hear me out. I realize that sentiment may evoke a range of emotions, but let me explain. This is heavily influenced by my experiences in web development. So let’s head back in time.&lt;/p&gt;

&lt;p&gt;I remember over two decades ago building some of my first web pages. These were the days of GeoCities, and later Blogger and MySpace. I remember using text editors to write static HTML files, and spicing it up with the latest and greatest tech including MIDI files, guest books, and page visit counters. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstryvemarketing.com%2Fwp-content%2Fuploads%2F2016%2F04%2Fflamingline.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstryvemarketing.com%2Fwp-content%2Fuploads%2F2016%2F04%2Fflamingline.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But as we moved into the 2000s, the web shifted. You may hear folks now reference this as Web 2.0, but to me it was discovering a new paradigm of web experiences, led by Gmail, YouTube, and Facebook. &lt;/p&gt;

&lt;p&gt;These pages revolutionized what a website could do. A web page could be dynamic, with controls or content that was able to update without clicking a hyperlink or navigating pages. As I was building web pages, I didn’t even know where to start to try to weave in this type of rich functionality. But in the late aughts that all changed when I got introduced to jQuery. To me, jQuery was magic. Someone like me, with only basic web development skills, could now create dynamic sites. Commands like &lt;code&gt;$("button").click&lt;/code&gt;, &lt;code&gt;$.ajax()&lt;/code&gt;, and &lt;code&gt;$("property").hide&lt;/code&gt; unlocked these capabilities by pulling in the jQuery script files onto any page and using that beautiful &lt;code&gt;$&lt;/code&gt; alias and APIs. At the time, I would recommend jQuery to anyone and everyone building websites because it made the complex simple. &lt;/p&gt;

&lt;p&gt;Times have changed some, and now in 2022, I don’t use jQuery when building sites. Those patterns have been baked into modern browsers and JavaScript and frameworks like Angular, React, and Vue. But jQuery, at least for me, was what democratized this new wave of dynamic websites.&lt;/p&gt;

&lt;p&gt;While dynamic web pages are no longer a mystery, modern microservices and cloud native is still heavily evolving. Microservices can trace some roots back to SOA, which is anything but "new," but there are still a set of common challenges when building microservices, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How can I make my services resilient to failures?&lt;/li&gt;
&lt;li&gt;How can I have better logging and tracing of my services?&lt;/li&gt;
&lt;li&gt;How do I secure and encrypt my sevices?&lt;/li&gt;
&lt;li&gt;How can I manage state efficiently and uniformly across my services?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are a variety of ways to solve each of the above. I could write custom retry logic in my code to catch and retry. I could pull in SDKs for observability and logging solutions, or could integrate service meshes to manage inter-service communication. But in the same way dynamic webpages felt advanced and overwhelming for me without jQuery, solving each of these problems on its own can be a daunting task for any team building microservices. This is why for me, Dapr is doing for microservices what jQuery did for dynamic web pages: providing a simplified and developer friendly way to solve a variety of common challenges.&lt;/p&gt;

&lt;p&gt;As mentioned at the beginning of the blog, Dapr is a set of APIs for building portable and reliable microservices. I can pull Dapr into my microservices architecture and have out of the box APIs to perform tasks like secure service invocation, publish &amp;amp; subscribe, and secrets management. Let’s take a simple example.&lt;/p&gt;

&lt;p&gt;Imagine I have two services: service A and service B. I have deployed both services, and now want to call service B from service A within an orchestrator like Kubernetes. Without much effort I could call service B over HTTP (&lt;code&gt;http://serviceB/hello&lt;/code&gt;). This works, but it’s missing a few useful capabilities. How can I make sure the network traffic between A and B is secured and encrypted? What if service B has a transient issue I want to automatically handle and retry? How can I make sure whenever service A calls service B I can trace that call and “follow the message” from origin to eventual destination? With these considerations, my simple call over HTTP now falls short. &lt;/p&gt;

&lt;p&gt;The good news? I can simply swap out the call over HTTP with the Dapr API to “invoke” and get service A to call service B, but Dapr now layers on resiliency, encryption, and tracing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;daprClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;invoker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;serviceB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HttpMethod&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wqaesq2onmxryxe5xax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wqaesq2onmxryxe5xax.png" alt="[Service invocation overview | Dapr Docs](https://docs.dapr.io/developing-applications/building-blocks/service-invocation/service-invocation-overview/)"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.dapr.io/developing-applications/building-blocks/service-invocation/service-invocation-overview/" rel="noopener noreferrer"&gt;Service invocation overview&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s just one of many examples in how a simple API call to Dapr will layer on best practices and solutions to common microservices challenges.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifpxl3v0dya33rro8hiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifpxl3v0dya33rro8hiy.png" alt="[Building blocks | Dapr Docs](https://docs.dapr.io/concepts/building-blocks-concept/)"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.dapr.io/concepts/building-blocks-concept/" rel="noopener noreferrer"&gt;Dapr Building Blocks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ve used Dapr a bit in the last year and have really come to value it in my toolchain. I personally find the service invocation APIs and observability APIs the most useful. I may use Dapr for state management, but other times I choose to leverage the state SDKs (like Redis or Cosmos DB) directly in my code. But that’s one part I like about how Dapr was built: you can mix and match just the APIs that make sense for your current project and team.&lt;/p&gt;

&lt;p&gt;If you haven’t tried Dapr, it’s simple to get started using the &lt;a href="https://docs.dapr.io/getting-started/" rel="noopener noreferrer"&gt;Dapr quickstarts&lt;/a&gt;. There are options to try out locally or hosted in Kubernetes.&lt;/p&gt;

&lt;p&gt;As our team has been building &lt;a href="http://aka.ms/containerapps" rel="noopener noreferrer"&gt;Azure Container Apps&lt;/a&gt;, a serverless container offering built on Kubernetes, we wanted to make it as easy as possible for users to leverage these Dapr APIs for containers and microservices. So we’ve baked Dapr into the service as a feature that can be easily enabled for any container. If you want to try creating a hosted Dapr app, check out this tutorial of creating a &lt;a href="https://docs.microsoft.com/en-us/azure/container-apps/microservices-dapr?tabs=bash" rel="noopener noreferrer"&gt;microservice with Dapr in container apps&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The same way I would recommend any web developer in the 2000s use jQuery, I strongly recommend Dapr for anyone building microservices in 2022 and beyond. Is it possible in 20 years these patterns and practices from Dapr will be built into these underlying microservice platforms the way jQuery APIs were for web? Who knows. But until I’ll be using Dapr in my microservice development.&lt;/p&gt;




&lt;p&gt;Jeff Hollan is Director of Product for Azure PaaS and Serverless. &lt;a href="https://twitter.com/jeffhollan" rel="noopener noreferrer"&gt;@jeffhollan&lt;/a&gt; on Twitter.&lt;/p&gt;

</description>
      <category>microservices</category>
      <category>kubernetes</category>
      <category>azure</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Serverless circuit breakers with Durable Entities</title>
      <dc:creator>Jeff Hollan</dc:creator>
      <pubDate>Mon, 21 Oct 2019 15:01:11 +0000</pubDate>
      <link>https://dev.to/azure/serverless-circuit-breakers-with-durable-entities-3l2f</link>
      <guid>https://dev.to/azure/serverless-circuit-breakers-with-durable-entities-3l2f</guid>
      <description>&lt;p&gt;I remember a call with a customer from my first few months on the Azure Functions team.  The customer’s function triggered from a queue message and ran a stored procedure in a SQL database.  The solution had been working fantastic for months at a high scale until one fateful day the SQL database crashed.  What resulted is a serverless version of kicking someone while they're down.  The function executions started to fail, and as they failed, the message would be re-queued and retried (which is great! Retrying generally is a best practice, especially for transient failures).  But more queue messages kept landing in the queue, in addition to a growing number of retries.  Within minutes they had gone from hundreds of failed executions to thousands.  What's worse, once the SQL database failure was resolved, Azure Functions was ready to &lt;em&gt;hammer&lt;/em&gt; that poor SQL database back into a failed state with a mountain of queue messages and retries it had been holding up.  Eventually, things got back into a steady-state, but the team was left with thousands of messages in a dead-letter queue and some battle scars.  It's a powerful reminder that sometimes serverless scale can make a bad situation worse.&lt;/p&gt;

&lt;p&gt;What the customer wanted was simple enough to understand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Can we make it so our function stops triggering if lots of failures start happening?" &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s a simple problem that has been asked by many a developer.  &lt;strong&gt;I want to show how you can use the new Durable Entities feature of Azure Functions to create a stateful circuit breaker to achieve exactly that.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Traditional circuit breakers and serverless scale
&lt;/h2&gt;

&lt;p&gt;The pattern isn't a new one.  Generally, it's known as the circuit breaker pattern.  You can think of it as an electric circuit with a gate.  Imagine electricity is flowing through a circuit to a destination.  A gate is there bridging the connection.  If you ever need to stop the flow of electricity, the gate "opens" creating a gap in the circuit, and stopping electricity.  When electricity should resume, you "close" the gate connecting the circuit.&lt;/p&gt;

&lt;p&gt;Many libraries exist that can implement this pattern in traditional apps.  &lt;a href="https://github.com/App-vNext/Polly/wiki/Circuit-Breaker"&gt;Polly&lt;/a&gt; for .NET is my favorite, but there are plenty to choose from.  You can define a threshold number of exceptions within a range - if I get more than 30 exceptions in 1 minute - and it "opens" the gate to stop processing.  While this works for many applications, serverless presents new challenges where this model doesn't fit.&lt;/p&gt;

&lt;h3&gt;
  
  
  Single server vs distributed apps
&lt;/h3&gt;

&lt;p&gt;Assume I'm processing all of my queue messages on a single server.  Here, libraries like Polly have worked great.  Polly keeps an in-memory count of exceptions as they occur, and it can easily keep track of the threshold because &lt;strong&gt;every exception will only occur on that single instance.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Distributed applications like serverless functions are much different.  Within seconds under load, an Azure Function can scale to dozens of instances.  Imagine now that you have 50 active function instances triggering on events.  You could have a circuit breaker library running in your app - but you'd have 50 instances of that circuit breaker library.  Now imagine that you get 50 exceptions all at once, but each exception spreads evenly across your instances.  Each library, like Polly, will think everything is healthy.  "Hey I'm good, I only saw one exception, and my threshold is 30, so let's keep the circuit closed."  But the global story is much different.  Globally the threshold was crossed, but because each instance was processing in isolation, there was nothing that knew the state and health of the entire circuit.  Not to mention, even if one instance "halted" processing, the other 49 may keep chugging along.&lt;/p&gt;

&lt;p&gt;The scaling and abstraction of all of these instances make traditional circuit breaker libraries unpredictable.  Sometimes 30 exceptions could open the circuit breaker if they all happen on a single instance, while other times it could take many many more exceptions to get a successfully opened gate.&lt;/p&gt;

&lt;h2&gt;
  
  
  State management and distributed circuits
&lt;/h2&gt;

&lt;p&gt;The missing piece here for a distributed circuit breaker is some external state that can monitor the health and status of the entire circuit.  There's no single answer on how that state could manifest.  The state could be Azure Alerts and metrics that aggregate exceptions and take some action.  The state could be an Azure Logic App like &lt;a href="https://medium.com/hackernoon/reliable-event-processing-in-azure-functions-37054dc2d0fc"&gt;an example I created a few years back before we had Durable Entities&lt;/a&gt;.  My current weapon of choice though is a Durable Entity.&lt;/p&gt;

&lt;p&gt;Let's step away from this circuit breaker problem for a beat and introduce what Durable Entities are.&lt;/p&gt;

&lt;h2&gt;
  
  
  Durable Entities and stateful functions
&lt;/h2&gt;

&lt;p&gt;Serverless functions are no longer the stateless ephemeral snippets of code you've likely heard of.  With new capabilities like &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview"&gt;Azure Durable Functions&lt;/a&gt; you can now write Azure Functions that maintain state for an indefinite amount of time.  This is extremely useful when you need to orchestrate or coordinate work as it moves through a system.  &lt;/p&gt;

&lt;p&gt;Recently we've released a new flavor of Durable Functions called &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-entities"&gt;Durable Entities&lt;/a&gt;.  You can think of Durable Entities like a function that can have infinite instances, each with its unique state and ID.  The basic example is something like a counter.  Let's say you've now become the lead architect for Fitbit and need to build a solution that receives step counts for each of the thousands of users and keep them stored in state.  Each Fitbit device will only ever send one signal: "Add Step."  You could write a stateless function that interacts with a database, and does something like "lookup this user, get the current step count, add one, and save it back to the database."  But it's a bit cumbersome to write, and things start to get messy when I think about how I can prevent two signals from executing at the same time on the same user, and what was two unique step signals only end up incrementing the database by one.&lt;/p&gt;

&lt;p&gt;Durable Entities lets me describe that entity - an instance of a counter - while maintaining state and single-threaded operations on each instance.  The code ends up looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Counter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;JsonProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"steps"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;CurrentValue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddStep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CurrentValue&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Reset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CurrentValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;FunctionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;EntityTrigger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;IDurableEntityContext&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DispatchAsync&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it - that's an entire Azure Function app.  Once I publish this, I can use the Durable SDK or HTTP API to do things like "&lt;code&gt;AddStep&lt;/code&gt; for Jeff" or "&lt;code&gt;AddStep&lt;/code&gt; for Chris."  I could have infinite instances of these counters, each one storing the state for that user.  And these Durable Entities are just as serverless as Azure Functions.  If I send 1,000 steps for Jeff, my function will scale and run and process those 1,000 events (guaranteeing that after all 1,000 are processed my count is "1000").  I pay only for those 1,000 steps.  If I never call that function again, the state still lives (in Azure Storage by default), but I never pay for the function compute until I use it again.&lt;/p&gt;

&lt;p&gt;Durable Entities make managing state extremely intuitive.  We've seen tremendous interest in scenarios like IoT, where each IoT device could persist and expose state operations as a durable entity.  My personal favorite use of entities though is to solve our circuit breaker dilemma.&lt;/p&gt;

&lt;h2&gt;
  
  
  Durable Entities and Circuit State
&lt;/h2&gt;

&lt;p&gt;Let's pull these threads back together.  If you recall from the beginning, we have a problem: we need to be able to manage the state and rate of exceptions external to each function.  We also have a pretty slick new tool of Durable Entities.  Now I want to show how you can combine these for a serverless circuit breaker.&lt;/p&gt;

&lt;p&gt;First, let's break down the flow of how things will work.  There are two main components.  The function app that will be triggering and processing messages from something like an Azure Event Hub or queue, and a durable entity that will monitor and manage the state of the circuit.&lt;/p&gt;

&lt;h4&gt;
  
  
  Azure Function
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Trigger and try to process the message&lt;/li&gt;
&lt;li&gt;If there is an exception, send a signal to the durable entity to let it know it hit an exception&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Azure Function Code
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;FunctionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MyFunction"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nf"&gt;QueueTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"myqueue"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// try to process the message&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;// hit an exception&lt;/span&gt;
       &lt;span class="c1"&gt;// send a signal to the Durable Entity&lt;/span&gt;
       &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PostAsJsonAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entityUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FailureRequest&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;FailureTime&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UtcNow&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;               

       &lt;span class="c1"&gt;// Throw the exception so the retries will kick in&lt;/span&gt;
       &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Durable Entity
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Keep track of how many exceptions have been reported across all scaled out instances of a function&lt;/li&gt;
&lt;li&gt;If several exceptions within a certain period are reported, break the circuit&lt;/li&gt;
&lt;li&gt;When breaking the circuit, use the Azure API to stop the Azure Function&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Durable Function Code
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;JsonObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MemberSerialization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OptIn&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circuit&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;   
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;JsonProperty&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;JsonConverter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StringEnumConverter&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;CircuitState&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CircuitState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Closed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Current rolling window of failures reported for this circuit&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;JsonProperty&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IDictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FailureRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FailureWindow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FailureRequest&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CloseCircuit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CircuitState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Closed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OpenCircuit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CircuitState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;AddFailure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FailureRequest&lt;/span&gt; &lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Check to make sure the circuit isn't already opened&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CircuitState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Tried to add additional failure to &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EntityKey&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; that is already opened.  Close the circuit to resume processing"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Add this failure to the stateful aggragate&lt;/span&gt;
        &lt;span class="n"&gt;FailureWindow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RequestId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Calculate the time window we should evaluate exceptions for&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;thresholdCutoff&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FailureTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;windowSize&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Filter the window only to exceptions within the cutoff timespan&lt;/span&gt;
        &lt;span class="n"&gt;FailureWindow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FailureWindow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FailureTime&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;thresholdCutoff&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;ToDictionary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FailureWindow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;failureThreshold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogCritical&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Break this circuit for entity &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EntityKey&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Kick off a call to disable the Azure Function App&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_durableClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StartNewAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OpenCircuitOrchestrator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenCircuit&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResourceId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Mark the circuit as open&lt;/span&gt;
            &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CircuitState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"The circuit &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Entity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EntityKey&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; currently has &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;FailureWindow&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; exceptions in the window of &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;windowSize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;FunctionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Circuit&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;EntityTrigger&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;IDurableEntityContext&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DispatchAsync&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Circuit&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are other ways you can chain these together - like adding some in-memory retries to the function or even having the function explicitly check the state of the circuit before processing the message, but this flow is my favorite and optimizes for high throughput and low cost.  I can deploy the durable entity and function app, and once the durable entity detects the number of failures is too high it will automatically disable the Azure Function app so it stops processing.  It is worth noting you can use the same durable entity to monitor and manage the circuit for many different function apps in your subscription at the same time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/jeffhollan/functions-durable-actor-circuitbreaker"&gt;The full code for my sample is on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sample circuit breaker scenario
&lt;/h3&gt;

&lt;p&gt;To help make this pattern clear, let's walk through an example.  Imagine I have the Azure Function above and the Durable Entity deployed in my subscription.  We'll use the same scenario as we started this post with - the function triggers on the queue, interacts with a SQL server, and completes processing.&lt;/p&gt;

&lt;p&gt;My function could be running for months without issue.  All during this time, my durable entity is sitting idle (and free) in my subscription.  Assume the function is triggering on queue messages across 100 active instances.&lt;/p&gt;

&lt;p&gt;Suddenly, the SQL server starts having some issues.  A few of the messages start throwing exceptions.  An instance hits an exception and lets the durable entity know.  The durable entity keeps track that 1 exception has happened in the last 30 seconds.  Moments later, 20 other instances hit an exception.  Each sends a signal to the durable entity, which now knows that 21 exceptions have happened in the span of a few seconds.  Finally, the failures start to mount, and quickly the durable entity detects the threshold has crossed - it has the state for over 30 exceptions in a 30 second window.&lt;/p&gt;

&lt;p&gt;The logic activates the breaking and opening of the circuit.  It makes a call to the Azure APIs and disables the function app so it stops triggering.  No messages are lost - the queue messages stay safely in the Service Bus queue.  But rather than creating cascading failures, I've gracefully broken the circuit until the health of the systems can be confirmed, the circuit closed, and processing resumes.&lt;/p&gt;

&lt;p&gt;With durable entities and Azure Functions we've very efficiently solved the problem we started with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Can we make it so our function stops triggering if lots of failures start happening?" &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  More than just functions
&lt;/h2&gt;

&lt;p&gt;You can use Durable Entities to manage the state of any distributed app.  &lt;a href="https://github.com/App-vNext/Polly/issues/687"&gt;Polly recently announced support for Durable Entity powered circuit breakers&lt;/a&gt; that can manage the state of any application.  I'd encourage you to check them out, and give durable entities a spin!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>serverless</category>
      <category>csharp</category>
      <category>azure</category>
    </item>
    <item>
      <title>Develop Azure Functions using .NET Core 3.1</title>
      <dc:creator>Jeff Hollan</dc:creator>
      <pubDate>Thu, 17 Oct 2019 20:15:35 +0000</pubDate>
      <link>https://dev.to/azure/develop-azure-functions-using-net-core-3-0-gcm</link>
      <guid>https://dev.to/azure/develop-azure-functions-using-net-core-3-0-gcm</guid>
      <description>&lt;p&gt;&amp;lt;&amp;lt; Updated on Dec 9 2019 with go-live announcement of Azure Functions on .NET Core 3.1 &amp;gt;&amp;gt;&lt;/p&gt;

&lt;p&gt;We're extremely excited to announce you can now develop and publish Azure Functions built on top of the .NET Core 3.1 runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to know
&lt;/h2&gt;

&lt;p&gt;Azure Functions 3.0 is now go-live and ready for production.  It is a highly backwards compatible release.  One of the immediate benefits of this release is you can write Azure Functions targeting &lt;code&gt;netcoreapp3.1&lt;/code&gt;.  You should also be able to move existing ~2 apps to ~3 without issue.  A list of considerations on changes between 2.x and 3.x can &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions#migrating-from-2x-to-3x"&gt;be found in our docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can write any Azure Function language in 3.0 (Java, JavaScript, PowerShell, TypeScript, Python) - though note some of the tooling and integrations will continue to receive updates until mid-January.  Instructions on changing your tools or apps to target 3.x for other languages &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions#locally-developed-application-versions"&gt;can be found in our docs&lt;/a&gt;.  For now let's walk through creating a .NET Core 3.1 app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;h4&gt;
  
  
  &lt;a href="https://dotnet.microsoft.com/download"&gt;Install the .NET Core 3.1 SDK&lt;/a&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Install the 3.x core tools (optional for Visual Studio development)
&lt;/h4&gt;

&lt;p&gt;Using npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g azure-functions-core-tools@3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using brew:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew tap azure/functions
brew install azure-functions-core-tools@3
# If you already have func installed
brew link --overwrite azure-functions-core-tools@3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Create an Azure Functions project
&lt;/h4&gt;

&lt;p&gt;You have a few options here.  You could create a new project using our &lt;a href="https://docs.microsoft.com/azure/azure-functions/functions-create-your-first-function-visual-studio"&gt;Visual Studio&lt;/a&gt;, &lt;a href="https://docs.microsoft.com/azure/azure-functions/functions-create-first-function-vs-code"&gt;Visual Studio Code&lt;/a&gt;, or &lt;a href="https://docs.microsoft.com/azure/azure-functions/functions-run-local#create-a-local-functions-project"&gt;CLI&lt;/a&gt; tools.  For other tools, once you install the 3.x core tools you should be set.  If attempting to target .NET Core 3.1 just be sure to update your .csproj file template with &lt;code&gt;netcoreapp3.1&lt;/code&gt;.  Also be sure to target the `&lt;code&gt;Microsoft.NET.Sdk.Functions&lt;/code&gt; package version of at least &lt;code&gt;3.0.1&lt;/code&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  Creating with Visual Studio 2019
&lt;/h5&gt;

&lt;p&gt;If you want to develop using .NET Core 3.1, you must use Visual Studio 2019 16.4 or newer. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open Visual Studio and select to create a new Azure Functions project

&lt;ul&gt;
&lt;li&gt;You should see an indicator at the bottom as it downloads the runtime and templates of "Making sure all templates are up to date."  This could take a few minutes, but wait for it to finish.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Once the templates are up to date, go back one screen and forward so template options are refreshed.&lt;/li&gt;
&lt;li&gt;Select the now available "Azure Functions v3" option from the template drop-down.&lt;/li&gt;
&lt;li&gt;Create your project.&lt;/li&gt;
&lt;li&gt;If you want to target .NET Core 3.1, edit the project properties and select .NET Core 3.1.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should be set!  You can now debug, test, and publish this function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Publish your Function App
&lt;/h4&gt;

&lt;p&gt;You can publish your function using the regular tools.  Depending on the tool you will likely see a prompt to update your &lt;code&gt;FUNCTIONS_EXTENSION_VERSION&lt;/code&gt; application setting from the default &lt;code&gt;~2&lt;/code&gt; to &lt;code&gt;~3&lt;/code&gt;.  This does need to occur before a 3.x app can run successfully in Azure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Upgrading a v2 app to v3
&lt;/h2&gt;

&lt;p&gt;If you have an existing app and want to test it out with the new release, the following settings would need to be modified:&lt;/p&gt;

&lt;h3&gt;
  
  
  Non-.NET Functions
&lt;/h3&gt;

&lt;p&gt;Locally once you start using the 3.x CLI your apps will debug and run in the 3.0 environment automatically.  For apps in the cloud, you simply need to update the app setting of &lt;code&gt;FUNCTIONS_EXTENSION_VERSION&lt;/code&gt; to &lt;code&gt;~3&lt;/code&gt; - but make sure to do some validation tests first before changing anything in production 😅.&lt;/p&gt;

&lt;h3&gt;
  
  
  .NET Functions
&lt;/h3&gt;

&lt;p&gt;If using Visual Studio, make sure you've already followed all of the steps in the "Creating with Visual Studio 2019" section to ensure you have the latest templates and project options available before attempting to upgrade.  You may need to go to the new app template screen to get the 3.x bits to pull in on your machine, so if you get an error like "no runtime available that matches the version," try creating a new 3.x app from scratch first to make sure VS has fetched all the latest bits.&lt;/p&gt;

&lt;p&gt;To change a project between ~2 and ~3 locally, update the &lt;code&gt;.csproj&lt;/code&gt; file of the project and change:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;AzureFunctionsVersion&lt;/code&gt; from &lt;code&gt;v2&lt;/code&gt; to &lt;code&gt;v3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Microsoft.NET.Sdk.Functions&lt;/code&gt; package version to at least &lt;code&gt;3.0.1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Optionally, you can change to target &lt;code&gt;netcoreapp3.1&lt;/code&gt; if you want to target the latest .NET Core version&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You &lt;strong&gt;should not&lt;/strong&gt; change the &lt;code&gt;host.json&lt;/code&gt; version to &lt;code&gt;3.0&lt;/code&gt;.  The &lt;code&gt;host.json&lt;/code&gt; schema version is different than the function runtime version, and is still 2.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  Path to GA
&lt;/h2&gt;

&lt;p&gt;While you can begin using 3.x in production today, we are tracking a few work items around tooling and platform features before we announce full GA for tools and runtime in January 2020.  You can see some of those work items called out &lt;a href="https://github.com/Azure/azure-functions-host/wiki/Functions-3.x-go-live-work-items"&gt;in GitHub&lt;/a&gt;&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>azure</category>
    </item>
    <item>
      <title>Announcing the RabbitMQ extension for Azure Functions</title>
      <dc:creator>Jeff Hollan</dc:creator>
      <pubDate>Thu, 12 Sep 2019 23:06:50 +0000</pubDate>
      <link>https://dev.to/azure/announcing-the-rabbitmq-extension-for-azure-functions-2mbo</link>
      <guid>https://dev.to/azure/announcing-the-rabbitmq-extension-for-azure-functions-2mbo</guid>
      <description>&lt;p&gt;We're excited to announce today the public preview of the &lt;a href="https://github.com/azure/azure-functions-rabbitmq-extension"&gt;RabbitMQ Extension&lt;/a&gt; for the Azure Functions runtime.  With this new extension you can now have functions trigger in response to queue messages, or connect to RabbitMQ for an output binding.  Currently the preview trigger is supported when publishing functions to the &lt;a href="https://docs.microsoft.com/azure/azure-functions/functions-premium-plan"&gt;Functions Premium plan&lt;/a&gt; with runtime driven scale enabled, or a dedicated &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale#app-service-plan"&gt;App Service Plan&lt;/a&gt;.  You can also be able to use RabbitMQ with Azure Functions containers in &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-kubernetes-keda#deploying-a-function-app-to-kubernetes"&gt;Kubernetes alongside KEDA&lt;/a&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  How to get started with the RabbitMQ Trigger
&lt;/h2&gt;

&lt;p&gt;To get started with using the RabbitMQ trigger, you need to include the extension in your function project. &lt;/p&gt;

&lt;h4&gt;
  
  
  .NET Functions
&lt;/h4&gt;

&lt;p&gt;For .NET functions, you can pull in the &lt;a href="https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.RabbitMQ"&gt;RabbitMQ NuGet extension&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Install-Package Microsoft.Azure.WebJobs.Extensions.RabbitMQ -Version 0.2.2029-beta
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  JavaScript, TypeScript, Python, Java, and PowerShell Functions
&lt;/h4&gt;

&lt;p&gt;For other functions, you need to install the extension into your project using the Azure Functions core tools&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func extensions install Microsoft.Azure.WebJobs.Extensions.RabbitMQ -v 0.2.2029-beta
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then create a function that can activate and run whenever a message is dropped in a defined RabbitMQ queue.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example C# Trigger and Output Binding
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;RabbitMQTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"queue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ConnectionStringSetting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"RabbitMqConnection"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;RabbitMQ&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;ConnectionStringSetting&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"RabbitMQConnection"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;QueueName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"downstream"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;outputMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;outputMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"RabittMQ output binding function sent message: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;outputMessage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Example function.json
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"bindings"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rabbitMQTrigger"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"connectionStringSetting"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"RabbitMqConnection"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"queueName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"queue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"deadLetterExchangeName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dtx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"myQueueItem"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;connectionStringSetting&lt;/code&gt; resolves to an application setting (&lt;code&gt;local.settings.json&lt;/code&gt; when developing locally) that should follow a format like: &lt;code&gt;amqp://user:PASSWORD@127.0.0.1:5672&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For instructions on how to use Azure Kubernetes Service to host a RabbitMQ endpoint for testing, you can &lt;a href="https://github.com/Azure/azure-functions-rabbitmq-extension/wiki/Setting-up-a-RabbitMQ-Endpoint"&gt;follow this guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redelivery and Deadletter
&lt;/h2&gt;

&lt;p&gt;Like the other queue-based triggers for Azure Functions, the RabbitMQ trigger supports retries and deadletters.  If an execution results in an exception, the message can be retried.  Once the message has exceeded its redelivery count threshold, we will route it to the dead letter exchange defined, where messages can end up in a poison queue. The extension will automatically append "-poison" to the original queue name when the DLX option is filled.&lt;/p&gt;

&lt;p&gt;You can learn more about &lt;a href="https://github.com/Azure/azure-functions-rabbitmq-extension/wiki/Configuring-a-Dead-Letter-Exchange-and-Queue"&gt;how to configure deadletter and poison queues here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open and Evolving
&lt;/h2&gt;

&lt;p&gt;This is just the initial public preview release.  Like all Azure Functions extensions, this extension is &lt;a href="https://github.com/azure/azure-functions-rabbitmq-extension"&gt;completely open source&lt;/a&gt;.  If you have any issues, feedback, or requests - please file issues on the GitHub repository.  We also welcome any contributions and additions from the community.&lt;/p&gt;

&lt;p&gt;We're excited with this announcement and look forward to even more event sources being compatible with Azure Functions everywhere.&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>opensource</category>
      <category>azure</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Ordered queue processing in Azure Functions with Sessions</title>
      <dc:creator>Jeff Hollan</dc:creator>
      <pubDate>Thu, 30 May 2019 14:40:52 +0000</pubDate>
      <link>https://dev.to/azure/ordered-queue-processing-in-azure-functions-4h6c</link>
      <guid>https://dev.to/azure/ordered-queue-processing-in-azure-functions-4h6c</guid>
      <description>&lt;p&gt;Let's chat about ordering.  It's one of my favorite topics, and something &lt;a href="https://medium.com/@jeffhollan/in-order-event-processing-with-azure-functions-bb661eb55428" rel="noopener noreferrer"&gt;I've blogged about extensively before&lt;/a&gt;.  Previously ordered processing in Azure Functions was only possible with event streams like Azure Event Hubs, but today I want to show how you can preserve order for Service Bus queues and topics as well.  &lt;/p&gt;

&lt;p&gt;On the surface it seems pretty straight-forward: I want to be able to process messages from a queue in the &lt;em&gt;exact&lt;/em&gt; order that I received them.  For a simple service running on a machine, it's pretty easy to achieve.  However, how do I preserve the ordering of queue messages when I want to process at scale?  With something like Azure Functions I may be processing messages across dozens of active instances, how can I preserve ordering?&lt;/p&gt;

&lt;p&gt;Let's use a simple example of a messaging system that deals with patients at a hospital.  Imagine I have a few events for each patient:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Patient arrives&lt;/li&gt;
&lt;li&gt;Patient assigned a room&lt;/li&gt;
&lt;li&gt;Patient receives treatment&lt;/li&gt;
&lt;li&gt;Patient is discharged&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I want to make sure I never process a message out of order and potentially discharge a patient before I've processed their treatment!&lt;/p&gt;

&lt;p&gt;Let's run some quick experiments to see what happens.  For this I'm going to simulate 1000 patients each sending these 4 messages (in order) and processing them (ideally in order as well).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1frj701yey2wb0vyinxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1frj701yey2wb0vyinxx.png" alt="Service Bus logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Default and out of order
&lt;/h2&gt;

&lt;p&gt;Let's try this with a simple Azure Function that just triggers on a queue.  I'm not going to do anything special, just trigger on the queue and push the operation it's processing to a list on Redis Cache.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;ServiceBusTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"queue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ServiceBusConnectionString"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"C# ServiceBus queue trigger function processed message: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PushData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserProperties&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"patientId"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After sending 1000 patients worth of data (4 messages each) to this queue, what does the Redis Cache look like after processing?  Well some of the patients look great.  When I lookup Patient #4 I see:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;lrange Patient-&lt;span class="nv"&gt;$4&lt;/span&gt; 0 &lt;span class="nt"&gt;-1&lt;/span&gt;
1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-0"&lt;/span&gt;
2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-1"&lt;/span&gt;
3&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-2"&lt;/span&gt;
4&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-3"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Great! All 4 events were sent for Patient 4, and got processed in order.  But if I look at patient 2:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;lrange Patient-&lt;span class="nv"&gt;$2&lt;/span&gt; 0 &lt;span class="nt"&gt;-1&lt;/span&gt;
1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-1"&lt;/span&gt;
2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-2"&lt;/span&gt;
3&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-0"&lt;/span&gt;
4&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-3"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this case it didn't finish processing the "patient arrives" message until after 2 other messages had already been processed. So what happened here?  Azure Service Bus does guarantee ordering, so why are my messages out of order?&lt;/p&gt;

&lt;p&gt;Well by default, the queue trigger will do a few things.  First, for every instance that spins up, it will process a set of messages concurrently.  &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus#hostjson-settings" rel="noopener noreferrer"&gt;By default&lt;/a&gt; an instance concurrently processes 32 messages.  That means it may be processing all 4 messages for a patient at the same time and they finish in different order than they were sent.  Well that seems easy enough to fix, let's just limit the concurrency to 1.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Anti-pattern: limit scale out and concurrency
&lt;/h2&gt;

&lt;p&gt;Here's maybe the most common solution to the above problem that I see.  Let's limit the concurrency to only process 1 message at a time instead of 32.  For that I modify my &lt;code&gt;host.json&lt;/code&gt; file and set the &lt;code&gt;maxConcurrentCalls&lt;/code&gt; to 1.  Now each instance will only process 1 message at a time.  I run the same test again.&lt;/p&gt;

&lt;p&gt;First off, it's &lt;em&gt;super slow&lt;/em&gt;.  It takes me a long time to chew through the 4000 queue messages because each instance only processes 1 at a time.  And worse yet? When I check the results afterwards, some of the patients are still out of order!  What's going on here?  Even though I limited the &lt;strong&gt;instance&lt;/strong&gt; concurrency to 1, Azure Functions has scaled me out to multiple instances.  So if I have 20 function app instances that have scaled, I have 20 messages being processed concurrently (1 per instance).  That means I still get into a spot where messages from the same patient could be processed at the same time - just on different instances.  I'm still not guaranteed ordered processing.&lt;/p&gt;

&lt;p&gt;The fix here?  Many people want to limit the scale out of Azure Functions.  While &lt;a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings#website_max_dynamic_application_scale_out" rel="noopener noreferrer"&gt;it's technically possible&lt;/a&gt;, it would hurt my throughput even more.  Now only one message globally could be processed at a time, meaning during high traffic I'm going to get a large backlog of patient events that my function may not be able to keep up with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sessions to the rescue
&lt;/h2&gt;

&lt;p&gt;Wouldn't this be such a sad blog post if I ended it here?  There is a better way!  Previously I would have said your best bet here may be to use Event Hubs which, because of partitions and batches, &lt;a href="https://medium.com/@jeffhollan/in-order-event-processing-with-azure-functions-bb661eb55428" rel="noopener noreferrer"&gt;you can guarantee ordering&lt;/a&gt;.  The challenge here though is that sometimes a queue is the right message broker for the job given its transactional qualities like retries and deadlettering.  And now you can use queues &lt;em&gt;and&lt;/em&gt; get ordering with Service Bus sessions 🎉.&lt;/p&gt;

&lt;p&gt;So what are sessions?  Sessions enable you to set an identifier for a group of messages.  In order to process messages from a session, you first have to "lock" the session.  You can then start to process each message from the session individually (using the same lock / complete semantics of a regular queue).  The benefit of sessions is &lt;strong&gt;it enables you to preserve order even when processing at high scale across multiple instances.&lt;/strong&gt; Think of before where we had something like 20 Azure Function app instances all competing for the same queue.  Rather than not scaling to 20, now all 20 instances each will "lock" its own available session and only process events from that session.  Sessions also ensure that messages from a session are processed in order.&lt;/p&gt;

&lt;p&gt;Sessions can be dynamically created at any time.  An instance of Azure Functions spins up and first asks "are there any messages that have a session ID that hasn't been locked?"  If so, it locks the session and starts processing in order.  When a session no longer has any available messages, Azure Functions will release the lock and move on to the next available session.  No message will be processed without first having to lock the session the message belongs to.&lt;/p&gt;

&lt;p&gt;For our example above, I'm going to send the same 4000 messages (4 patient events for 1000 patients).  In this case, I'm going to set the patient ID as the session ID.  Each Azure Functions instance will acquire a lock on a session (patient), process any messages that are available, and then move on to another patient that has messages available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using sessions in Azure Functions
&lt;/h2&gt;

&lt;p&gt;Sessions are currently available in the &lt;code&gt;Microsoft.Azure.WebJobs.Extensions.ServiceBus&lt;/code&gt; extension using version &amp;gt;= 3.1.0, and at the time of writing this is in preview.  So first I'll pull in the extension.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Microsoft.Azure.WebJobs.Extensions.ServiceBus&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Pre&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And then make the tiniest code change to my function code to enable sessions (&lt;code&gt;isSessionsEnabled = true&lt;/code&gt;):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;ServiceBusTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"queue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Connection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"ServiceBusConnectionString"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IsSessionsEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"C# ServiceBus queue trigger function processed message: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PushData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SessionId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I also need to make sure I'm using a session-enabled queue or topic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65lle7zncxebtphrpwof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65lle7zncxebtphrpwof.png" alt="session enabled queue"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And when I push the messages to the queue, I'll set the right &lt;code&gt;sessionId&lt;/code&gt; for each patient message I send.&lt;/p&gt;

&lt;p&gt;After publishing the function I push the 4000 messages.  The queue gets drained pretty quickly, because I'm able to process multiple sessions concurrently across scaled-out instances.  After running the test I check Redis Cache.  As expected, I see all messages were processed, and for every single patient I see they were processed in order:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;lrange Patient-&lt;span class="nv"&gt;$10&lt;/span&gt; 0 &lt;span class="nt"&gt;-1&lt;/span&gt;
1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-0"&lt;/span&gt;
2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-1"&lt;/span&gt;
3&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-2"&lt;/span&gt;
4&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-3"&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;lrange Patient-&lt;span class="nv"&gt;$872&lt;/span&gt; 0 &lt;span class="nt"&gt;-1&lt;/span&gt;
1&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-0"&lt;/span&gt;
2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-1"&lt;/span&gt;
3&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-2"&lt;/span&gt;
4&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Message-3"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So with the new Azure Functions support for sessions, I can process messages from a Service Bus queue or topic in order without having to sacrifice on overall throughput.  I can dynamically add messages to a new or existing session, and have confidence that messages in a session will be processed in the order they are received by service bus.&lt;/p&gt;

&lt;p&gt;You can see the full sample I used for testing and loading messages in &lt;a href="https://github.com/jeffhollan/functions-csharp-servicebus-sessions" rel="noopener noreferrer"&gt;my GitHub repo&lt;/a&gt;.  The &lt;code&gt;master&lt;/code&gt; branch will be all in order, and the &lt;code&gt;out-of-order&lt;/code&gt; branch is the default and out of order experiment.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>serverless</category>
      <category>architecture</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Using Entity Framework with Azure Functions</title>
      <dc:creator>Jeff Hollan</dc:creator>
      <pubDate>Tue, 28 May 2019 16:41:47 +0000</pubDate>
      <link>https://dev.to/azure/using-entity-framework-with-azure-functions-50aa</link>
      <guid>https://dev.to/azure/using-entity-framework-with-azure-functions-50aa</guid>
      <description>&lt;p&gt;&amp;lt;&amp;lt; Update: The instructions below are still good but I did update my sample to use .NET Core 3.1 here if interested: &lt;a href="https://github.com/jeffhollan/functions-chsarp-entitycore-3" rel="noopener noreferrer"&gt;https://github.com/jeffhollan/functions-chsarp-entitycore-3&lt;/a&gt; &amp;gt;&amp;gt;&lt;/p&gt;

&lt;p&gt;One of the exciting developments from Build this year was support for &lt;a href="https://aka.ms/functions-di-docs" rel="noopener noreferrer"&gt;dependency injection&lt;/a&gt; in Azure Functions.  This means you can register and use your own services as part of functions.  While you've been able to use Entity Framework Core in the past, the pairing with dependency injection makes it a much more natural fit.  Let's create a simple Azure Function that can interact with stateful data using Entity Framework Core.  I'm going to create a very simple API that can get and set blog data in an Azure SQL Database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding entity framework to a function project
&lt;/h2&gt;

&lt;p&gt;First up, I created a brand new v2 (.NET Core 2) Azure Functions project.  I selected the HTTP Trigger template to start with.  In order to use dependency injection I need to verify two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;Microsoft.NET.Sdk.Functions&lt;/code&gt; is at least &lt;code&gt;1.0.28&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Install the &lt;code&gt;Microsoft.Azure.Functions.Extensions&lt;/code&gt; package to provide the API to use dependency injection
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Update-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Microsoft.NET.Sdk.Functions&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Microsoft.Azure.Functions.Extensions&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we're at it, let's install the Entity Framework libraries.  ⚠️  These versions are &lt;em&gt;very&lt;/em&gt; particular with the version of .NET you will be running.  So choose your version of these libraries thoughtfully.  At the time of writing this Azure is running .NET Core 2.2.3 so that's what I'll use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Microsoft.EntityFrameworkCore.SqlServer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Version&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;2.2.3&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also want to take advantage of the design-time migration, so I'll install the tools needed to drive that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Microsoft.EntityFrameworkCore.Design&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Version&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;2.2.3&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Microsoft.EntityFrameworkCore.Tools&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Version&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;2.2.3&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add the entity models
&lt;/h3&gt;

&lt;p&gt;Next, I'll create a very simple &lt;code&gt;DbContext&lt;/code&gt; for the data models I want to interact with.  Let's just stick with the simple Blog and Posts entities.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.EntityFrameworkCore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;functions_csharp_entityframeworkcore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BloggingContext&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DbContext&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;BloggingContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DbContextOptions&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BloggingContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DbSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Blogs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DbSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Posts&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;BlogId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Url&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ICollection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Posts&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;PostId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;BlogId&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Blog&lt;/span&gt; &lt;span class="n"&gt;Blog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Write the function code to inject the entity context
&lt;/h3&gt;

&lt;p&gt;I need to verify that I'm using constructors in my function classes so I can inject in the right dependencies (in this case, the entity framework context).  This means getting rid of static and adding a constructor.  I'll also modify the constructor to accept a &lt;code&gt;BloggingContext&lt;/code&gt; created above (which itself will need a &lt;code&gt;DbContextOptions&amp;lt;BloggingContext&amp;gt;&lt;/code&gt; injected).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Threading.Tasks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.AspNetCore.Mvc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.Azure.WebJobs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.Azure.WebJobs.Extensions.Http&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.AspNetCore.Http&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.Extensions.Logging&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;functions_csharp_entityframeworkcore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HttpTrigger&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;BloggingContext&lt;/span&gt; &lt;span class="n"&gt;_context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;HttpTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BloggingContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_context&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;FunctionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GetPosts"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IActionResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AuthorizationLevel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"get"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"posts"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="n"&gt;HttpRequest&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C# HTTP trigger function processed a request."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Code that returns posts - you can see this code by going&lt;/span&gt;
            &lt;span class="c1"&gt;// to the full sample linked at the bottom&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;OkResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To register services like &lt;code&gt;BloggingContext&lt;/code&gt;, I create a &lt;code&gt;Startup.cs&lt;/code&gt; file in the project and implement the &lt;code&gt;FunctionsStartup&lt;/code&gt; configuration.  Notice I use an attribute to signal to the functions host where it can run my configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.Azure.Functions.Extensions.DependencyInjection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.EntityFrameworkCore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.Extensions.DependencyInjection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;assembly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;FunctionsStartup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;functions_csharp_entityframeworkcore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Startup&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;functions_csharp_entityframeworkcore&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Startup&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;FunctionsStartup&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Configure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IFunctionsHostBuilder&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;SqlConnection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetEnvironmentVariable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SqlConnectionString"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddDbContext&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BloggingContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
                &lt;span class="n"&gt;options&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseSqlServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SqlConnection&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enable design-time DbContext creation
&lt;/h3&gt;

&lt;p&gt;As mentioned, I want to use the &lt;a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation" rel="noopener noreferrer"&gt;design-time DbContext creation&lt;/a&gt;.  Since I'm not using ASP.NET directly here, but implementing the Azure Functions &lt;code&gt;Configure&lt;/code&gt; method, entity framework won't automatically discover the desired &lt;code&gt;DbContext&lt;/code&gt;.  So I'll make sure to implement an &lt;code&gt;IDesignTimeDbContextFactory&lt;/code&gt; to drive the tooling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BloggingContextFactory&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IDesignTimeDbContextFactory&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BloggingContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;BloggingContext&lt;/span&gt; &lt;span class="nf"&gt;CreateDbContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;optionsBuilder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;DbContextOptionsBuilder&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BloggingContext&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;optionsBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseSqlServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Environment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetEnvironmentVariable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SqlConnectionString"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BloggingContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;optionsBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Getting the project .dll in the right spot
&lt;/h3&gt;

&lt;p&gt;There's one last thing I need to do.  When you build an Azure Functions project, &lt;code&gt;Microsoft.NET.Sdk.Functions&lt;/code&gt; does some organizing of the build artifacts to create a valid function project structure.  One of the .dlls it moves to a sub-folder is the project .dll.  Unfortunately, for the design-time tools like entity framework migration, it expects the .dll to be at the root of the build target.  So to make sure both tools are happy, I'll add a quick post-build event to copy the .dll to the root as well.  I added this to my &lt;code&gt;.csproj&lt;/code&gt; file for the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Target&lt;/span&gt; &lt;span class="na"&gt;Name=&lt;/span&gt;&lt;span class="s"&gt;"PostBuild"&lt;/span&gt; &lt;span class="na"&gt;AfterTargets=&lt;/span&gt;&lt;span class="s"&gt;"PostBuildEvent"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;Exec&lt;/span&gt; &lt;span class="na"&gt;Command=&lt;/span&gt;&lt;span class="s"&gt;"copy /Y &amp;amp;quot;$(TargetDir)bin\$(ProjectName).dll&amp;amp;quot; &amp;amp;quot;$(TargetDir)$(ProjectName).dll&amp;amp;quot;"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Target&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding an entity framework migration
&lt;/h2&gt;

&lt;p&gt;I went into the Azure Portal and created a simple Azure SQL database.  I copied the connection string into the &lt;code&gt;local.settings.json&lt;/code&gt; file with a new value for &lt;code&gt;SqlConnectionString&lt;/code&gt;.  You can see in my previous code samples I used that as the environment variable that would have the connection string.  This way I don't have to check it into source control 🥽. &lt;/p&gt;

&lt;p&gt;To make sure the connection string is also available to the CLI tooling for migrations, I'll open my Package Manager Console and set the environment variable before adding a migration and updating the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$&lt;/span&gt;&lt;span class="nn"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;SqlConnectionString&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Server=tcp:mySqlServerStuffxxx"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Add-Migration&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;InitialCreate&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="n"&gt;Update-Database&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When browsing to the SQL database in the Azure Portal, I can see the tables were successfully created!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flvrlryfu5a4xppft8etf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flvrlryfu5a4xppft8etf.png" alt="Post create tables create"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing the function app and publishing
&lt;/h2&gt;

&lt;p&gt;That's all the heavy lifting.  Now I just write a few simple HTTP triggered functions that can return and add data to the &lt;code&gt;BloggerContext&lt;/code&gt; I injected in.  When running the app, I can then call the different &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;PUT&lt;/code&gt; methods to validate data is being persisted and returned correctly.&lt;/p&gt;

&lt;p&gt;After publishing, I just make sure to set the appropriate application setting for &lt;code&gt;SqlConnectionString&lt;/code&gt; with my production SQL connection string, and we're in business!&lt;/p&gt;

&lt;p&gt;You can see the full project code &lt;a href="https://github.com/jeffhollan/functions-csharp-entityframeworkcore" rel="noopener noreferrer"&gt;on my GitHub account&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>azure</category>
      <category>serverless</category>
      <category>entityframework</category>
    </item>
  </channel>
</rss>
