I'm trying to add some custom binding using my app settings for my Azure Function. I need to receive only string a string from my settings.
I would like to get simpleValue from my settings.
{
"bindings": [
{
"name": "someValue",
"type": "stringSetting",
"connection": "simpleValue",
"direction": "in"
}
],
"disabled": false
}
and the get it in Run method:
static void GetOrders(TraceWriter log, string someValue)
{
log.Info(someValue);
}
Is it even possible. Maybe there is other way to do it?
I already found the solution. Just add:
using System.Configuration;
and add this line to code with the key ("simpleValue") value:
ConfigurationManager.AppSettings["simpleValue"]
App Settings configurations can be referred in binding json as %MY_CUSTOM_CONFIG% - enclosed within percent symbols.
Note that the connection property of triggers and bindings is a
special case and automatically resolves values as app settings,
without percent signs.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings
Related
I have got a local.settings.json like this
{
"IsEncrypted": false,
"ServiceBusSettings": {
"TransformedJson": {
"ConnectionString": "connectionstring2"
},
"ServiceLogs": {
"ConnectionString": "connectionstring3"
}
},
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
First of all is it acceptable to have nested JSON in Azure Functions Local.settings.json? I have that confusion because in Azure we dont have a json file instead we have environment variables as key val pair. (Please correct I am wrong)
If above json is acceptable, how can I get a specific key from the nested settings
For getting IsEncryped I can just use
bool IsEncryped= configuration.GetValue<bool>("IsEncrypted")
Also in function parameter if I need to use value I can Just use
%IsEncrypted%
In my case if I need to get the connection string under TransformedJson attribute and get its values and also I'd like to use that in my parameter with percentage notation.
Please suggest how can I accomplish that?
I have this array in my appsettings.json:
"ServiceDefinitions": [
{
"Name": "encryption-api",
"Url": "http://localhost:5032",
"ApiKey": "",
"ExposedEndpoints": [
"encrypt",
"decrypt"
]
}
],
I want to be able to override this with an environment variable. I've added the env vars to the configuration builder like this, which seems to work for other values:
builder.Configuration.AddEnvironmentVariables();
And then I've attempted to set this in my Docker Compose file with:
environment:
- ASPNETCORE_URLS=http://gateway:8080/
- ServiceDefinitions="{\"Name\":\"encryption-api\",\"Url\":\"http://encryption-api:80\",\"ApiKey\":\"\",\"ExposedEndpoints\":[\"encrypt\",\"decrypt\"]}"
However, it's still picking up the value from the json file rather than the env var. I've also tried setting it inside [] but that makes no difference.
How can I do this?
Microsoft docs regarding app configuration.
You are going to want to make sure that your Json provider is registered before your environment variables provider. Then you'll want to setup your environment variables in your Docker file as follows:
environment:
- ASPNETCORE_URLS=http://gateway:8080/
- ServiceDefinitions__0__Name="encryption-api"
- ServiceDefinitions__0__Url="http://encryption-api:80"
- ServiceDefinitions__0__ApiKey=""
- ServiceDefinitions__0__ExposedEndpoints__0="encrypt"
- ServiceDefinitions__0__ExposedEndpoints__1="decrypt"
when i am trying to replace string value in Azure logic app
it throwing error that you can't give self reference of variable
"Set_variable": {
"inputs": {
"name": "Images",
"value": "#replace(variables('Images'), 'cdn.gomasterkey.com/images/watermark.aspx?imageurl=/uf/', '~~')"
},
"runAfter": {
"Append_to_array_variable": [
"Succeeded"
]
},
"type": "SetVariable"
}
when i save above code i got this error it doesn't allow me to give self reference although i wanna replace from same variable and again put into it.
You could do self reference in logic app, however you could use workflow functions to get the value, then replace it with the string you want.
I use actions('Initialize_variable').inputs.variables[0].value to get the variable.
What does actually Using do in Serilog JSON configuration (e.g. in AppSettings.json file in .Net Core environment)?
Let us take this configuration for example:
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ], <=======***HERE***=========
"MinimumLevel": "Debug",
"WriteTo": [
{ "Name": "Console" },
{
"Name": "RollingFile",
"Args": {
"pathFormat": "logs\\log-{Date}.txt",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "My Application"
}
}
In the example above we used File sink WITHOUT adding it to Using attribute. However, everything seems to be working ok.
So I cannot understand exactly why we basically need this Using. Could someone explain it to me please?
This is covered in the documentation for Serilog.Settings.Configuration:
(This package implements a convention using DependencyContext to find any package with Serilog anywhere in the name and pulls configuration methods from it, so the Using example above is redundant.)
This means that it's used to define which packages are used for locating Serilog sinks, but it's redundant when using the Serilog.Settings.Configuration package.
More Information
I've had a look at the Serilog source code in order to be able to provide more information on exactly what Using does and why it might be needed in the first place. I hope the following explanation is helpful.
Consider the following code-based setup:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
In this example, Console is an extension method for LoggerSinkConfiguration (and so it takes, as its first parameter, an instance of LoggerSinkConfiguration). When using this code-based approach, the code will only compile if this extension method can be found within a referenced assembly.
Next, consider the following approach, which uses the IConfiguration-based approach:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(someConfiguration)
.CreateLogger();
{
"Serilog": {
"Using": ["Serilog.Sinks.Console"], // Redundant.
"WriteTo": ["Console"]
}
}
In this example, the compilation process has no knowledge of what the JSON string value "Console" refers to and so there's a need for a process that can go from the string "Console" to the Console() extension method mentioned above. In order to do that, Serilog needs to first find the extension method at runtime (which, in this example, lives in the Serilog.Sinks.Console assembly).
This finding process is done using reflection, which does a bit of assembly scanning to find public static methods that take as their first parameter a LoggerSinkConfiguration. The Using directive you've asked about in your question is a mechanism for helping determine exactly which assemblies should be scanned when looking for these extension methods.
As the documentation states, the IConfiguration-based approach uses DependencyContext in order to automatically scan assemblies that have Serilog in their name. Because Serilog.Sinks.Console does have Serilog in its name, there's no need to add this to the Using directive. You also have the option to provide your own DependencyContext instance when using this approach and so you might then need to be explicit about which assemblies to scan when looking for sinks.
I'd like to create an Azure Function that is triggered when a new message is added to a topic/subscription.
For the moment I've created an Azure Function using the ServiceBusQueueTrigger C# Template and I've set the Queue Name to
topicPath + "/Subscriptions/" + subscriptionName
But I've got this exception:
Microsoft.ServiceBus: Cannot get entity 'topic-test/Subscriptions/subscription-test' because it is not of type QueueDescription. Check that you are using method(s) with the correct entity type. System.Runtime.Serialization: Error in line 1 position 1762. Expecting element 'QueueDescription' from namespace 'http://schemas.microsoft.com/netservices/2010/10/servicebus/connect'.. Encountered 'None' with name '', namespace ''. .
I thought the Azure Function was using the MessagingFactory.CreateMessageReceiver to initialize the message pump but not.
Is there any support for topic/subscription for the moment ?
Yes topics are supported, but our UI and templates are behind on that unfortunately - we'll be releasing some updates soon addressing these issues.
For now, you can use the Advanced Editor to edit your trigger binding directly. There you can specify your subscriptionName and topicName values. Here's an example:
{
"bindings": [
{
"type": "serviceBusTrigger",
"name": "message",
"direction": "in",
"subscriptionName": "subscription-test",
"topicName": "topic-test",
}
]
}
In general, since Azure Functions is build atop the WebJobs SDK, our various bindings are mapped directly to their SDK counterparts. For example serviceBusTrigger maps to ServiceBusTriggerAttribute which has SubscriptionName/TopicName properties. Therefore, expect to see the same properties in the Function metadata model.