0

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

xdtTransform
  • 1,986
  • 14
  • 34
Neo
  • 15,491
  • 59
  • 215
  • 405
  • Which line is the error on? It looks like it's possibly not on one of the lines you've shown. – iakobski Jan 24 '20 at 07:51
  • Is it possible that you could edit your post to separate your code examples from your text. – Simon Jensen Jan 24 '20 at 08:12
  • ok let me update question again – Neo Jan 24 '20 at 09:35
  • The error say namespace. I don't the code outside of a class here your sample https://stackoverflow.com/questions/21175781/a-namespace-cannot-directly-contain-members-such-as-fields-or-methods-in-net-r – xdtTransform Jan 24 '20 at 09:44
  • Does this answer your question? ["A namespace cannot directly contain members such as fields or methods" in Net.Reflector](https://stackoverflow.com/questions/21175781/a-namespace-cannot-directly-contain-members-such-as-fields-or-methods-in-net-r) – xdtTransform Jan 24 '20 at 09:45

0 Answers0