running Azure Function in VS and error on reading Application settings - azure

I have a function which read "Application settings" which are manly keys. It works fine in Azure but when I run it locally from VS it through an error:
[27/02/2018 5:39:14 AM] A ScriptHost error has occurred
[27/02/2018 5:39:14 AM] Exception while executing function: GetFile. OOPInteg: Object reference not set to an instance of an object.
[27/02/2018 5:39:14 AM] Exception while executing function: GetFile
[27/02/2018 5:39:14 AM] Exception while executing function: GetFile. OOPInteg: Object reference not set to an instance of an object.
[27/02/2018 5:39:14 AM] Function completed (Failure, Id=463f7a76-b34c-42ba-aa84-ca1b496da288, Duration=61ms)
[27/02/2018 5:39:14 AM]
I have added App.config locally with same keys as well but it is not making a difference. Kindly guide how I can fix it. I just want to test it as it works in Azure. Thanks
EDIT:
I have done as per reply by Tom Sun but result is same. Have copied his json file changed only its 'AzureWebJobsStorage' and 'AzureWebJobsDashboard' but on running got same effect. Please help.

If you want to run the Azure function locally, you could configurate the "Applcation settings" in the local.settings.json.
The file local.settings.json stores app settings, connection strings, and settings for Azure Functions Core Tools. It has the following structure:JSON
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<connection string>",
"AzureWebJobsDashboard": "<connection string>"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
},
"ConnectionStrings": {
"SQLConnectionString": "Value"
}
}
These settings can also be read in your code as environment variables. In C#, use System.Environment.GetEnvironmentVariable or ConfigurationManager.AppSettings. In JavaScript, use process.env. Settings specified as a system environment variable take precedence over values in the local.settings.json file.
Update:
There is no need to provide the cors and sql connectionstring if it is not
necessary and the default local http port is 7071. How to develop and test Azure Functions locally, please refer to this document. How to develop with VS please refer to Azure Functions Tools for Visual Studio.

Related

Azure Durable Function (Node.JS) error: System.Security.Cryptography.CryptographicException : Error occurred while trying to encrypt the provided data

Have an Azure Durable Functions App written in NodeJS. Functions are deploying fine to Azure but unable to debug them locally in VSCode due to the following error. I don't understand why I am getting a .NET core error in my app.
MS_FUNCTION_LOGS 2,,,,ErrorOccuredDuringStartupOperation,Host.Startup,"System.Security.Cryptography.CryptographicException : An error occurred while trying to encrypt the provided data. Refer to the inner exception for more information.
---> System.InvalidOperationException : The key ring does not contain a valid default protection key. The data protection system cannot create a new key because auto-generation of keys is disabled.
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.CreateCacheableKeyRingCore(DateTimeOffset now,IKey keyJustAdded)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.Microsoft.AspNetCore.DataProtection.KeyManagement.Internal.ICacheableKeyRingProvider.GetCacheableKeyRing(DateTimeOffset now)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRingCore(DateTime utcNow,Boolean forceRefresh)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingProvider.GetCurrentKeyRing()
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(Byte[] plaintext) End of inner exception
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Protect(Byte[] plaintext)
at Microsoft.AspNetCore.DataProtection.DataProtectionCommonExtensions.Protect(IDataProtector protector,String plaintext)
at Microsoft.Azure.WebJobs.Script.WebHost.DataProtectionKeyValueConverter.WriteValue(Key key) at D:\a\1\s\src\WebJobs.Script.WebHost\Security\KeyManagement\DataProtectionKeyValueConverter.cs : 59
at Microsoft.Azure.WebJobs.Script.WebHost.KeyValueConverterFactoryExtensions.WriteKey(IKeyValueConverterFactory factory,Key key) at D:\a\1\s\src\WebJobs.Script.WebHost\Security\KeyManagement\KeyValueConverterFactoryExtensions.cs : 23 at Microsoft.Azure.WebJobs.Script.WebHost.SecretManager.CreateKey(String name,String secret) at D:\a\1\s\src\WebJobs.Script.WebHost\Security\KeyManagement\SecretManager.cs : 608
at Microsoft.Azure.WebJobs.Script.WebHost.SecretManager.GenerateKey(String name) at D:\a\1\s\src\WebJobs.Script.WebHost\Security\KeyManagement\SecretManager.cs : 601
at Microsoft.Azure.WebJobs.Script.WebHost.SecretManager.GenerateHostSecrets() at D:\a\1\s\src\WebJobs.Script.WebHost\Security\KeyManagement\SecretManager.cs : 494
at async Microsoft.Azure.WebJobs.Script.WebHost.SecretManager.GetHostSecretsAsync() at D:\a\1\s\src\WebJobs.Script.WebHost\Security\KeyManagement\SecretManager.cs : 97
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.Azure.WebJobs.Script.WebHost.DefaultScriptWebHookProvider.GetOrCreateExtensionKey(String extensionName)
at D:\a\1\s\src\WebJobs.Script.WebHost\WebHooks\DefaultScriptWebHookProvider.cs : 74
This same error is occurring when kicking off debugging on either windows or an ubuntu dev environment.
Azure Functions Core Tools Core Tools Version: 3.0.3904
Function Runtime Version: 3.3.1.0
local.settings.json details:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "node",
"BlobStorageConnection": "UseDevelopmentStorage=true",
"BlobStorageAccountKey": "",
"CONTAINER_NAME": "downloads"
}
}
Is there a way to change some configuration or flat to enable the key generation and auto-refresh of the crypto keys so that the system does this by itself and no need for the developer to interfere?
found a similar question on stack overflow but not sure where to specify storage account details as my storage is set to the local emulator.
Thanks
I upgrade Azure Functions Core Tools version to 4.0.3971.
And also add "AzureWebJobsSecretStorageType": "files" in local.settings.json file. It works for me.

