在Web服务中使用ConfigurationManager.GetSection("AppConfig"). [英] using ConfigurationManager.GetSection("AppConfig") in web services.

查看:87
本文介绍了在Web服务中使用ConfigurationManager.GetSection("AppConfig").的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对Web服务和配置文件有疑问.

平台:
.Net Framework,C#背后的代码.

场景:
我有一个Web服务,当我发布该服务时,会创建一个bin文件夹.一切正常.现在,在该bin文件夹中,我有一个dll,它实际上是我的数据访问层,用于数据库的CRUD等.现在,我在此dll中使用ConfigurationManager.GetSection("AppConfig")以获取该DLL带来的数据库连接字符串.从Web服务的web.config文件中.

文件结构:

<我的Web服务文件夹>
->< AppData>
->< bin>
-><我的数据访问层DLL>
->< web.config(包括数据访问层变量)>

在我的开发环境中,使用dll中的ConfigurationManager.GetSection("AppConfig")从我的web.config中取出appconfig部分,然后搜索节点以找到正确的密钥,这就像一个魅力. >
web.config的示例:

< AppConfig>
<配置use ="true">
< add key ="Databases" use ="true"/>
</Configs>

<!-Web服务配置设置->
<数据库>
< ConnectionStrings>
<添加密钥=<我的密钥名称>" providerName ="System.Data.SqlClient"
value =< MY CONNECTION STRING>" />
</ConnectionStrings>
</数据库>
</AppConfig>

到目前为止一切都很好.

现在,我正在虚拟PC上测试此系统的部署.我复制并粘贴c:\ initpub \< mywebservice>文件夹放入虚拟PC的c:\ inetpub.我尝试运行它,但失败了.

在将调试插入代码中以查看失败的地方之后,我发现ConfigurationManager.GetSection("AppConfig")实际上返回的是null,而不是从web.config返回的appconfig部分.有人知道为什么会这样吗?

请原谅帖子的长度,但需要说明情况.

James

Hi all,

I have a question regarding web services and configuration files.

Platform:
.Net Framework , code behind c#.

Scenario:
I have a web service which when i publish, creates a bin folder. All normal. Now, in that bin folder i have a dll which is effectively my data access layer, CRUD for databases etc. Now i use ConfigurationManager.GetSection("AppConfig") within this dll in order to get database connection strings, which the dll brings out from the web service web.config file.

File Structure:

<My Web Service Folder>
--><AppData>
--><bin>
--><My Data Access Layer DLL>
--><web.config(includes data access layer variables)>

Within my development environment, this works like a charm, using ConfigurationManager.GetSection("AppConfig") from my dll to get out the appconfig section from my web.config and then searching the nodes in order to find the right key.

Example of web.config:

<AppConfig>
<Configs use="true">
<add key="Databases" use="true" />
</Configs>

<!-- Web Service Configuration Settings-->
<Databases>
<ConnectionStrings>
<add key="<MY KEY NAME>" providerName="System.Data.SqlClient"
value="<MY CONNECTION STRING>" />
</ConnectionStrings>
</Databases>
</AppConfig>

So far so good.

Now i am testing a deployment of this system on a virtual pc. I copy and paste the c:\initpub\<mywebservice> folder into the c:\inetpub of the virtual pc. I try and run it and it fails.

After inserting debugging into the code to see where it is failing, I find that ConfigurationManager.GetSection("AppConfig") is actually returning null instead of the appconfig section from the web.config. Does anybody know why this is happening ?

Please excuse the length of the post but was needed to explain the situation.

James

推荐答案

除了访问该部分并尝试解析该部分的连接字符串外,您还可以尝试:

Instead of accessing the section and trying to parse the connection sting from that section, can you please try:

string connString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;



同时,您还可以检查在事件查看器"中看到的内容是否有错误. (就像没有找到访问权限或标记一样)



Meanwhile you can also check what you see in ''Event Viewer'' for if there was an error. (Like access permission or tag not found or anything else)


问题是我创建了一个自定义类来处理配置,并且需要按所述方式进行访问.如果您愿意,我可以向您发送代码文件,但是大约有5个文件
the thing is that i have created a custom class to handle the configuration and need to access it as stated. I could send you the code files if you want, but there are about 5 files


这篇关于在Web服务中使用ConfigurationManager.GetSection("AppConfig").的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