在一次开发过程中发现这样的问题:
我引用外部的webservice连接并不是在solution里面的web层,而是在solution里面的类库中引用的,原本在web中引用自动生成的引用路径会在web.config里面生成,例如:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<appSettings>
<add key="BidSer.Service" value="http://192.168.1.16:8083/Service.asmx"/>
</appSettings>
但是如果是在类库里面引用的话,默认就不会在web.config里面出现key值了,会在类库里面自动生成app.config,生成如下的配制信息:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="COM365.BLL.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<COM365.BLL.Properties.Settings>
<setting name="COM365_BLL_BidSer_Service" serializeAs="String">
<value>http://192.168.1.16:8083/Service.asmx</value>
</setting>
</COM365.BLL.Properties.Settings>
</applicationSettings>
</configuration>
本来我想,在发布网站之后app.config应该会部署出来吧,否则怎么在部署之后更改webservice的引用呢?可是在部署之后app.config找不到了,难道是把app.config里面的值封装到bin里面去了吗?有点匪夷所思,如果封装进去的话,那何必生成一个app.config给咱们用哦。仔细查找在引用webservice之后生成的东西。
第一,在类库里面会自动添加一个properties文件夹
![](https://www.cnblogs.com/images/cnblogs_com/sherrys/properties.GIF)
里面会通过代码生成器生成两个配制文件,不能手动更改的。
第二个生成的就是app.config文件了。
第三个会在类库的跟目录下面生成一个Setting的类,这是一个密封类,并且是访问的权限是internal的。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace COM365.BLL.Properties
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// This class allows you to handle specific events on the settings class:
// The SettingChanging event is raised before a setting's value is changed.
// The PropertyChanged event is raised after a setting's value is changed.
// The SettingsLoaded event is raised after the setting values are loaded.
// The SettingsSaving event is raised before the setting values are saved.
internal sealed partial class Settings
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Settings()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// // To add event handlers for saving and changing settings, uncomment the lines below:
//
// this.SettingChanging += this.SettingChangingEventHandler;
//
// this.SettingsSaving += this.SettingsSavingEventHandler;
//
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Add code to handle the SettingChangingEvent event here.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Add code to handle the SettingsSaving event here.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这里面能做什么呢,通过这个类就可以来更改app.config里面的元素值了。因为它是一个inernal的,所以在别的类库和web层里面无法直接使用这个类,所以我在此引用webservice的类库里面做了一个Config来封装它来达到可以在外部更新值的效果。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
namespace COM365.BLL
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class Config
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 根据 Web.Config 配制 BidSer.Service 的路径
/// </summary>
public static void SetBidSerUrl()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
COM365.BLL.Properties.Settings.Default.Properties["COM365_BLL_BidSer_Service"].DefaultValue = ConfigurationManager.AppSettings["BidSer.Service"];
COM365.BLL.Properties.Settings.Default.Save();
COM365.BLL.Properties.Settings.Default.Reload();
}
}
}
第一步是通过web.config里面的值来更新app.config里面的值,因为Setting.Default是只读的,所以只能通过Properties来更新了。
第二步和第三步需要放一起的,否则更新不会有效果的。
如果跟我一样是通过读取web.config里面的值来更新app.config里面的值的话,就可以把这函数放到Golable里面,这样的效果就是每次web.config被更改或者服务器重起的话都会自动更新到app.config里面了。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Application Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<script RunAt="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Application_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在应用程序启动时运行的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CommConfig.OnApplicationStart(Server.MapPath(Context.Request.ApplicationPath));
COM365.BLL.Config.SetBidSerUrl();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Application_End(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在应用程序关闭时运行的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Application_Error(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在出现未处理的错误时运行的代码
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Session_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在新会话启动时运行的代码
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void Session_End(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)