Error while Reading from ServiceBus topic/subscription using Azure funtion

I'm currently developing azure functions (new at it) but I'm getting the below error while trying to read from a topic/subscription. I have no idea what's causing this. Any help would be appreciated.
[20/12/2018 14:22:22] Loaded custom extension: ServiceBusExtensionConfig from 'referenced by: Method='Function.ContentCacheUpdate.ReadNotificationQueue.Run', Parameter='mySbMsg'.'
[20/12/2018 14:22:22] Generating 1 job function(s)
[20/12/2018 14:22:23] Found the following functions:
[20/12/2018 14:22:23] Function.ContentCacheUpdate.ReadNotificationQueue.Run
[20/12/2018 14:22:23]
[20/12/2018 14:22:23] Host initialized (1208ms)
Listening on http://localhost:7071/
Hit CTRL-C to exit...
[20/12/2018 14:22:23] Host started (1682ms)
[20/12/2018 14:22:23] Job host started
[20/12/2018 14:22:23] Host lock lease acquired by instance ID '000000000000000000000000EB6A5850'.
My function looks like this
private const string TopicName = "testtopic";
[FunctionName("Function2")]
public static void Run([ServiceBusTrigger(TopicName, "SubscriptionName", Connection = "MyBindingConnection")]string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
and my local.settings.json file is
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"TopicName": "testtopic",
"SubscriptionName": "testsubscription",
"MyBindingConnection": "Endpoint=sb://test-.xxxxxxxxxxxxxxxxxxxx="
}
}
Thanks
Looks like your Azure Functions Core Tools used by VS is outdated. To fix this,
First, go to VS menus>Tools>Extensions and Updates, find Azure Functions and Web Jobs Tools, update it if it's not the latest(15.10.2046.0 right now). Close all VS instances. Wait for the update to finish(if there is).
Then clean the old tools and templates and use VS to download new tools.
Remove %localappdata%\AzureFunctionsTools and %userprofile%\.templateengine folder.
Reopen VS to create a new Function project, wait at the creation dialog, See Making sure all templates are up to date....
After a while, we can see the tip changes as
Click Refresh to work with the latest template instantly.
After creating a new v2 ServiceBus Topic trigger, change your code as below. Connection looks for value in app settings(local.setting.json) by default, while for others properties, we need to wrap them with percent sign. Check details in doc.
[FunctionName("MyServiceBusTrigger")]
public static void Run([ServiceBusTrigger("%TopicName%", "%SubscriptionName%", Connection = "MyBindingConnection")]string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"TopicName": "testtopic",
"SubscriptionName": "testsubscription",
"MyBindingConnection": "Endpoint=sb://test-.xxxxxxxxxxxxxxxxxxxx="
}
}
Even I did using this way,Can you cross check if there are messages in the subscription.

