在web项目中的类库中引用webservice 在部署后更改webservice路径的方法

在一次开发过程中发现这样的问题:
我引用外部的webservice连接并不是在solution里面的web层,而是在solution里面的类库中引用的,原本在web中引用自动生成的引用路径会在web.config里面生成,例如:

ContractedBlock.gif ExpandedBlockStart.gif
None.gif<appSettings>
None.gif        
<add key="BidSer.Service" value="http://192.168.1.16:8083/Service.asmx"/>
None.gif
</appSettings>
但是如果是在类库里面引用的话,默认就不会在web.config里面出现key值了,会在类库里面自动生成app.config,生成如下的配制信息:
ContractedBlock.gif ExpandedBlockStart.gif
None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif
<configuration>
None.gif    
<configSections>
None.gif  
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
None.gif   
<section name="COM365.BLL.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
None.gif  
</sectionGroup>
None.gif 
</configSections>
None.gif 
<applicationSettings>
None.gif  
<COM365.BLL.Properties.Settings>
None.gif   
<setting name="COM365_BLL_BidSer_Service" serializeAs="String">
None.gif    
<value>http://192.168.1.16:8083/Service.asmx</value>
None.gif
   </setting>
None.gif  
</COM365.BLL.Properties.Settings>
None.gif 
</applicationSettings>
None.gif
</configuration>
本来我想,在发布网站之后app.config应该会部署出来吧,否则怎么在部署之后更改webservice的引用呢?可是在部署之后app.config找不到了,难道是把app.config里面的值封装到bin里面去了吗?有点匪夷所思,如果封装进去的话,那何必生成一个app.config给咱们用哦。仔细查找在引用webservice之后生成的东西。
第一,在类库里面会自动添加一个properties文件夹

里面会通过代码生成器生成两个配制文件,不能手动更改的。
第二个生成的就是app.config文件了。
第三个会在类库的跟目录下面生成一个Setting的类,这是一个密封类,并且是访问的权限是internal的。
ContractedBlock.gif ExpandedBlockStart.gif
None.gifnamespace COM365.BLL.Properties
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
InBlock.gif    
// This class allows you to handle specific events on the settings class:
InBlock.gif    
//  The SettingChanging event is raised before a setting's value is changed.
InBlock.gif    
//  The PropertyChanged event is raised after a setting's value is changed.
InBlock.gif    
//  The SettingsLoaded event is raised after the setting values are loaded.
InBlock.gif    
//  The SettingsSaving event is raised before the setting values are saved.
InBlock.gif
    internal sealed partial class Settings
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
public Settings()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// // To add event handlers for saving and changing settings, uncomment the lines below:
InBlock.gif            
//
InBlock.gif            
// this.SettingChanging += this.SettingChangingEventHandler;
InBlock.gif            
//
InBlock.gif            
// this.SettingsSaving += this.SettingsSavingEventHandler;
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Add code to handle the SettingChangingEvent event here.
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Add code to handle the SettingsSaving event here.
InBlock.gif

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
这里面能做什么呢,通过这个类就可以来更改app.config里面的元素值了。因为它是一个inernal的,所以在别的类库和web层里面无法直接使用这个类,所以我在此引用webservice的类库里面做了一个Config来封装它来达到可以在外部更新值的效果。
ContractedBlock.gif ExpandedBlockStart.gif
None.gifnamespace COM365.BLL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Config
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据 Web.Config 配制 BidSer.Service 的路径
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void SetBidSerUrl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            COM365.BLL.Properties.Settings.Default.Properties[
"COM365_BLL_BidSer_Service"].DefaultValue = ConfigurationManager.AppSettings["BidSer.Service"];
InBlock.gif            COM365.BLL.Properties.Settings.Default.Save();
InBlock.gif            COM365.BLL.Properties.Settings.Default.Reload();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
第一步是通过web.config里面的值来更新app.config里面的值,因为Setting.Default是只读的,所以只能通过Properties来更新了。
第二步和第三步需要放一起的,否则更新不会有效果的。
如果跟我一样是通过读取web.config里面的值来更新app.config里面的值的话,就可以把这函数放到Golable里面,这样的效果就是每次web.config被更改或者服务器重起的话都会自动更新到app.config里面了。

ContractedBlock.gif ExpandedBlockStart.gif
None.gif<%@ Application Language="C#" %>
None.gif
None.gif
<script RunAt="server">
None.gif
None.gif    
void Application_Start(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
// 在应用程序启动时运行的代码
InBlock.gif

InBlock.gif        CommConfig.OnApplicationStart(Server.MapPath(Context.Request.ApplicationPath));
InBlock.gif        COM365.BLL.Config.SetBidSerUrl();
ExpandedBlockEnd.gif    }

None.gif
None.gif    
void Application_End(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
//  在应用程序关闭时运行的代码
InBlock.gif

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif    
void Application_Error(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
// 在出现未处理的错误时运行的代码
InBlock.gif

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif    
void Session_Start(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
// 在新会话启动时运行的代码               
ExpandedBlockEnd.gif
    }

None.gif
None.gif    
void Session_End(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
// 在会话结束时运行的代码。 
InBlock.gif        
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
InBlock.gif
InBlock.gif        
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
InBlock.gif        
// 或 SQLServer,则不会引发该事件。
InBlock.gif

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif       
None.gif
</script>
None.gif
None.gif

转载于:https://www.cnblogs.com/sherrys/archive/2007/01/25/630351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值