使用WCF回调更新asp.net gridview数据

时间:2009-08-13 14:36:24

标签: c# asp.net wcf callback

我在asp.net Web应用程序中使用wsdualhttpbinding实现了WCF回调,我想用它来更新页面上gridview中的行。我将gridview放在更新面板中,并且回调在客户端上激活,但网格中的数据永远不会更新。我已经尝试在调用数据绑定后调用更新面板的Update()方法无效。是否有我缺少的东西或其他我需要做的事情才能让它发挥作用?

以下是我正在使用的一些代码:

在页面加载中,我附加到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
}

回调定义如下:

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

1 个答案:

答案 0 :(得分:1)

回调是在您从Subscribe操作返回之后还是在Page_Load之后发生的?如果它发生在Page_Load之后,我担心在回调发生时页面是否仍然存在。

意识到每个请求都会创建一个新的页面实例?一旦将HTML发送到客户端,实例就会被丢弃?将HTML发送到客户端后,服务器无法对其进行更改。