从WCF获得响应后显示消息

时间:2013-02-11 06:05:22

标签: c# multithreading push-notification duplex wcf-callbacks

我正在为推送通知构建WCF应用程序,其中用户必须首先订阅自己并在订阅客户端后从WCF获得响应。

我在WCF中使用wsDualHttpBinding作为绑定和回调。

我不想从客户端调用递归函数。我在WCF和WCF中使用计时器在数据库中发生任何更新时抛出消息。我的WCF已准备就绪,但无法在客户端显示消息。

现在我的问题是 如何在客户端从WCF获得响应时显示消息。

我在aspx.cs上的客户端代码是:

public void SendResult(string message)//I got message = "Test String"
{
   Response.Write(message);// it throw error "Response is not available in this context."
// I also use this code 

HttpContext.Current.Response.Write(message); // but it also give error "Object reference not set to an instance of an object."

}

这是一个非常关键的问题,我只需要显示消息

任何帮助都将受到赞赏。

编辑1

服务方法调用

  protected void Page_Load(object sender, EventArgs e)
  {
        SocialProfilesService.SocialClient client = 
               new SocialProfilesService.SocialClient(new InstanceContext(this)); 
        client.Subscribe(userid)
  } 

编辑2

我也试过

 SynchronizationContext uiSyncContext;
 uiSyncContext = SynchronizationContext.Current;
 public void SendResult(string message)//I got message = "Test String"
 {
   SendOrPostCallback callback = delegate(object state)
    {
        Response.Write(message);
    };
    uiSyncContext.Post(callback, message);
 }

但它也给我错误“对象引用未设置为对象的实例”。 该做什么

1 个答案:

答案 0 :(得分:0)

你应该看看这篇文章。这应该有所帮助。它提供了订阅和推送更新的简单示例。

Article

根据我们的讨论。看来你的服务工作正常。我假设您正在适当地调用SendResult并传递消息。这是你可以尝试的另一件事......这是来自Asp.net的java脚本警报

    Page.RegisterStartupScript("startScript", "<script language=JavaScript>Message("+message+" );</script>");

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "testmsg", "<script>alert('your message goes here');</script>");