在运行时更改服务的地址

时间:2011-03-11 07:56:44

标签: wcf

我的客户端Web配置就像这样

    <client>
        <endpoint address="http://192.168.1.7/zfsapi/api.php" binding="basicHttpBinding"
            bindingConfiguration="ZfsSoapBinding" contract="SourceAPI.ZfsSoapPort"
            name="ZfsSoapPort" />          
    </client>

我在运行时点击我的地址

        EndpointAddress epa1 = new EndpointAddress("http://192.168.1.7/zfsapi/api.php");
        DemoChangingAddressApi.SourceAPI.ZfsSoapPortClient oservice = new SourceAPI.ZfsSoapPortClient(binding1, epa1);
        DemoChangingAddressApi.SourceAPI.ZfsVolume[] v1 = oservice.getVolumeList();


       // or instantiate whatever other binding you're using    
        BasicHttpBinding binding = new BasicHttpBinding();

       // define the endpoint address
        EndpointAddress epa = new EndpointAddress("http://192.168.1.8/zfsapi/api.php");

       // create your WCF client-side proxy based on those settings
       DemoChangingAddressApi.SourceAPI.ZfsSoapPortClient oservice1 = new SourceAPI.ZfsSoapPortClient(binding, epa);
       DemoChangingAddressApi.SourceAPI.ZfsVolume[] v2 = oservice1.getVolumeList();

当我这样做时,我得到错误@ DemoChangingAddressApi.SourceAPI.ZfsVolume[] v2 = oservice1.getVolumeList();

错误:

  

反序列化主体时出错   操作消息'getVolumeList'。

我如何在服务运行时更改地址

1 个答案:

答案 0 :(得分:0)

如果您在运行时始终可以访问web.config文件作为xml: 像这样的东西:             //在下面的代码中我有动态制作网站副本的要求

        // Set the root path of the Web application that contains the
        // Web.config file that you want to access.
        string configPath = ConfigurationManager.AppSettings["appRoot"] + virtualDirectoryName + "\\";

        XmlDocument doc = new XmlDocument();
        bool change = false;
        string configFile = Path.Combine(configPath, "web.config");
        doc.Load(configFile);

        //get root element
        System.Xml.XmlElement Root = doc.DocumentElement;
        //I have created appSettings dictionary that holds key/value pairs that wanted to update in section.
        Dictionary<string, object> appSettings = new Dictionary<string, object>();
        appSettings.Add("connString", "server=" + serverAddressAppDb +
                            "; uid=" + uidAppDb + "; pwd=" + pwdAppDb +
                            "; database=" + virtualDirectoryName + "; min pool size=1; max pool size=100;");
        appSettings.Add("applogin", urlApp + virtualDirectoryName + "/login.aspx");
        appSettings.Add("appUrl", urlApp + virtualDirectoryName);
        appSettings.Add("mailer_completeImagePath", urlApp + virtualDirectoryName + "/backgrounds/");
        appSettings.Add("mailer_pathImagesComplete", urlApp + virtualDirectoryName + "/images/");

        // Now the code below will go loop through each key/value pair under section and will compare the values in with appSettings dictionary. If it sees values are different, It will update in section.
        XmlNode appNode = Root["appSettings"];
        foreach (XmlNode node in appNode.ChildNodes)
        {
            if (node.Attributes != null)
            {
                try
                {
                    string key = node.Attributes.GetNamedItem("key").Value;
                    string value = node.Attributes.GetNamedItem("value").Value;
                    if (appSettings.ContainsKey(key) && value != appSettings[key].ToString())
                    {
                        node.Attributes.GetNamedItem("value").Value = appSettings[key].ToString();
                        change = true;
                    }
                }
                catch (Exception e)
                {
                }
            }
        }