首页新闻找找看学习计划

wcf客户端更改终结点

0
悬赏园豆:5 [已关闭问题] 关闭于 2013-12-12 14:37

需求:服务端对外提供一个终结点用于提供给其他客户端访问(IP为动态) 采用双向异步通过回调方式通知客户端处理结束。

客户端引用服务后TCPIP方式进行操作,首先采用默认终结点连接服务端,如果连接不到的时候,可以手动进行更改配置,重新实例代理类客户端(客户端不允许重新启动)。

 

目前实现方式:

在客户端要使用服务的地方,实现回调函数接口,实例化代理类客户端

代码如下

//实现患者信息查询回调服务接口     public partial class patientForm : Form, IPatientInfoCallback     {         private PatientInfoClient proxy;         public patientForm()         {             InitializeComponent();

            //初始化WCF服务实例

            if (ClientTools.PatientClient == null )             {                 InstanceContext site = new InstanceContext(this);                  proxy = new PatientInfoClient(site);             }         }                   

        #region BtnPatientQuery_Click         /// <summary>         /// 患者查询执行事件         /// </summary>         /// <param name="sender"></param>         /// <param name="e"></param>         private void BtnPatientQuery_Click(object sender, EventArgs e)         {             try             {                 if (this.txtPatientName.Text.Length <= 0)                 {                     MessageBox.Show("请输入患者姓名");                     return;                 }                                CommunicationState cs = proxy.State;                 proxy.GetPatientInfo(this.txtPatientName.Text);             }             catch (Exception ex)             {             }         }         #endregion

        #region ReceivePatient         /// <summary>         /// 回调函数 用于接收服务端 处理结果         /// </summary>         /// <param name="info"></param>         public void ReceivePatient(PatientInfo info)         {             try             {                 ClearPatient();                 if (info == null)                 {                     MessageBox.Show("患者不存在!");                 }                 else                 {                     this.txtPatientAge.Text = info.Age;                     this.txtPatientName.Text = info.Name;                     this.txtPatientSex.Text = info.Sex;                 }             }             catch (Exception ex)             {                 MessageBox.Show("查询患者失败!");             }

        }         #endregion

        #region ClearPatient         /// <summary>         /// 清除界面患者信息         /// </summary>         public void ClearPatient()         {             this.txtPatientAge.Text = string.Empty;             this.txtPatientName.Text = string.Empty;             this.txtPatientSex.Text = string.Empty;         }         #endregion

        private void button1_Click(object sender, EventArgs e)         {             AddressSet asShow = new AddressSet();             asShow.ChangerAddress += new ChangeClient(asShow_ChangerAddress);             asShow.Show();         }

        void asShow_ChangerAddress(EndpointAddress address)         {             try             {                 InstanceContext site = new InstanceContext(this);                 proxy = new PatientInfoClient(site, "TCPConnection", address);             }             catch (Exception ex)             {             }         }

 

 

AddressSet窗口为地址配置窗口,配置完成之后 ,通过委托,通知父窗体中的服务代理类中的Address进行变更,然后再进行调用。

 

现在想要做的是,请教一下诸位有没有更好的更方便的操作,目前主要问题是涉及到回调函数,定为全局变量实例化会出现调用错乱

Address配置页面代码

 

 

    private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                EndpointAddress Address = new EndpointAddress(new Uri("net.tcp://" + this.txtIP.Text + ":" + this.txtPort.Text + "/PatientDuplex"));
                ChangerAddress(Address);
                MessageBox.Show("修改成功", "修改配置");
                this.Close();
            }
            catch (Exception ex)
            {
                DialogResult mesResult = MessageBox.Show("修改失败是否重新修改", "修改配置", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (mesResult != DialogResult.OK)
                {
                    this.Close();
                }
            }

 

 

现在想要做的是,请教一下诸位有没有更好的更方便的操作,目前主要问题是涉及到回调函数,定为全局变量实例化会出现调用错乱

不起名字了的主页 不起名字了 | 初学一级 | 园豆:25
提问于:2013-01-10 10:22
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册