简体   繁体   English

使用双工WCF服务调用结果更新ASP.NET页

[英]Update an ASP.NET page with Duplex WCF Service Call Result

I am having a problem to design the best way for invoking a duplex WCF service from ASP.NET Application I have the following scenario: 我在设计从ASP.NET应用程序调用双工WCF服务的最佳方法时遇到问题,我遇到以下情况:

1) I have a duplex WCF service with some operations 1)我有一些操作的双工WCF服务
2) From an ASP.NET web application (which is my client) I make the default page implement the callback Interface and then call a method from the duplex service sending itself as the handler of the callback 2)从ASP.NET Web应用程序(这是我的客户端),使默认页面实现回调接口,然后从双工服务中调用一个方法,将其自身作为回调的处理程序发送
3) When the callback returns on the default.aspx page I couldn't show the result on the page because the whole HttpContext is null so I can't access any control or Application[] or Session[] variables 3)当回调在default.aspx页面上返回时,我无法在页面上显示结果,因为整个HttpContext为null,因此我无法访问任何控件或Application []或Session []变量

Here is the code in the Default.aspx 这是Default.aspx中的代码

[CallbackBehavior(UseSynchronizationContext = false)]
public partial class _Default : System.Web.UI.Page, VehicleTrackingService.IDuplexServiceCallback
{
    public _Default()
    {

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
        {
            try
            {

                DuplexService client = new DuplexServiceClient(new InstanceContext(new_Default()));
                switch (DropDownList1.SelectedItem.Value)
                {
                    case "0":
                        {
                            client.Method1(int.Parse(txt_name.Text));
                            break;
                        }
                    case "1":
                        {
                            lbl_res.Text = "Not Provided yet.";
                            break;
                        }
                    default:
                        {
                            lbl_res.Text = "Not Provided yet.";
                            break;
                        }
                }
            }
            catch(Exception ex)
            {
            }
        }));
    }

   public void DuplexCallbackFunction(string response)
    {
        // Wanna to show result (the response) from here ...
    }

Any Help Please? 有什么帮助吗?

You are calling the WCF service from the ASP.Net page while the page is being processed on the server. 您正在ASP.Net页上调用WCF服务,而正在服务器上处理该页。

The problem with this is that the page is on the server for a very short time, purhaps less than a second. 问题是页面在服务器上的时间很短,可能不到一秒钟。 Then the page has been returned to the browser before the WCF service has responded. 然后,在WCF服务响应之前,该页面已返回到浏览器。

As Lloyd mentioned the way to fix this is to call the web service from your browser using AJAX. 正如劳埃德(Lloyd)所提到的,解决此问题的方法是使用AJAX从浏览器中调用Web服务。

For one example of how to do this see http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call 有关如何执行此操作的示例,请参见http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call

You have to keep in mind that your application consists of a browser client that is accessing the ASP.NET application as a server, and these two communicate via HTTP requests and responses. 您必须记住,您的应用程序由一个作为服务器访问ASP.NET应用程序的浏览器客户端组成,这两个客户端通过HTTP请求和响应进行通信。 Your ASP.NET application will most likely have sent an HTTP response back to the browser before the WCF service sends a message back to the ASP.NET application. 在WCF服务将消息发送回ASP.NET应用程序之前,您的ASP.NET应用程序很可能已将HTTP响应发送回了浏览器。

I think Shiraz and Lloyd have made an excellent suggestion; 我认为设拉子和劳埃德提出了很好的建议。 try to call the WCF service directly from the browser, if possible. 如果可能,请尝试直接从浏览器调用WCF服务。 This answer suggests that it may be possible to perform duplex communication with a WCF service with JavaScript. 该答案表明,可能有可能通过JavaScript与WCF服务执行双向通信。

However, there may be a number of reasons why you can't do that, such as credentials, network firewall rules, or simply the fact that the ASP.NET application has the necessary data to make the call to the WCF service, and perhaps you don't want to expose that data to the browser. 但是,您可能无法执行此操作的原因有很多,例如凭据,网络防火墙规则,或者仅仅是ASP.NET应用程序具有必要的数据来调用WCF服务的事实,也许您不想将该数据公开给浏览器。

In these cases, you can choose to implement complicated solutions that involve using JavaScript in the browser to poll the ASP.NET server for updates. 在这些情况下,您可以选择实现复杂的解决方案,这些解决方案涉及在浏览器中使用JavaScript来轮询ASP.NET服务器以获取更新。 There are a couple of ways to do this. 有两种方法可以做到这一点。 There is "short polling" and "long polling". 有“短轮询”和“长轮询”。 There is also a relatively new feature called WebSockets, but whether or not your server and your target browser supports WebSockets is another question. 还有一个相对较新的功能,称为WebSockets,但是服务器和目标浏览器是否支持WebSockets是另一个问题。

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

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