如何在wcf回调操作上填充DataGridView

时间:2014-03-25 03:55:19

标签: c# wcf c#-4.0 callback

我试图在按钮点击时使用wcf回调操作填充datagridview但是在第一次单击时wcf返回null值&当我第二次点击按钮时,它会获取数据&填写gridview数据。

以下是我的WCF服务....

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract =typeof(IServiceCallback))]
    public interface IService
    {
        [OperationContract(IsOneWay=true)]
        void GetData(string userName,string password);
    }

    /// <summary>
    /// The Callback Interface
    /// </summary>
    public interface IServiceCallback
    {
        [OperationContract(IsOneWay=true)]
        void SendResult(Department[] arrDept);
    }


    [DataContract]
    public class Department
    {
        [DataMember]
        public int DeptNo { get; set; }
        [DataMember]
        public string DeptName { get; set; }
        [DataMember]
        public int Capacity { get; set; }
    }


[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class Service : IService
    {

        /// <summary>
        /// Get a channel instance that is used to called the current operation 
        /// </summary>
        public IServiceCallback CallBack
        {
            get 
            {
                return OperationContext.Current.GetCallbackChannel<IServiceCallback>(); 
            }
        }

        public void GetData(string userName, string password)
        {
            Department[] arrDept = new Department[]
            {
                 new Department() {DeptNo=10,DeptName="IT",Capacity=4500},
                 new Department() {DeptNo=20,DeptName="HRD",Capacity=200},
                 new Department() {DeptNo=30,DeptName="ACCTS",Capacity=40}
            };

            if (userName.Trim() == "mahesh" && password == "mahesh")
            {
                Thread.Sleep(10000); 
                CallBack.SendResult(arrDept);
            }
        }
    }

的Web.Config

<system.serviceModel>
    <services>
      <service behaviorConfiguration="Service"
        name="WCF_CallBack_Service.Service">
        <endpoint address="" binding="wsDualHttpBinding" 
                  contract="WCF_CallBack_Service.IService">
        </endpoint>
        <endpoint address="mex" 
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Service">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户电话......

public class RequestCallBack : IServiceCallback
    {
        private Department[] _Departments;

        public Department[] Departments
        {
            get { return _Departments; }
            set 
            {
                _Departments = value;
            }
        }
        public void SendResult(Department[] arrDept)
        {
            Departments = arrDept;
            MessageBox.Show("Response  Received " + Departments.Count().ToString() );

        }


    }


public partial class Form1 : Form
    {
        MyRef.ServiceClient Proxy;
        RequestCallBack callback;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            Proxy.GetData("mahesh", "mahesh");
           MessageBox.Show("Values are send to the service");
          dgvDept.DataSource = callback.Departments; 

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            callback = new RequestCallBack(); 
            //Get the InstanceContext with the help of the CallBack contract class
            InstanceContext context = new InstanceContext(callback);
            Proxy = new MyRef.ServiceClient(context);
        }
    }

客户端app.config ..

<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_IService" />
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:4221/Service.svc" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IService" contract="MyRef.IService"
                name="WSDualHttpBinding_IService">
                <identity>
                    <userPrincipalName value="MY-PC\MY PC" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:0)

我猜你正在寻找一些可以让它神奇地工作的代码,但我认为你不会发现它。我可以告诉你如何使用WCF来更新我的DataGridView。

所以我有一个网格存在的控制器程序,网格用于显示加载测试软件的一组模拟器的状态。因此,当我启动控制器和负载测试时,接下来就是:

控制器使用WCF调用(wshttpbinding)来调用仿真器上的Start()路由,该路由具有对象的返回值

public void Start(IProgress<EmulatorStatus> progress)
{
}

在仿真器端,每次仿真器完成例程时,它都使用Progress.Report(EmulatorStatus)将描述仿真状态的对象(包含文本,数字,布尔值)发送回Controller。

在客户端,Controller每10秒循环一次,检查仿真器主机的状态更新。

while ((this.m_DoHeartBeatLoop)) {

   //update grid
   if (StatusChange) {
       DataGridView.DataSource = new List<EmulatorStatus>();
   }

   //Sleep between each WatchDog Loop
   Thread.Sleep(10000);
}

我不确定这是不是你要找的东西,但这就是我正在使用的东西。祝你好运。