PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/articles/azure-functions/functions-run-local.md

https://gitlab.com/yeah568/azure-content
Markdown | 223 lines | 133 code | 90 blank | 0 comment | 0 complexity | fcf5463c59e1a66b9cf0f9706e6c8ce7 MD5 | raw file
  1. <properties
  2. pageTitle="Develop and run Azure functions locally | Microsoft Azure"
  3. description="Learn how to code and test Azure functions in Visual Studio before running them in Azure App Service."
  4. services="functions"
  5. documentationCenter="na"
  6. authors="tdykstra"
  7. manager="wpickett"
  8. editor=""/>
  9. <tags
  10. ms.service="functions"
  11. ms.workload="na"
  12. ms.tgt_pltfrm="multiple"
  13. ms.devlang="multiple"
  14. ms.topic="article"
  15. ms.date="04/14/2016"
  16. ms.author="tdykstra"/>
  17. # How to code and test Azure functions in Visual Studio
  18. ## Overview
  19. This article explains how to run [Azure Functions](functions-overview.md) locally by downloading the [WebJobs.Script](https://github.com/Azure/azure-webjobs-sdk-script/) GitHub repository and running the Visual Studio solution that it contains.
  20. The runtime for Azure Functions is an implementation of the WebJobs.Script open source project. This project is in turn built on the [Azure WebJobs SDK](../app-service-web/websites-dotnet-webjobs-sdk.md), and both frameworks can run locally. You do need to connect to an Azure storage account, however, because the WebJobs SDK uses storage account features that the storage emulator doesn't support.
  21. Functions are easy to code and test in the Azure portal, but sometimes it's useful to work with them locally before running in Azure. For example, some of the languages that Azure Functions supports are easier to write code for in Visual Studio because it provides [IntelliSense](https://msdn.microsoft.com/library/hcw1s69b.aspx). And while you can debug a function remotely, it may be quicker and easier to debug locally. When you run locally, you can debug and set breakpoints in function code as well as in the WebJobs Script host code.
  22. >[AZURE.NOTE] Azure Functions is currently in preview, and the overall experience including tooling is still under rapid development. The procedures outlined in this article do not reflect the final local development experience, and wed love for you to [provide your feedback](https://feedback.azure.com/forums/355860-azure-functions).
  23. ## Prerequisites
  24. ### An Azure account with a function app
  25. This article assumes that you have worked with [Azure Functions](functions-overview.md) in the portal and are familiar with Azure Functions concepts such as [triggers, bindings, and JobHost](functions-reference.md).
  26. When you run functions locally, you get some output in the console window, but you'll also want to use the dashboard that is hosted by a live function app to view function invocations and logs.
  27. ### Visual Studio 2015 with the latest Azure SDK for .NET
  28. If you don't have Visual Studio 2015, or you don't have the current Azure SDK, [download the Azure SDK for Visual Studio 2015](http://go.microsoft.com/fwlink/?linkid=518003). Visual Studio 2015 is automatically installed with the SDK if you don't already have it.
  29. ### Conditional prerequisites
  30. Some Azure resources and software installations are required only if you plan to run functions that use them, for example:
  31. * Azure resources
  32. * Service Bus
  33. * Easy Tables
  34. * DocumentDB
  35. * Event Hubs
  36. * Notification Hubs
  37. * Compilers and script engines
  38. * F#
  39. * BASH
  40. * Python
  41. * PHP
  42. For details about these requirements, including environment variables that you have to set for them, see the [wiki pages for the WebJobs.Script repository](https://github.com/Azure/azure-webjobs-sdk-script/wiki/home)
  43. If your purpose is to contribute to the WebJobs.SDK project, you need all of the conditional prerequisites to run complete tests.
  44. ## To run locally
  45. 1. [Clone](https://github.com/Azure/azure-webjobs-sdk-script/) or [download](https://github.com/Azure/azure-webjobs-sdk-script/archive/master.zip) the Webjobs.Script repository.
  46. 2. Set environment variables for storage connection strings.
  47. * AzureWebJobsStorage
  48. * AzureWebJobsDashboard
  49. You can get these values from the App Service **Application Settings** portal blade for a function app.
  50. a. On the **Function app** blade, click **Function app settings**.
  51. ![Click Function App Settings](./media/functions-run-local/clickfuncappsettings.png)
  52. b. On the **Function App Settings** blade, click **Go to App Service Settings**.
  53. ![Click App Service Settings](./media/functions-run-local/clickappsvcsettings.png)
  54. c. On the **Settings** blade, click **Application settings**.
  55. ![Click Application Settings](./media/functions-run-local/clickappsettings.png)
  56. d. On the **Application settings** blade, scroll down to the **App settings** section and find the WebJobs SDK settings.
  57. ![WebJobs settings](./media/functions-run-local/wjsettings.png)
  58. e. Set an environment variable with the same name and value as the `AzureWebJobsStorage` app setting.
  59. f. Do the same for the `AzureWebJobsDashboard` app setting.
  60. 2. Create an environment variable named AzureWebJobsServiceBus, and set it to your Service Bus connection string.
  61. This environment variable is required for Service Bus bindings, and we recommend that you set it even if you don't use Service Bus bindings. In some scenarios, you might see exceptions if the Service Bus connection string is not set, regardless of the bindings in use.
  62. 3. Make sure any other environment variables that you need are set. (See preceding [Conditional prerequisites](#conditional-prerequisites) section).
  63. 4. Start Visual Studio, and then open the WebJobs.Script solution.
  64. 6. Set the startup project. If you want to run functions that use HTTP or WebHook triggers, choose **WebJobs.Script.WebHost**; otherwise, choose **WebJobs.Script.Host**.
  65. 4. If your startup project is WebJobs.Script.Host:
  66. a. In **Solution Explorer**, right-click the WebJobs.Script.Host project, and then click **Properties**.
  67. b. In the **Debug** tab of the **Project Properties** window, set **Command line arguments** to `..\..\..\..\sample`.
  68. ![Command line arguments](./media/functions-run-local/cmdlineargs.png)
  69. This is a relative path to the *sample* folder in the repository. The *sample* folder contains a *host.json* file that contains global settings, and a folder for each sample function.
  70. To get started it's easiest to use the *sample* folder that's provided. Later you can add your own functions to the *sample* folder or use any folder that contains a *host.json* and function folders.
  71. 5. If your startup project is WebJobs.Script.WebHost:
  72. a. Set an AzureWebJobsScriptRoot environment variable to the full path to the `sample` folder.
  73. b. Restart Visual Studio to pick up the new environment variable value.
  74. See the [API keys](#api-keys) section for additional information about how to run HTTP trigger functions.
  75. 5. Open the *sample\host.json* file, and add a `functions` property to specify which functions you want to run.
  76. For example, the following JSON will make the WebJobs SDK JobHost look for only two functions.
  77. {
  78. "functions": [ "TimerTrigger-CSharp", "QueueTrigger-CSharp"],
  79. "id": "5a709861cab44e68bfed5d2c2fe7fc0c"
  80. }
  81. When you use your own folder instead of the *sample* folder, include in it only the functions that you want to run. Then you can omit the `functions` property in *host.json*.
  82. 6. Build and run the solution.
  83. The console window shows that the JobHost only finds the functions specified in the `host.json` file.
  84. Found the following functions:
  85. Host.Functions.QueueTrigger-CSharp
  86. Host.Functions.TimerTrigger-CSharp
  87. Job host started
  88. If you're starting the WebHost project, you get a blank browser page because there is no content to serve at the base URL of the project. See the [API keys](#apikeys) section for information about URLs to use for HTTP trigger functions.
  89. ## Viewing function output
  90. Go to the dashboard for your function app to see function invocations and log output for them.
  91. The dashboard is at the following URL:
  92. https://{function app name}.scm.azurewebsites.net/azurejobs/#/functions
  93. The **Functions** page displays a list of functions that have been executed, and a list of function invocations.
  94. ![Invocation Detail](./media/functions-run-local/invocationdetail.png)
  95. Click an invocation to see the **Invocation Details** page, which indicates when the function was triggered, the approximate run time, and successful completion. Click the **Toggle Output** button to see logs written by the function code.
  96. ![Invocation Detail](./media/functions-run-local/invocationdetail.png)
  97. ## <a id="apikeys"></a> API Keys for HTTP triggers
  98. To run an HTTP or WebHook function, you'll need an API key unless you include `"authLevel": "anonymous"` in the *function.json* file.
  99. For example, if the API key is `12345`, you can trigger the *HttpTrigger* function with the following URL when the WebJobs.Script.WebHost project is running.
  100. http://localhost:28549/api/httptrigger?code=12345
  101. (As an alternative, you can put the API key in the `x-functions-key` HTTP header.)
  102. API keys are stored in `.json` files in the [App_Data/secrets](https://github.com/Azure/azure-webjobs-sdk-script/tree/master/src/WebJobs.Script.WebHost/App_Data/secrets) folder in the WebJobs.Script.WebHost project.
  103. ### API keys that apply to all HTTP and WebHook functions
  104. The *host.json* file in the *App_Data/secrets* folder has two keys:
  105. ```json
  106. {
  107. "masterKey": "hyexydhln844f2mb7hyexydhln844f2mb7",
  108. "functionKey": "7hyexydhn844f2mb7hyexydhln844f2mb7"
  109. }
  110. ```
  111. The `functionKey` property stores a key that can be used for any HTTP or WebHook function if no override for that particular function is defined. This feature eliminates the need to always define new API keys for every function you create.
  112. The `masterKey` property stores a key that is useful in some testing scenarios:
  113. * If you call a WebHook function with a master key, the WebJobs SDK bypasses the validation of the WebHook provider's signature.
  114. * If you call an HTTP or WebHook function with a master key, the function is triggered even if it's disabled in the *function.json* file. This is used in the Azure portal to make the **Run** button work even for disabled functions.
  115. ### API keys that apply to individual functions
  116. Files that are named *{function name}.json* contain the API key for a particular function. For example, the following example JSON content in *App_Data/secrets/HttpTrigger.json* sets the API key for the `HttpTrigger` function.
  117. ```json
  118. {
  119. "key": "844f2mdhn844f2mb7hyexydhln844f2mb7"
  120. }
  121. ```
  122. ## Using NuGet package references in functions
  123. Due to the way NuGet references are currently processed, make sure that you "touch" the *project.json* file while the host is running. The host watches for file modifications and initiates a restore when it detects changes. Also, *NuGet.exe* (3.3.0 recommended) must either be in your path or you must have an environment variable named AzureWebJobs_NuGetPath set, with the path to *NuGet.exe*.
  124. ## Troubleshooting
  125. Environment variable changes done while Visual Studio is running aren't picked up automatically. If you added or changed an environment variable after starting Visual Studio, shut down Visual Studio and restart it to make sure it is picking up the current values.
  126. When you're debugging, you might get more information about exceptions by selecting **Common Language Runtime Exceptions** in the **Exception Settings** window (CTRL-ALT-E to open the window).
  127. Another way you might get more exception information while debugging is to set a breakpoint in the `catch` block of the main loop for the script host. You'll find this in the WebJobs.Script project, in *Host/ScriptHostManager.cs*, in the `RunAndBlock` method.
  128. ## Next steps
  129. For more information, see the following resources:
  130. * [Azure Functions developer reference](functions-reference.md)
  131. * [Azure Functions C# developer reference](functions-reference-csharp.md)
  132. * [Azure Functions NodeJS developer reference](functions-reference-node.md)
  133. * [Azure Functions triggers and bindings](functions-triggers-bindings.md)