Starting Azure Service Bus Trigger Function throws InvalidOperationException for "Host not yet started"

I have a v.2 Service Bus Trigger function which, when I attempt to start, throws the following exception:
System.InvalidOperationException
HResult=0x80131509
Message=The host has not yet started.
Source=Microsoft.Azure.WebJobs.Host
StackTrace:
at Microsoft.Azure.WebJobs.JobHost.StopAsync() in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\JobHost.cs:line 121
at Microsoft.Azure.WebJobs.Hosting.JobHostService.StopAsync(CancellationToken cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\JobHostService.cs:line 32
at Microsoft.Extensions.Hosting.Internal.Host.<StopAsync>d__10.MoveNext()
I've searched around but cannot find anyone with a similar issue (and fix). I'm running VS 15.8.7 with all extensions and packages updated.
Here's what my function looks like:
[FunctionName("ServiceBusListenerFunction")]
public static void Run([ServiceBusTrigger("myTopic", "MySubscription", Connection = "MyConnection")]string mySbMsg, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}
And here's my local.settings.json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"MyConnection": "UseDevelopmentStorage=true",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"Host": {
"LocalHttpPort": 7077
}
}
I also tried doing the following in launchSettings.json, but it didn't help:
{
"profiles": {
"MyProject": {
"commandName": "Project",
"executablePath": "C:\\Users\\[myUserName\\AppData\\Roaming\\npm\\node_modules\\azure-functions-core-tools\\bin\\func.dll",
"commandLineArgs": "host start --port 7077"
}
}
}
I have Service Bus Explorer running and have created the above-named topic and subscription on it. The project in which the functions are located is built against .NET Standard 2.0.
Please let me know if you have any suggestions or need additional information.
EDIT: I grabbed the red exception text that appears briefly in the console window before it closes (which happens right before I get the above exception), and it reads:
Host initialized
A host error has occurred
System.Private.Uri: Value cannot be null
Parameter name: uriString
Stopping job host
Searching on this, I found this, but it doesn't seem as though I should have to change the attribute to get this working.
Thanks in advance for any help.
Problem is caused by this setting
"MyConnection": "UseDevelopmentStorage=true"
UseDevelopmentStorage=true represents Storage emulator connection string, for a Service Bus trigger, use Service Bus connection string(same one used in Service Bus Explorer or find it in Azure portal).
Some improvements:
In local.settings.json, LocalHttpPort somehow doesn't work in VS, you can remove it as commandLineArgs in launchSettings.json works as expected.
AzureWebJobsDashboard is not required now, so it can be deleted without special purpose.
In launchSettings.json, remove executablePath which is invalid as well. Usually we don't need this setting as VS use latest CLI by default.
One of the ways, I sorted the issue by removing the connection string from the [ServiceBusTrigger] and inserting it through local.settings.json.
in the function file.
[ServiceBusTrigger("Your-Topics-Name", "SubscriptionName",Connection = "MyServiceBus")]
Inside the local.settings.json.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsMyServiceBus": "your connection string",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
note: connection string name start with the "AzureWebJobs" so you can put the remaining as the name.
In my case, it was just to update the version of Microsoft.Azure.WebJobs.Extensions.ServiceBus from 4.7.x to 5.x.x, and that's it :-)
I had to install Azure Functions Core Tools. It includes a version of the same runtime that powers Azure Functions runtime that you can run on your local development computer. It also provides commands to create functions, connect to Azure, and deploy function projects.
In my case the problem was the Platform target, change it to Any CPU instead of x86
I solve the issue by updating all the packages. I had sold packages that were incompatible with a recent package I installed.

Microsoft.WindowsAzure.Storage: No valid combination of account information found

