c# - how access connectionstring in a web.config in other project from a WCF -
i'm having difficulty access connectionstrings in web.config wcf "web services"
cenario: solution inside visual studio 2010 2 web projects, placement , wcfplacement. web.config has connectionstings inside placement project.
the wcfplacement not have web.config can see in picture, when build it, generates one.
from placement project can access connection strings using configurationmanager.connectionstrings["xx"].connectionstring; - ok.
but if i'm inside service, gives me error when try access, forcing me have connection string declared inside each class want database access, , that's not good.
how can access connectionstrings in th web.config inside placement project wcf ?
i tried:
sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["webv"].connectionstring);
but gives me error: system.nullreferenceexception: object reference not set instance of object. @ wcfplacement.prospect.dologin(string login, string senha)
tried inside class:
system.configuration.configuration rootwebconfig = system.web.configuration.webconfigurationmanager.openwebconfiguration("/placement"); system.configuration.connectionstringsettings connstring; if (rootwebconfig.connectionstrings.connectionstrings.count > 0) { connstring = rootwebconfig.connectionstrings.connectionstrings["northwindconnectionstring"]; if (connstring != null) console.writeline("northwind connection string = \"{0}\"", connstring.connectionstring); else console.writeline("no northwind connection string"); }
but error. error 1 type or namespace name 'configuration' not exist in namespace 'system.web' (are missing assembly reference?)
you have add reference system.configuration namespace.
https://msdn.microsoft.com/en-us/library/system.configuration(v=vs.110).aspx
Comments
Post a Comment