【问题标题】:How to fill DataGridView on wcf callback operation如何在 wcf 回调操作上填充 DataGridView
【发布时间】:2014-03-25 03:55:18
【问题描述】:

我尝试在单击按钮时使用 wcf 回调操作填充 datagridview,但在第一次单击时 wcf 返回空值,当我第二次单击按钮时,它会获取数据并填充 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>

【问题讨论】:

    标签: c# wcf c#-4.0 callback


    【解决方案1】:

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

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

    控制器使用WCF调用(wshttpbinding)调用模拟器上的Start()路由,该路由有一个对象的返回值

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

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

    在客户端,控制器每 10 秒循环一次,检查来自模拟器主机的状态更新。

    while ((this.m_DoHeartBeatLoop)) {
    
       //update grid
       if (StatusChange) {
           DataGridView.DataSource = new List<EmulatorStatus>();
       }
    
       //Sleep between each WatchDog Loop
       Thread.Sleep(10000);
    }
    

    我不确定这是否是您正在寻找的,但这是我正在使用的。祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      相关资源
      最近更新 更多