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

本文关键字:填充 DataGridView 操作 回调 wcf | 更新日期: 2023-09-27 17:52:47

我已经尝试使用wcf回调操作按钮点击,但在第一次点击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);
            }
        }
    }

网络。配置

<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>

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

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

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

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

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

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

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

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

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