{"id":2116,"date":"2017-01-06T08:45:02","date_gmt":"2017-01-06T07:45:02","guid":{"rendered":"http:\/\/blog.repsaj.nl\/?p=2116"},"modified":"2017-01-06T09:01:31","modified_gmt":"2017-01-06T08:01:31","slug":"azure-using-precompiled-dlls-for-your-functions","status":"publish","type":"post","link":"http:\/\/blog.repsaj.nl\/index.php\/2017\/01\/azure-using-precompiled-dlls-for-your-functions\/","title":{"rendered":"[Azure] Using precompiled DLL&#8217;s for your Functions"},"content":{"rendered":"<p>One of the cool things about <strong>Azure Functions<\/strong> is that they are very easy to get started. You create a new function, type some code and you&#8217;re off. This is very nice from a getting started point of view, but once you&#8217;re considering to use them for more than just playing around, other things come into play. For instance, you might want to actually test what you&#8217;re doing. You might want to reference projects, you might want to reuse some of the code you (or your company) already has. Now there&#8217;s all kinds of ways of doing this, but just recently the Function teams introduced another very interesting possibility: the use of <strong>precompiled DLL&#8217;s<\/strong>.<!--more--><\/p>\n<p>This means that instead of writing the code in one (or multiple) .csx files, you can use good-old DLL&#8217;s instead. Basically you only need to do two things:<\/p>\n<ol>\n<li>Provide an entry point for the Function to be executed. This is just like the <strong>Run<\/strong> method in a normal .csx file.<\/li>\n<li>Point the <strong>function.json<\/strong> configuration to the DLL which needs to be used. This is done using the\u00a0<strong>scriptFile<\/strong> and\u00a0<strong>entryPoint<\/strong> properties.<\/li>\n<\/ol>\n<p>The function.json will look like this:<\/p>\n<pre class=\"lang:default decode:true\">{\r\n    \"scriptFile\": \"PreCompiledFunctionSample.dll\",\r\n    \"entryPoint\": \"PreCompiledFunctionSample.MyFunction.Run\",\r\n    \"bindings\": [\r\n        {\r\n            \"authLevel\": \"function\",\r\n            \"name\": \"req\",\r\n            \"type\": \"httpTrigger\",\r\n            \"direction\": \"in\"\r\n        },\r\n        {\r\n            \"name\": \"$return\",\r\n            \"type\": \"http\",\r\n            \"direction\": \"out\"\r\n        }\r\n    ],\r\n    \"disabled\": false\r\n}<\/pre>\n<p>And the matching code file would look like this:<\/p>\n<pre class=\"lang:default decode:true \">using System.Net;\r\nusing System.Linq;\r\nusing System.Threading.Tasks;\r\nusing System.Net.Http;\r\n\r\nnamespace PreCompiledFunctionSample\r\n{\r\n    public class MyFunction\r\n    {\r\n        public static async Task&lt;HttpResponseMessage&gt; Run(HttpRequestMessage req)\r\n        {\r\n            \/\/ parse query parameter\r\n            string name = req.GetQueryNameValuePairs()\r\n                .FirstOrDefault(q =&gt; string.Compare(q.Key, \"name\", true) == 0)\r\n                .Value;\r\n\r\n            \/\/ Get request body\r\n            dynamic data = await req.Content.ReadAsAsync&lt;object&gt;();\r\n\r\n            \/\/ Set name to query string or body data\r\n            name = name ?? data?.name;\r\n\r\n            return name == null\r\n                ? req.CreateResponse(HttpStatusCode.BadRequest, \"Please pass a name on the query string or in the request body\")\r\n                : req.CreateResponse(HttpStatusCode.OK, \"Hello \" + name);\r\n        }\r\n    }\r\n}<\/pre>\n<p>I shamelessly copy\/pasted this example from <a href=\"https:\/\/github.com\/Azure\/azure-webjobs-sdk-script\/wiki\/Precompiled-functions\" target=\"_blank\">this article<\/a> on the function <a href=\"https:\/\/github.com\/Azure\/azure-webjobs-sdk-script\/wiki\" target=\"_blank\">GitHub wiki<\/a> since it&#8217;s a nice simple example to start with. The matching GitHub repo is found <a href=\"https:\/\/github.com\/fabiocav\/azfunc-precompiled-sample\" target=\"_blank\">here<\/a>. It does not clearly state where to put the DLL. Don&#8217;t put it in a bin folder like you would with reference files, you can simply store it in the function folder itself.<\/p>\n<p>Apart from the already mentioned benefits, this also means that you can now make a single DLL with multiple Run methods as entry points. Your would then create multiple Functions with a function.json each, all pointing to different entry points in your DLL. Although powerful; this does require some proper planning and architecture, a mistake is easily made!<\/p>\n<p>I would like to add one little caveat: I found out that when running these things locally, you need to be very aware of the version of the functions cli you&#8217;ve got installed. That&#8217;s easily overlooked, just remember that these things are still in development so fixes and new features get pushed very often. That leads to the situation where the bits you&#8217;ve got on your box might not support the latest and greatest. To install the last version of the CLI, run\u00a0<strong>npm install azure-functions-cli<\/strong> <strong>-g<\/strong>.<strong>\u00a0<\/strong>When running\u00a0<strong>func<\/strong>, the output should resemble this:<\/p>\n<p><a href=\"http:\/\/blog.repsaj.nl\/index.php\/2017\/01\/azure-using-precompiled-dlls-for-your-functions\/funcwindow\/\" rel=\"attachment wp-att-2122\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2122\" src=\"http:\/\/blog.repsaj.nl\/wp-content\/uploads\/2017\/01\/funcwindow.png\" alt=\"funcwindow\" width=\"979\" height=\"512\" srcset=\"http:\/\/blog.repsaj.nl\/wp-content\/uploads\/2017\/01\/funcwindow.png 979w, http:\/\/blog.repsaj.nl\/wp-content\/uploads\/2017\/01\/funcwindow-300x157.png 300w, http:\/\/blog.repsaj.nl\/wp-content\/uploads\/2017\/01\/funcwindow-768x402.png 768w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the cool things about Azure Functions is that they are very easy to get started. You create a new function, type some code and you&#8217;re off. This is very nice from a getting started point of view, but once you&#8217;re considering to use them for more than just playing around, other things come<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[34],"tags":[78,168],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3KFR1-y8","_links":{"self":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/2116"}],"collection":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/comments?post=2116"}],"version-history":[{"count":0,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/2116\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/media?parent=2116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/categories?post=2116"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/tags?post=2116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}