Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在相同的解决方案中从我的WCF服务访问另一个项目连接字符串_C#_Wcf_Web Config_Connection String - Fatal编程技术网

C# 如何在相同的解决方案中从我的WCF服务访问另一个项目连接字符串

C# 如何在相同的解决方案中从我的WCF服务访问另一个项目连接字符串,c#,wcf,web-config,connection-string,C#,Wcf,Web Config,Connection String,我的应用程序和WCF服务在同一个解决方案中。 我在类(在应用程序中)中编写了一个属性,以从应用程序的Web.Config获取ConnectionString。 现在需要在我的WCF服务中访问相同的连接字符串,但在我的WCF服务中有另一个Web.Config(定义了所有绑定)。但我需要访问应用程序的连接字符串 //This is the connection string of App, where I am retrieving it in a common class. public sta

我的应用程序和WCF服务在同一个解决方案中。 我在类(在应用程序中)中编写了一个属性,以从应用程序的
Web.Config
获取ConnectionString。 现在需要在我的WCF服务中访问相同的连接字符串,但在我的WCF服务中有另一个
Web.Config
(定义了所有绑定)。但我需要访问应用程序的连接字符串

//This is the connection string of App, where I am retrieving it in a common class. 
public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
正确的答案是“不要这样做,将连接字符串添加到edge项目的配置中”

如果你坚持要与魔鬼为伍,请使用类似的方法(根据需要更改):


您可以从另一个
web.config
访问连接字符串。在asp.net项目中,可以在不同的目录中有两个web.config文件

注意:如果连接字符串具有相同的“名称”,则目录中的字符串将覆盖根文件夹中的字符串


命名空间
添加到类中,并按原样访问/读取web.config文件连接字符串。在此之前,请将相同的Web.config文件添加到项目中的其他文件目录。

您可以使用相同的名称访问它。.Net将在启动应用程序域的应用程序的配置文件中查找配置密钥。如果配置密钥不起作用,请添加System.configuration的引用。我也尝试过这样做,但问题是,我不会加载这个类(它有连接字符串),但是我想要这个类中的连接字符串,可能很愚蠢,但是请给我其他的选择。将你的连接字符串添加到一个新类中,调用它,比如MyConfig
var fileMap = new ConfigurationFileMap("configFilePath");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var sectionGroup = configuration.GetSectionGroup("connectionStrings "); // This is the section group name, change to your needs
var section = (ClientSettingsSection)sectionGroup.Sections.Get("MyTarget.Namespace.Properties.Settings"); // This is the section name, change to your needs
var setting = section.Settings.Get("connectionStringKey "); // This is the setting name, change to your needs
return setting.Value.ValueXml.InnerText;