从WebSite web.config共享连接字符串 [英] share connection string from WebSite web.config
问题描述
我已经阅读了有关"ASP.NET配置文件的层次结构和继承"的信息.但是我对WCF不太熟悉...问题是我需要将DB ConnectionString设置从WebSite web.config传递给WebApp项目(WCF)web.config,它们是不同的项目,这可能吗?
第一个web.config(WebSite)
< connectionStrings>
<添加名称="SiteSqlServer" connectionString =数据源=.\ SQLEXPRESS;集成安全性= True;用户实例= True; AttachDBFilename = C:\ MyDocuments \ Projects \ Test \ App_Data \ Test05.mdf;" providerName ="System.Data.SqlClient"; />
</connectionStrings>
第二个web.config(WebAppProject WCF)
< hibernate-configuration xmlns =""urn:nhibernate-configuration -2.2>
< session-factory>
....
......
<属性名称="connection.connection_string_name"> ; SiteSqlServer</property>
</session-factory>
</hibernate-configuration>
上面的那一项不起作用.但如果我添加相同的< connectionString>像在第一个web.config中的标记一样,它工作正常,但这是我要避免的.我希望第二个Web配置从第一个Web配置获取连接字符串.
WebApp(WCF)是否可以使用与我的WebSite配置文件相同的连接?
/>预先感谢您的帮助.
I've already read about "ASP.NET Configuration File Hierarchy and Inheritance" but i'm not too familiar with WCF... the thing is that i need to pass the DB ConnectionString setting from my WebSite web.config to the WebApp project(WCF) web.config, they are different projects, is this possible ?
First web.config (WebSite)
<connectionStrings>
<add name="SiteSqlServer" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;AttachDBFilename=C:\MyDocuments\Projects\Test\App_Data\Test05.mdf;" providerName="System.Data.SqlClient" />
</connectionStrings>
Second web.config (WebAppProject WCF)
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
....
......
<property name="connection.connection_string_name">SiteSqlServer</property>
</session-factory>
</hibernate-configuration>
This one above doesn't work. but if i add the same <connectionString> tag like in the First web.config it works fine but that is what i want to avoid. I want that the second web config get the connectionstring from the first web config.
Is there a way for the WebApp(WCF) use the same connection of my WebSite config file ?
Thanks in advance for any help.
推荐答案
是的,但您不能这样做.这是这种方法.
Yes you can but not this way. This is oe way of doing it.
您将需要在WCF上构建一个函数,并将连接字符串值作为字符串参数传递给该函数.然后,您可以从客户端应用程序调用此WCF服务函数,并传递连接字符串值.
You will need to build a function on WCF to which you pass on the connection string value as a string parameter. You can then call this WCF service function from your client application and pass the connection string value.
例如.在WCF服务中,创建以下功能:
For example. In your WCF service, create the following function:
///-----In WCF Service-----///
Private string strConnString;
public voide setConnectionString (string strConn)
{
strConnString = strConn.
}
///--------End of WCF Service----////
然后在您的客户端应用程序中,调用此函数来设置值.
Then in your client application, call this function to set the value.
///-----In Client Application-------
const string strConn = "your connection string";
wcfClient.setConnectionString (strConn);
///----End of Client Application
如果对您有帮助,请标记为答案和/或有帮助.
Please mark as an answer and/or helpful if this is useful to you in anyway.
谢谢,
穆罕默德·易卜拉欣·莫斯塔法(Mohamed Ibrahim Mostafa)
博客: http://www.mohamedibrahim.net
这篇关于从WebSite web.config共享连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!