c# - pass value from app.config from one console application to one class library -
in console application have 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 following:
static void main(string[] args) { try { console.writeline("press key continue"); console.readkey(); queuehelper.receivemessage("empresa"); } catch (exception ex) { throw ex; } }
however wanted isolate queing methods in different class library, actual implementation of receivemessage in class library
/// <summary> /// receives message /// </summary> /// <param name="queuname"></param> public static void receivemessage(string queuname) { queueclient client = queueclient.createfromconnectionstring(connectionstring, "empresa"); // configure callback options onmessageoptions options = new onmessageoptions(); options.autocomplete = false; options.autorenewtimeout = timespan.fromminutes(1); // callback handle received messages client.onmessage((message) => { try { empresa empresa = getbody<empresa>(message); // process message queue //console.writeline("body: " + ); console.writeline("messageid: " + message.messageid); // remove message queue message.complete(); } catch (exception ex) { // indicates problem, unlock message in queue message.abandon(); } }, options); }
the problem trying connection string info app.config local library , not caller.
so null connection string.
now, dont want duplicate app settings in class library projects, how , best way achieve this?
one of reasons @ end console app installed in azure webjob, , azure has interface change appsettings webjobs, not class libraries.
using reflection might able hold of application configuration calling assembly.
below 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 callback options onmessageoptions options = new onmessageoptions(); options.autocomplete = false; options.autorenewtimeout = timespan.fromminutes(1); // callback handle received messages client.onmessage((message) => { try { empresa empresa = getbody(message); // process message queue //console.writeline("body: " + ); console.writeline("messageid: " + message.messageid); // remove message queue message.complete(); } catch (exception ex) { // indicates problem, unlock message in queue message.abandon(); } }, options); }
the configurationmanager
part of system.configuration
assembly.
if have more complex scenario please post details , try figure out solution.
Comments
Post a Comment