• 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="空间名+契约名称"

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

  • 相关阅读:
    vue点击元素变色兄弟元素不变色
    获取今天昨天本月的时间段
    java.io.InputStream -- 1.8 初识,应用场景待更新
    java.io.FilterInputStream
    java.io.FileInputStream
    java.io.ByteArrayInputStream -- 1.8
    JavaBeans -- 1.8
    mysql 导出和导入数据
    tp5 数据库迁移工具 migrate&seed
    tp5模型一对一关联hasOne
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1734929.html
Copyright © 2020-2023  润新知