I've downloaded VS2017 Community Edition to try out Azure functions and I can't get it to work. I've searched many post on Stackoverflow and Azure functions Github pages but nowhere there seems to be a complete documentation about anything.
Here's what I have:
Azure function:
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("ngctestqueue", AccessRights.Manage, Connection = "Endpoint=sb://ngcservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SERVICE_BUS_KEY")]string myQueueItem, TraceWriter log)
{
log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
}
}
host.json
{
"disabled": false,
"bindings": [
{
"name": "myQueueItem",
"type": "serviceBusTrigger",
"direction": "in",
"queueName": "ngctestqueue",
"connection": "connection",
"accessRights": "manage"
}
]
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=ngctest2;AccountKey=STORAGE_ACCOUNT_KEY;EndpointSuffix=core.windows.net",
"AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=ngctest2;AccountKey=STORAGE_ACCOUNT_KEY;EndpointSuffix=core.windows.net",
"AzureWebJobsServiceBus": "Endpoint=sb://ngcservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SERVICE_BUS_KEY",
"connection": "Endpoint=sb://ngcservicebus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SERVICE_BUS_KEY"
}
}
When I run the function via F5 locally, I get the following error:
Microsoft.Azure.WebJobs.Host: Error indexing method 'Func
tion1.Run'. Microsoft.Azure.WebJobs.ServiceBus: Microsoft Azure WebJobs SDK Serv
iceBus connection string 'AzureWebJobsEndpoint=sb://ngcservicebus.servicebus.win
dows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SERVICE_BUS_KEY' is missing or empty.
My dev environment is as follows:
1) Windows 8.1 Pro
2) VS2017 Community 15.3.5
3) Azure Functions CLI 1.0.4
Any help would really be appreciated.
Thank you.
Connection property should be set to connection string name, not the value itself. The value will then be read from the config.
[ServiceBusTrigger("ngctestqueue", AccessRights.Manage, Connection = "AzureWebJobsServiceBus")]
You don't need to create host.json in local environment (it will be auto-generated by SDK).
Related
I took care of all the settings described here Azure WebJobs SDK ServiceBus connection string 'AzureWebJobsAzureSBConnection' is missing or empty
Here is my local.settings.json looks like
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"ConnectionStrings": {
"AzureWebJobsAzureSBConnection": "Endpoint=sb://host.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xx"
}
}
Still I get this error.
Here is the function looks like
public static class Function1
{
[FunctionName("ServiceBusQueueTriggerCSharp")]
public static void Run(
[ServiceBusTrigger("queue", Connection = "Endpoint=sb://host.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxx")]
ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message:");
}
}
A host error has occurred during startup operation '952d758d-8e6c-4159-bc78-ee315de1b93a'.
[2021-08-25T12:41:13.453Z] Microsoft.Azure.WebJobs.ServiceBus: Microsoft Azure WebJobs SDK ServiceBus connection string 'Endpoint=sb://host.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;[Hidden Credential]' is missing or empty.
Value cannot be null. (Parameter 'provider')
Thank you Melissa. Posting your suggestion as an answer to help community members.
You can update local.settings.json and place AzureWebJobsServiceBus inside the Values element.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsServiceBus": "Endpoint=sb://host.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
I am trying to use the standard ILogger and make it log to Azure. First I added following to host file:
{
"version": "2.0",
"Logging": {
"ApplicationInsights": {
"LogLevel": {
"Default": "Trace",
"System": "None",
"Microsoft": "None"
}
}
},
"ApplicationInsights": {
"Instrumentationkey": "xx-xx-xx-xx-xx"
}
}
And this is my function:
namespace Jobs
{
public static class ExchangeRates
{
[FunctionName("ExchangeRates")]
public static void Run([TimerTrigger("0 0 0 * * *", RunOnStartup =true)]TimerInfo myTimer, ILogger log)
{
string lol1 = "lol1 text";
string lol2 = "lol2 text";
log.LogWarning("adsad");
log.LogDebug("LogDebug", "asdasd", lol2);
log.LogTrace("LogTrace {lol1}, {lol2}", lol1, lol2);
log.LogInformation("LogInfo {lol1}, {lol2}", lol1, lol2);
log.LogError("LogError");
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
}
But no logging are added. I also tried installing nuget package:
Microsoft.Extensions.Logging.ApplicationInsights
Am I missing something - what does it take to make an function app writing to Application Insights?
I think you're running the azure function locally with application insights, right?
If yes, actually it's not recommended since application insights is integrated with azure function in azure portal.
But for testing only, you can just add this line of setting "APPINSIGHTS_INSTRUMENTATIONKEY": "the key" in local.settings.json(remember to set it as "copy if newer"). The sample settings in local.settings.json looks like below:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxxxx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "the key"
}
}
By the way, I also installed the nuget package Microsoft.Extensions.Logging.ApplicationInsights in my test.
We know the Connection string for event hub can be used from local.setting.json file. So for the same function app in different environments, I can add the event hub connection string setting in Application settings in the azure portal.
As the EventHubTrigger function app also expects event name and consumer group(optional) as attributes parameters, I was wondering how the event hub name and consumer group can be used from app settings?
public static void EventHubTriggerFunc([EventHubTrigger("myeventhubname", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "myconsumergroupname")] EventData myEventHubMessage, DateTime enqueuedTimeUtc, Int64 sequenceNumber, string offset, ILogger log)
{
// Here EventHubConnectionAppSetting is specified in local.setting.json file
//myeventhubname & myconsumergroupname are hard coded string
}
local.settings.Json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"EventHubConnectionAppSetting": "Endpoint=.....",
"EventHubConsumerGroup": "myconsumergroup"
}
}
([EventHubTrigger("%myeventhubname%", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "%myconsumergroupname%")]
Tried #Roman Kiss answer, and applied it to Python Azure Functions and it works.
In function.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"type": "eventHubTrigger",
"name": "events",
"direction": "in",
"eventHubName": "%EVENT_HUB_NAME%",
"connection": "EVENT_HUB_CONN_STR",
"cardinality": "many",
"consumerGroup": "$Default",
"dataType": "binary"
}
]
}
Notice that connection string does not need %
In local.settings.json:
{
...
"Values": {
...
"EVENT_HUB_NAME": "<actual name of event hub>",
"EVENT_HUB_CONN_STR": "Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...",
...
},
}
I user those and run Azure Functions at local environment.
Azure Functions Core Tools (2.0.3)
Function Runtime Version:2.0.12115.0
azurite#2.7.0
I try as Microsoft document says.
Here is functions.json
{
"bindings": [
{
"name": "input",
"type": "httpTrigger",
"direction": "in"
},
{
"tableName": "Person",
"connection": "MyStorageConnectionAppSetting",
"name": "tableBinding",
"type": "table",
"direction": "out"
}
],
"disabled": false
}
Here is local.settings.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"MyStorageConnectionAppSetting": "UseDevelopmentStorage=true"
}
}
Here is index.js
module.exports = function (context) {
context.bindings.tableBinding = [];
for (var i = 1; i < 10; i++) {
context.bindings.tableBinding.push({
PartitionKey: "Test",
RowKey: i.toString(),
Name: "Name " + i
});
}
context.done();
};
Installed extensions with this.
$ func extensions install -p Microsoft.Azure.WebJobs.Extensions.Storage --version 3.0.0
Run functions from mac terminal, Send http request, I got this error.
System.Private.CoreLib: Exception while executing function:
Functions.test. Microsoft.Azure.WebJobs.Host: Error while handling
parameter _binder after function returned:.
Microsoft.Azure.WebJobs.Extensions.Storage: Not Implemented (HTTP
status code 501: . ). Microsoft.WindowsAzure.Storage: Not Implemented.
Error from Table Storage
POST /devstoreaccount1/$batch 501 0.980 ms - 45
Any help?
UseDevelopmentStorage=true represents function is set to use Storage emulator, which provides a local environment that emulates the Azure Storage Blob, Queue and Table services for development purposes. But unfortunately it's only available on OS Windows, so you got Not Implemented on Mac.
Workaround is to use a real world Storage account, follow the tutorial to get the Connection string, if you don't have a Storage account you may have to create one first.
One more suggestion is install latest Azure Functions Core Tools, right now it's 2.1.725.
I am trying to run an azure function locally on my laptop using the Azure Functions Core Tools. Notice that this function is configured as a cosmosDB trigger
I was partially inspired by the instructions in this tutorial
I started by creating a function called MyFirstFunction with the following commands (and inserting the required inputs when prompted):
func init
func start
My generated javascript function is (the same the Azure portal creates for the same kind of template function):
module.exports = function (context, documents) {
if (!!documents && documents.length > 0) {
context.log('Document Id: ', documents[0].id);
}
context.done();
}
My generated function.json is:
{
"bindings":
[
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases"
}
]
}
My generated local.settings.json is
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}
Given this setup I try to run the function by executing:
func host start
Everything runs fine in the console output until the error message:
Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.
What I missing? I was supposed to trigger the function through an http POST to:
http://localhost:{port}/admin/functions/{function_name}
as explained in the tutorial linked above (being this function a cosmosDB trigger) but the function cannot even be loaded after this error.
What am I missing to run a cosmosDB trigger locally?
Many thanks.
The problem is your local.settings.json and function.json lack necessary configuration of cosmosdb connection string.
See cosmosdb trigger document.
function.json
{
"bindings":
[
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",
// Missed in your code
"connectionStringSetting": "CosmosDBConnectionString",
"databaseName": "<Get db name>",
"collectionName": "<Get coll name>",
"createLeaseCollectionIfNotExists": true
}
]
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
//Missed in your code
"CosmosDBConnectionString":"<Get connection string from Azure portal>"
}
}
Note that in latest version of function core tools(2.0.1-beta.31), no need to register CosmosDB extension if you use func new to create CosmosDB Trigger, tools will install it automatically.
After this problem solved, if you met another error about Microsoft.Azure.Documents.ServiceInterop.dll
The listener for function 'Functions.CosmosDb' was unable to start.
The listener for function 'Functions.CosmosDb' was unable to start. System.Private.CoreLib: One or more errors occurred. (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)).
Microsoft.Azure.DocumentDB.Core: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E).
Just install one package to fix(See this issue)
func extensions install -p Microsoft.Azure.DocumentDB.Core -v 2.0.0-preview
Then everything should work.