Blob path name provider for WebJob trigger - azure

I have a following test code that is placed inside a WebJob project. It is triggered after any blob is created (or changed) inside "cBinary/test1/" storage account.
The code works.
public class Triggers
{
public void OnBlobCreated(
[BlobTrigger("cBinary/test1/{name}")] Stream blob,
[Blob("cData/test3/{name}.txt")] out string output)
{
output = DateTime.Now.ToString();
}
}
The question is: how to get rid of ugly hard-coded const string "cBinary/test1/" and ""cData/test3/"?
Hard-coding is one problem, but I need to create and maintain couple of such strings (blob directories) that are created dynamically - depend of supported types. What's more - I need this string value in couple of places, I don't want to duplicate it.
I would like them to be placed in some kind of configuration provider that builds the blob path string depending on some enum, for instance.
How to do it?

You can implement INameResolver to resolve QueueNames and BlobNames dynamically. You can add the logic to resolve the name there. Below is some sample code.
public class BlobNameResolver : INameResolver
{
public string Resolve(string name)
{
if (name == "blobNameKey")
{
//Do whatever you want to do to get the dynamic name
return "the name of the blob container";
}
}
}
And then you need to hook it up in Program.cs
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
//Configure JobHost
var storageConnectionString = "your connection string";
//Hook up the NameResolver
var config = new JobHostConfiguration(storageConnectionString) { NameResolver = new BlobNameResolver() };
config.Queues.BatchSize = 32;
//Pass configuration to JobJost
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
Finally in Functions.cs
public class Functions
{
public async Task ProcessBlob([BlobTrigger("%blobNameKey%")] Stream blob)
{
//Do work here
}
}
There's some more information here.
Hope this helps.

Related

AzurePageable breaks operation c#

I am having difficulties working with azure pageable.
I have these dificulties in several places.......
What happens is, if i interact with an AzurePageable and something goes wrong, the thread just doesnt return ........
For example yesterday I had too many requests to Azure Appconfiguration, the following piece of code would just hang.......
// Get all the settings from Azure app configuration
private static Dictionary<string, string> GetAllXYZSettings(ConfigurationClient client)
{
var settingsSelector = new SettingSelector() { KeyFilter = "xyz:*" };
var settings = client.GetConfigurationSettings(settingsSelector);
Dictionary<string, string> config = new();
foreach (dynamic setting in settings)
{
string settingValue = (string)setting.Value;
if (!string.IsNullOrEmpty(settingValue))
{
config.Add(setting.Key, settingValue);
}
}
return config;
}
I tried several things, wrapped everything in a try catch but my thread would just not return.
How should i read the config so i can do some correct error handeling .....
Same behaviour is also observed when reading the servicebus queues.......
wrapped in try catch, didn't work

Running some startup tasks in Configure of Azure FunctionsStartup

I have a function that is bound to a QueueTrigger. In this function I generate a file and write this to a Blob Storage.
But before writing (uploading) the file I want to make sure that the container exists. Is the Configure method in the startup class that inherits FunctionsStartup the right place? It feels wrong to do it every time the trigger runs, isn't it?
I'm using DI to supply my function class some services.
[FunctionName("MyFunction")]
public async Task Run([QueueTrigger(MyQueueName, Connection = "AzureWebJobsStorage")]
MyObject queueMessage, ILogger log)
{
var bytes = Encoding.UTF8.GetBytes("MyFileContent");
// Check if container exists - but not everytime?
var blobClient = new BlobClient(_settings.ConnectionString, _settings.ContainerName, _settings.FileName);
await using var memoryStream = new MemoryStream(bytes);
await blobClient.UploadAsync(memoryStream, true);
}
using MyApp.FunctionApp;
using MyApp.FunctionApp.Options;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
[assembly: FunctionsStartup(typeof(Startup))]
namespace MyApp.FunctionApp
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
// Some startup tasks here like ensuring existence of a Blob Container?
builder.Services.AddOptions<Storage>().Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection("Storage").Bind(settings);
});
}
}
}
Depending on the frequency of how often you want to check, you could even do something as simple as this:
//shared variable for all instances that run on the same VM
private static bool HaveCheckedBlobContainer = false;
Then, on each invocation:
if (!HaveCheckedBlobContainer)
{
//perform check ...
HaveCheckedBlobContainer = true;
}
I'll generally have an Initialize() method to set up some expensive instances that need to be stored in static member variables. I'll call Initialize() on each invocation, and use a check such as
_someMemberVariable ??= getItFromMyDiContainerOrInstantiateId();
So that it's only executed once, regardless of invocation count.

Expose webjobs functions to dashboard without azure storage

