简体   繁体   English

WebService 不读取 web.config

[英]WebService doesn't read web.config

I've got 3 connectionstrings in web.config, and I used theirs like this:我在 web.config 中有 3 个连接字符串,我像这样使用它们:

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SomeName"].ConnectionString))

Every metgod is called by winforms application.每个metgod 都由winforms 应用程序调用。

One of webmethods doesn't work properly because it reads only one connectionString:其中一个 webmethods 不能正常工作,因为它只读取一个 connectionString:

data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

It's not a connectionString from my web.config.这不是我的 web.config 中的连接字符串。

I invoke every method in the same way.我以相同的方式调用每个方法。

How it's impossible??怎么不可能??

EDITED:编辑:

I use facade: This is structure: WinForms calls WebMethod SaveItem SaveItem calls method on Facade: SaveItemAndDoDatabaseStuff SaveItemAndDoDatabaseStuff does database stuff.我使用外观:这是结构:WinForms 调用 WebMethod SaveItem SaveItem 在外观上调用方法:SaveItemAndDoDatabaseStuff SaveItemAndDoDatabaseStuff 执行数据库内容。

We can't see your web structure, but is it possible that your app isn't configured as an application in IIS, therefore is picking up the master web.config? 我们无法看到您的网络结构,但是您的应用程序可能未在IIS中配置为应用程序,因此正在选择 Web.config? Which would look exactly like that... 哪个看起来完全像......

Go into IIS and ensure it is an application (it may have a cog icon). 进入IIS并确保它是一个应用程序(它可能有一个cog图标)。

It's using the default connection string asp.net has (in the machine.config in the .net installation folders). 它使用asp.net的默认连接字符串(在.net安装文件夹中的machine.config中)。

Do a clear: 明确一点:

<configuration>
  <connectionStrings>
    <clear/>
    ... your connection strings here
  </connectionStrings>
</configuration>

Btw, when you say you are using "SomeName" in the connection string. 顺便说一句,当你说你在连接字符串中使用“SomeName”时。 It isn't surely any random connection string you used, its the default: "LocalSqlServer". 它不是你使用的任何随机连接字符串,它是默认的:“LocalSqlServer”。

You should store the connection-strings in the web.config or app.config in whatever project that you are executing. 您应该在您正在执行的任何项目中将连接字符串存储在web.configapp.config中。

In your case, you should have the connection-strings in your app.config for your winforms application. 在您的情况下,您应该在app.config中为winforms应用程序提供连接字符串。

If your WinForms application talks directly to database X ( not via the web service), then the connection string for database X should be in app.config (in the WinForms project). 如果您的WinForms应用程序直接与数据库X( 而不是通过Web服务)对话,那么数据库X的连接字符串应该在app.config中(在WinForms项目中)。

If your Web Service (as I understand, this includes your facade and your database layer), talks to databases X, Y and Z, then the connection strings for X, Y, and Z need to be in web.config (in the Web Services Project). 如果您的Web服务(据我所知,这包括您的外观和数据库层),与数据库X,Y和Z对话,那么X,Y和Z的连接字符串需要在web.config中(在Web中)服务项目)。

Why not just add a Trace line to print out the configuration file being used just before you ask for the connection string. 为什么不在请求连接字符串之前添加一条Trace行来打印出正在使用的配置文件。 Simply add the following line: 只需添加以下行:

        System.Diagnostics.Trace.WriteLine(
            System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
            "APP_CONFIG");

Set a breakpoint after this and look at the Output window for a line that starts with "APP_CONFIG:". 在此之后设置断点,并查看以“APP_CONFIG:”开头的行的“输出”窗口。 This will give the full path to the configuration file and allow you to determine where it's being loaded. 这将提供配置文件的完整路径,并允许您确定其加载位置。 If you still see a discrepancy between the runtime values and the configuration file then likely something is changing those values at runtime within your application. 如果您仍然发现运行时值与配置文件之间存在差异,则可能会在应用程序中的运行时更改这些值。

This is a late answer, but perhaps worthwhile.这是一个迟到的答案,但也许值得。

I have a web-service project, and I want to run it in two modes.我有一个网络服务项目,我想以两种模式运行它。

One is "local as app" (for testing), and for that, at least in my current system setup, the file where it reads from is Web.config (which it apparently reads in deug mode INSTEAD of Web.Debug.config, at least for database configuration)一个是“本地应用程序”(用于测试),为此,至少在我当前的系统设置中,它读取的文件是 Web.config (它显然在 Web.Debug.config 的调试模式 INSTEAD 中读取,至少对于数据库配置)

So, one replace the attribute section with and copy in the ones you would use in the "Calling project for hte web service).因此,将属性部分替换为并复制您将在“为 hte web 服务调用项目”中使用的部分。

So replace所以更换

<connectionStrings>
  <add name="MyDB" 
    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

with

<appSettings>
        <!-- Database Connection -->
        <add key="AppDatabase" value="RedactedDatabase"/>
        <add key="AppLoginID" value="RedactedLoginID"/>
        <add key="AppLoginPwd" value="RedactedPwd"/>
</appSettings>

The above are found in app.config in the "calling project".... To me, this is more elegant anyway.以上内容在“调用项目”的 app.config 中找到......对我来说,这无论如何都更优雅。

Now it will work in both modes.现在它将在两种模式下工作。

One could use a conditional compile in the C#, but now you just read it in:可以在 C# 中使用条件编译,但现在您只需阅读它:

C# code: C# 代码:

String whichDatabase = System.Configuration.ConfigurationManager.AppSettings["AppDatabase"]; 

   String appUsedID = System.Configuration.ConfigurationManager.AppSettings["AppLoginID"]; 
   String appUsedPwd = System.Configuration.ConfigurationManager.AppSettings["AppLoginPwd"];

The above works in either mode, without conditional compile directives.以上工作在任何一种模式下,没有条件编译指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM