My old code is like :
public static async Task SendMessageToServiceBus(string topicName, string message, ILogger log)
{
try
{
ServiceBusConnectionStringBuilder sbc = new ServiceBusConnectionStringBuilder(Environment.GetEnvironmentVariable("ConnectionStringSettingName"));
sbc.EntityPath = topicName;
TopicClient tc = new TopicClient(sbc);
byte[] bytes = Encoding.ASCII.GetBytes(message.ToString());
await tc.SendAsync(new Message(bytes));
log.LogInformation($"Successfully published a message to {topicName}");
}
catch (Exception exception)
{
log.LogInformation("Error: " + exception.ToString());
}
}
Instead if creating ServiceBusConnectionStringBuilder sbc
object
every time I want to use it as a static and want to reset it on every
call.
public static class testData
{
public static ServiceBusConnectionStringBuilder sbc = new ServiceBusConnectionStringBuilder(Environment.GetEnvironmentVariable("ConnectionStringSettingName"));
[FunctionName("test")]
public static async System.Threading.Tasks.Task RunAsync([ServiceBusTrigger("topic", "sub", Connection = "ConnectionStringSettingName")]string mySbMsg, ILogger log)
{
await SendMessageToServiceBus("topicname", mySbMsg, log);
And inside method SendMessageToServiceBus
reset sbc
like:
testData.sbc.Clear();
But this is not working for me getting error :
A namespace cannot directly contain member or field