I am using Azure Functions Preview and want to add a QueueTrigerFunction.
The function is defined as such:
[FunctionName("QueueTrigger")]
public static void Run([QueueTrigger("testqueue1", Connection = "AzureWebJobsStorage")]string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed: {myQueueItem}");
}
with the local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=test1storage;AccountKey=XXXXX==;EndpointSuffix=core.windows.net/;",
"AzureWebJobsDashboard": ""
}
}
When I start up the VS Debugger, I get the following error message:
[8/5/2017 1:07:56 AM] Microsoft.WindowsAzure.Storage: No valid combination of
account information found.
What am I missing? Is there some additional settings in Azure I need to check to make sure this scenario is correctly configured?
What am I missing? Is there some additional settings in Azure I need to check to make sure this scenario is correctly configured?
I can reproduce the issue that you mentioned with you supplied AzureWebJobsStorage connection string format. Please have a try to remove EndpointSuffix=core.windows.net/; from the connection string. Then it works correctly on my side.
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=storageaccountname;AccountKey=xxxxxxxxxxxx",
"AzureWebJobsDashboard": ""
}
}
This worked for me. The connection string in the Azure Portal that i copy/pasted, included 'EndpointSuffix=core.windows.net' at the end of the string. When i used this, i got the same error as above. When i simply removed this part of the connection string i was able to connect successfully.
I had a similar issue where it failed to connect.
I was running the function locally on a unix system and instead of using the local settings I used a straight forward environment variable.
Turned out that when declaring the variable it should be quoted:
export AzureWebJobsStorage="DefaultEndpointsProtocol=https;Accoun..." and that solved the problem, guessing some characters are treated incorrectly otherwise

Azure Functions Precompiled & Blob Trigger : The function type name is invalid

I try to deploy a precompiled Azure Function that use Blob Trigger.
After publishing the function, I have the following error in Kudu and my function is not executed:
2017-05-30T14:34:11.436 Starting Host (HostId=sfl-data-forecast-dev-funcs, Version=1.0.10945.0, ProcessId=17328, Debug=True, Attempt=0)
2017-05-30T14:34:11.436 Development settings applied
2017-05-30T14:34:11.436 No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
2017-05-30T14:34:11.436 Job host started
2017-05-30T14:34:11.436 The following 1 functions are in error:
Import: The function type name 'Forecasts.Functions.ImportForecastsFunction' is invalid.
I do not understand why I have this error. The Azure function is in a web project that targeting framework 4.6.1. WebJob SDK, and Extensions nuget package were added. I have downgraded Newtonsoft.Json to version 9.01 but it didn't change anything.
I have the following function.json:
{
"scriptFile": "..\\bin\\SFL.Data.Forecasts.Functions.dll",
"entryPoint": "SFL.Data.Forecasts.Functions.ImportForecastsFunction.Run",
"bindings": [
{
"name": "file",
"type": "blobTrigger",
"direction": "in",
"path": "forecasts/{name}",
"connection": "HotStorageAccount.ConnectionString"
}
],
"disabled": false
}
Faced the same exception. Turned out that the runtime version was invalid. Erroneously defined as ~1, even though the function is referencing netcore2.1, not supported by runtime Version 1.
In particular, the invalid version was caused by an ARM-template based resource group deployment, defining the function app's parameter FUNCTIONS_EXTENSION_VERSION as ~1 instead of ~2.
I solved the problem by providing a namespace for the Azure Function file.
namespace MyProject.AppFunctions
This is my class:
namespace MyProject.AppFunctions
{
public static class SomeFunction
{
public static async Task<HttpResponseMessage> Run(...)
{
// CODE
}
}
}
This is my functions.json file:
{
"scriptFile": "..\\bin\\MyProject.AppFunctions.dll",
"entryPoint": "MyProject.AppFunctions.SomeFunction.Run",
...
}
FWIW, I just resolved the same issue. The problem was that in my debugger settings, it was pointing to an old version of the func.exe application. I switched my debugger settings to launch %AppData%\npm\func.cmd instead and everything worked fine.
(Solution) Function (FunctionName/CsvUpload) Error: The function type name 'Functions.CsvUpload' is invalid
I have solved this error by setting the value in Azure configuration from ~1 to ~2. Make Sure you have to use Visual studio 2019 and Microsoft.NET.Sdk.Functions 3.0.2 When you are working on dotnet core 3.0 something ! If you are using Visual Studio 2017 you must have to set your sdk version lower than 3.0.2 (Microsoft.NET.Sdk.Functions : 1.0.29 something) then only you can set FUNCTIONS_EXTENSION_VERSION = ~1
If you using Visual Studio 2019 then use FUNCTIONS_EXTENSION_VERSION = ~2
If you using Visual Studio 2017 then use FUNCTIONS_EXTENSION_VERSION = ~1
This also happens when you have multiple Azure Functions Core Tools versions installed at the same time.
I got this error when trying to debug a v4 Azure Function locally. Turns out I had v2 installed. Once I removed v2 things started working again.

Categories

Resources