In this question there's an example on how to use a webjob that can perform some background operations without interacting with azure table storage.
I tried to replicate the code in the answer but it's throwing the following error:
' 'Void ScheduleNotifications()' can't be invoked from Azure WebJobs SDK. Is it missing Azure WebJobs SDK attributes? '
In this link they have a similar error and in one of the answers it says that this was fixed in the 0.4.1-beta release. I'm running the 0.5.0-beta release and I'm experiencing the error.
Here's a copy of my code:
class Program
{
static void Main()
{
var config = new JobHostConfiguration(AzureStorageAccount.ConnectionString);
var host = new JobHost(config);
host.Call(typeof(Program).GetMethod("ScheduleNotifications"));
host.RunAndBlock();
}
[NoAutomaticTrigger]
public static void ScheduleNotifications()
{
//Do work
}
}
I want to know if I'm missing something or is this still a bug in the Webjobs SDK.
Update: Per Victor's answer, the Program class has to be public.
Working code:
public class Program
{
static void Main()
{
var config = new JobHostConfiguration(AzureStorageAccount.ConnectionString);
var host = new JobHost(config);
host.Call(typeof(Program).GetMethod("ScheduleNotifications"));
host.RunAndBlock();
}
[NoAutomaticTrigger]
public static void ScheduleNotifications()
{
//Do work
}
}
Unless you use a custom type locator, a function has to satisfy all conditions below:
it has to be public
it has to be static
it has to be non abstract
it has to be in a non abstract class
it has to be in a public class
Your function doesn't meet the last condition. If you make the class public it will work.
Also, if you use webjobs sdk 0.5.0-beta and you run a program with only the code in your example, you will see a message saying that no functions were found.
Came looking for an answer here, but didn't quite find it in the answer above, though everything he said is true. My problem was that I accidentally changed the inbound property names of a Azure web job so that they DIDN'T match the attributes of the object the function was supposed to catch. Duh!
For the concrete example:
my web job was listening for a queue message based on this class:
public class ProcessFileArgs
{
public ProcessFileArgs();
public string DealId { get; set; }
public ProcessFileType DmsFileType { get; set; }
public string Email { get; set; }
public string Filename { get; set; }
}
But my public static async class in the Functions.cs file contained this as a function definition, where the declared parameters didn't match the names within the queue message class for which it was waiting:
public static async Task LogAndLoadFile(
[QueueTrigger(Queues.SomeQueueName)] ProcessFileArgs processFileArgs,
string dealid,
string emailaddress,
string file,
[Blob("{fileFolder}/{Filename}", FileAccess.Read)] Stream input,
TextWriter log,
CancellationToken cancellationToke)
{
So if you run into this problem, check to make sure the parameter and attribute names match.

Why does Azure scm show only one method?

I have this code in my project that I uploaded to Azure Web Job.
class Program {
static void Main(string[] args) {
var host = new JobHost();
host.RunAndBlock();
}
public static async Task AddSomethingAsync([QueueInput("myqueue1")] string _)
{
//....
}
public static async Task UpdateSomethingAsync([QueueInput("myqueue2")] string _)
{
//....
}
}
}
But it shows the first function "AddSomethingAsync" only in azure scm site (https://me.scm.azurewebsites.net/azurejobs/#/jobs/triggered/myjob/runs/201406170707442616)
Is it the known issue or Am I missing something?
Thanks!
Okie. I got it now. I need to go to Functions page to see it. The default shows only the first method.

ServiceName is not changing properly

I have a need to be able to install the same service multiple times on a single machine.
That part I have working! But I also need the ServiceName's to be different. That part is not.
Below is the code within my Installer.cs:
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
public override void Install(IDictionary stateSaver)
{
RetrieveServiceName();
base.Install(stateSaver);
}
public override void Uninstall(IDictionary savedState)
{
RetrieveServiceName();
base.Uninstall(savedState);
}
private void RetrieveServiceName()
{
var serviceName = Context.Parameters["servicename"];
if(!string.IsNullOrEmpty(serviceName))
{
auditStreamServiceInstaller.ServiceName = serviceName;
auditStreamServiceInstaller.DisplayName = serviceName;
}
}
}
and I use the following cmd to install the service
C:\Windows\Microsoft.Net\Framework\v4.0.30319> installutil /servicename="AuditStream-NW" d:AuditStreamService.exe
Now if I look at the installlog :
Affected parameters are:
logtoconsole =
logfile = C:\AuditStreams\NW\AuditStreamService.InstallLog
assemblypath = C:\AuditStreams\NW\AuditStreamService.exe
servicename = AuditStream-NW
This looks correct, but within my OnStart of my service, I have a line that outputs the ServiceName to a personal log file. But it says that the ServiceName is always AuditStreamService
I was hoping to have that say AuditStream-NW in this case...Can anyone see what I've got wrong?
EXTRA:
The reason I want these names different is because each service also creates a MemoryMappedFile, and originally I had it setup so the name of that non-persistant mmf was always "AuditStream-" + HubName(which is determined within the config file), but an outside program now will be monitoring what the service is doing by reading the mmf, but aside from reading the services config file the external application doesnt know the name of the mmf. My goal is to make all the names the same, ServiceName = MMF Name = ServiceDisplayName.
Ok so it turns out that my installation processes was fine, I just simply cannot use the this.ServiceName variable within the OnStart() as it will always return the generic default name and not the name that was chosen during installation. The following code is what I used to acquire my true name:
int myPid = Process.GetCurrentProcess().Id;
var services = ServiceController.GetServices();
foreach (var service in services)
{
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='" + service.ServiceName + "'");
wmiService.Get();
if (Convert.ToInt32(wmiService["ProcessId"]) == myPid)
myServiceName = service.ServiceName;
}

Resources