Reputation: 33998
In my console application I have this on app.config
<appSettings>
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xx/KrM="/>
</appSettings>
The console application does the following:
static void Main(string[] args)
{
try
{
Console.WriteLine("Press key to continue");
Console.ReadKey();
QueueHelper.ReceiveMessage("Empresa");
}
catch (Exception ex)
{
throw ex;
}
}
However I wanted to isolate the Queing methods in a different class library, so the actual implementation of ReceiveMessage is in another class library
/// <summary>
/// Receives a message
/// </summary>
/// <param name="queuName"></param>
public static void ReceiveMessage(string queuName)
{
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "Empresa");
// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);
// Callback to handle received messages
Client.OnMessage((message) =>
{
try
{
Empresa empresa = GetBody<Empresa>(message);
// Process message from queue
//Console.WriteLine("Body: " + );
Console.WriteLine("MessageID: " + message.MessageId);
// Remove message from queue
message.Complete();
}
catch (Exception ex)
{
// Indicates a problem, unlock message in queue
message.Abandon();
}
}, options);
}
The problem is that its trying to get the connection string info from app.config which is local to that library and not to the caller.
So I get a null connection string.
Now, I dont want to duplicate app settings in my class library projects, how and what would be the best way to achieve this?
One of the reasons for this is that at the end this console app will be installed in Azure as a WebJob, and Azure has an interface to change AppSettings for webjobs, but not for class libraries.
Upvotes: 1
Views: 763
Reputation: 3004
Using reflection you might be able to get hold of the application configuration from the calling assembly.
Below is a sample code
public static void ReceiveMessage(string queuName)
{
var assembly = Assembly.GetCallingAssembly();
string configPath = new Uri(assembly.CodeBase).LocalPath;
var configManager = ConfigurationManager.OpenExeConfiguration(configPath);
var connectionString = configManager.ConnectionStrings.CurrentConfiguration.AppSettings.Settings["Microsoft.ServiceBus.ConnectionString"];
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "Empresa");
// Configure the callback options
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);
// Callback to handle received messages
Client.OnMessage((message) =>
{
try
{
Empresa empresa = GetBody(message);
// Process message from queue
//Console.WriteLine("Body: " + );
Console.WriteLine("MessageID: " + message.MessageId);
// Remove message from queue
message.Complete();
}
catch (Exception ex)
{
// Indicates a problem, unlock message in queue
message.Abandon();
}
}, options);
}
The ConfigurationManager
is a part of System.Configuration
Assembly.
If you have an even more complex scenario then please post the details and I will try to figure out the solution.
Upvotes: 1