• wcf学习笔记_2(修改wcf配置文件)


    修改客户端配置文件:

    在客户端的配置文件中添加<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="空间名+契约名称"

    (这里有个小细节要注意, ""中不能出现空格,否则依然报错)

  • 相关阅读:
    金蝶VB插件开发,单据不满足条件,拒绝保存
    继承(子类构造执行的过程)
    Javascript基础(一)
    异常处理(一)
    File I/O(三)
    图片轮播
    java中的foreach循环
    File I/O(一)
    集合框架(三)
    集合框架(二)
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1734929.html
Copyright © 2020-2023  润新知