简体   繁体   English

使用WCF回调更新asp.net网格视图数据

[英]Using WCF Callback to update asp.net gridview data

I have a WCF Callback implemented in an asp.net web application using a wsdualhttpbinding that I would like to use to update the rows in a gridview on my page. 我有一个wsdualhttpbinding在asp.net Web应用程序中实现的WCF回调,我想用它来更新页面的gridview中的行。 I put the gridview in an update panel, and the callback is fireing on the client, but the data in the grid never gets updated. 我将gridview放在更新面板中,并且回调在客户端上启动,但是网格中的数据从未更新过。 I have tried calling the update panel's Update() method after calling the databind to no avail. 我试图在调用databind无效后调用更新面板的Update()方法。 Is there something I am missing or something else that I need to do to get this to work? 有什么我想念的东西还是我需要做的才能使它起作用?

Here is some of the code I am using: 这是我正在使用的一些代码:

In the page load, I attach to the WCF Callback, I inherit the interface for the callback, and in the implementation of the interface I bind to the grid with the data that is received from the Callback: 在页面加载中,我将附加到WCF回调,继承该回调的接口,并在该接口的实现中,使用从回调收到的数据绑定到网格:

[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public partial class activeJobs : System.Web.UI.UserControl, IAgentMessagingCallback
{
    AgentMessagingClient _messagingClient;

    protected void Page_Load(object sender, EventArgs e)
    {
        InstanceContext context = new InstanceContext(this);
        _messagingClient = new AgentMessagingClient(context, "AgentMessaging_IAgentMessaging");

        if (_messagingClient.Subscribe())
        {
            Page.Title = string.Format("Timeout will occur at {0}", DateTime.Now.AddMinutes(10));
        }
    }

    #region IAgentMessagingCallback Members

    public void ActiveJobs(SubmittedJob[] activeJobs1)
    {
        activeJobsGrid.DataSource = activeJobs1.ToList();
        //checked in the debugger, the data is actually recieved...
        activeJobsGrid.DataBind();

        //the update method for the updatepanel...tried this both ways, no go
        //activeJobsGridUP.Update(); 
    }

    #endregion
}

The Callback is defined as such: 回调的定义如下:

[ServiceContract(CallbackContract = typeof(IAgentMessagingCallback))]
public interface IAgentMessaging
{
    [OperationContract(IsOneWay = true)]
    void SendActiveJobs(List<SubmittedJob> activeJobs);

    [OperationContract(IsOneWay = false)]
    bool Subscribe();

    [OperationContract(IsOneWay = false)]
    bool Unsubscribe();
}

public interface IAgentMessagingCallback
{
    [OperationContract(IsOneWay = true)]
    void ActiveJobs(List<SubmittedJob> activeJobs);
}

public class AgentMessaging : IAgentMessaging
{
    private static readonly List<IAgentMessagingCallback> _subscribers = new List<IAgentMessagingCallback>();

    #region IAgentMessaging Members

    public void SendActiveJobs(List<SubmittedJob> activeJobs)
    {
        _subscribers.ForEach(delegate(IAgentMessagingCallback callback)
        {
            if (((ICommunicationObject)callback).State == CommunicationState.Opened)
            {
                try
                {
                    callback.ActiveJobs(activeJobs);
                }
                catch (Exception ex)
                {
                    Messaging.ErrorMessage(ex, this.ToString());
                }
            }
            else
            {
                _subscribers.Remove(callback);
            }
        });
    }

    public bool Subscribe()
    {
        try
        {
            IAgentMessagingCallback callback = OperationContext.Current.GetCallbackChannel<IAgentMessagingCallback>();
            if (!_subscribers.Contains(callback))
            {
                _subscribers.Add(callback);
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            Messaging.ErrorMessage(ex, this.ToString());
            return false;
        }
    }

    public bool Unsubscribe()
    {
        try
        {
            IAgentMessagingCallback callback = OperationContext.Current.GetCallbackChannel<IAgentMessagingCallback>();
            if (_subscribers.Contains(callback))
            {
                _subscribers.Remove(callback);
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            Messaging.ErrorMessage(ex, this.ToString());
            return false;
        }
    }

    #endregion
}

Does the callback happen before you've returned from the Subscribe operation, or after Page_Load? 回调是在您从Subscribe操作返回之前还是在Page_Load之后发生的? If it happens after Page_Load, I'm concerned about whether the page will still be around when the callback happens. 如果它发生在Page_Load之后,则我担心回调发生时页面是否仍然存在。

You do realize that a new page instance is created on each request? 是否意识到在每个请求上都会创建一个新的页面实例? And that the instance is discarded once the HTML has been sent to the client? 而且,一旦HTML发送到客户端,实例就被丢弃了吗? Once the HTML has been sent to the client, there's nothing the server can do to change it. 将HTML发送到客户端后,服务器将无法对其进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM