Setting a session id when pushing to an Azure Service Bus Queue in a csx file

1k views Asked by At

We have build a system in Azure and found that a Http triggered C# project dies if you call it 300 times in a short time periond. CSX files seem to not have this limitation. So it's pretty trivial to create a csx that receives a string via a POST and puts it on a queue. Sadly, all our queues have sessions. So I am going in circles trying to find a way to create a message with a session id. The docs say something like 'bind to a message type to set session id', but I can't set an out parameter in an async method, nor can I find any sort of example for what I am trying to do.

Is there any sort of sample code anywhere for creating a csx file that pushes to an Azure Service Bus queue with a session id?

Thanks

1

There are 1 answers

0
cgraus On

OK, so this is poorly documented and hard to track down. If you create a function that's using csx, the FUNCTIONS_EXTENSION_VERSION is set to 1. You need to set it to 2 or 3 in order to set session ids on functions. Then you need to upload a function.proj file. I found the file you upload is empty, you're just setting a name. Then you need to paste this in

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.1.0" />
    </ItemGroup>
</Project>

Once you do this, you will be able to access the DLL you need, like this:

#r "bin\\Microsoft.Azure.ServiceBus"

using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Queue;

Now you can create a trigger to a service bus and, if you make your method not async, you can set a Message as an out param. The docs say if you don't set a value to this property no message is sent, but of course it's an out param, so you have to.

You can pass a byte array to the constructor for the body of your message, like this:

new Message(Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(requestBody)));

Your message has a SessionId property. That's it. Set the SessionId and you are done.