wcf学习笔记_2(修改wcf配置文件)
2010-05-24 19:44
483 查看
修改客户端配置文件:
在客户端的配置文件中添加<appSettings>,方便获取更改.
修改服务端配置文件:
在服务端的配置文件中也添加<appSettings>,方便获取更改.
WCF中容易出现的错误:在服务“Service1”实现的协定列表中找不到协定名称
出错的原因有两个:
1. 看契约是否写对, 这个一般不会写错
2.看配置文件:service name="空间名+服务名称" endpoint contract="空间名+契约名称"
(这里有个小细节要注意, ""中不能出现空格,否则依然报错)
在客户端的配置文件中添加<appSettings>,方便获取更改.
/// <summary> /// 更改配置文件 /// </summary> /// <param name="serverIp"></param> public static void ChanageConfig(string serverIp) { Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"]; ServiceModelSectionGroup serviceModelSectionGroup = sct as ServiceModelSectionGroup; ClientSection clientSection = serviceModelSectionGroup.Client; foreach (ChannelEndpointElement item in clientSection.Endpoints) { string[] str = item.Address.ToString().Split('/'); string pattern =""; for (int i = 0; i < str.Length-2; i++) pattern += str[i] + '/'; string address = item.Address.ToString(); string replacement = string.Format("{0}", serverIp); address = Regex.Replace(address, pattern, replacement); item.Address = new Uri(address); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("system.serviceModel"); }
修改服务端配置文件:
在服务端的配置文件中也添加<appSettings>,方便获取更改.
/// <summary> /// 更改服务配置文件 /// </summary> /// <param name="serverIp"></param> public static void ChangeServiceConfig(string serverIp) { Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"]; ServiceModelSectionGroup serviceModelSectionGroup = sct as ServiceModelSectionGroup; ServicesSection serviceSection = serviceModelSectionGroup.Services; foreach (ServiceElement item in serviceSection.Services) { string address = item.Host.BaseAddresses[0].BaseAddress; string[] str = address.Split('/'); string pattern = ""; for (int i = 0; i < str.Length - 2; i++) pattern += str[i] + '/'; string replacement = string.Format("{0}", serverIp); address = Regex.Replace(address, pattern, replacement); item.Host.BaseAddresses[0].BaseAddress = address; } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("system.serviceModel"); }
WCF中容易出现的错误:在服务“Service1”实现的协定列表中找不到协定名称
出错的原因有两个:
1. 看契约是否写对, 这个一般不会写错
2.看配置文件:service name="空间名+服务名称" endpoint contract="空间名+契约名称"
(这里有个小细节要注意, ""中不能出现空格,否则依然报错)
相关文章推荐
- [WCF 学习笔记] 11. 配置文件
- WCF学习笔记之配置文件
- springMVC学习笔记---修改配置文件路径和传递数据
- Lua 学习笔记(三) —— Scite 的配置文件修改
- [WCF 学习笔记] 11. 配置文件
- WPF学习笔记--使用配置文件修改控件、面板颜色
- AndroidManifest.xml 文件配置学习笔记
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- Spring4学习笔记-AOP(基于配置文件的方式)
- nginx学习笔记4 nginx的配置文件2 虚拟主机的配置
- 学习SSM框架笔记五:利用五大组件采取配置文件和SpringMVC模式写HelloWorld
- 【Linux学习笔记】34:登录时生效的环境变量配置文件 上
- 移植内核学习笔记2-----修改分区及制作根文件系统
- Struts2.0学习笔记---设置国际化全局配置文件和包范围配置文件和Action范围配置
- spring MVC学习笔记(二) springMVC.xml配置文件解析
- 电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件
- Linux学习笔记:chmod 修改文件或文件夹权限
- xml配置文件---ssm完美整合(学习笔记)
- 慕课网(imooc)Selenium自动化学习笔记02:读取配置文件及简单参数化
- 博科SAN交换机学习笔记之二:配置文件备份与固件升级 作者 LiaoJL | 转载时请务必以超链接形式标明文章原文链接和作者信息及本版权声明。 原文链接:http://www.liaojl.co