简体   繁体   中英

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:

1) I have a duplex WCF service with some operations
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
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

Here is the code in the 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.

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.

As Lloyd mentioned the way to fix this is to call the web service from your browser using AJAX.

For one example of how to do this see 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. 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.

I think Shiraz and Lloyd have made an excellent suggestion; try to call the WCF service directly from the browser, if possible. This answer suggests that it may be possible to perform duplex communication with a WCF service with JavaScript.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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