Below is the code i used to consume web servcie in SilverLight.
private void button1_Click(object sender, RoutedEventArgs e)
{
BasicHttpBinding bind = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress("http://loalhost/Service.asmx");
ServiceSoapClient client = new ServiceSoapClient(bind, endpoint);
client.RunHelloCompleted += new EventHandler<RunHelloCompletedEventArgs>(client_RunQwinCompleted);
client.RunHelloAsync(command);
}
void client_RunHelloCompleted(object sender, RunHelloCompletedEventArgs e)
{
txtProcess.Text=Process(e.Result);
}
I want to know a way that after i run RunHelloAsync(Command), I want to get the returned result without going to Completed event. Please advise me. thank you.
Simple answer : You can't. Everything in Silverlight is Asynchronous so there is no way to block after the client.RunHelloAsync(command) call and wait for the result.
Long answer : There are ways to simulate working with calls in a synchronous fashion, but the calls still being made asynchronously. Take a look at this thread for a few more answers.
Related
Ok ... next question. I am using Silverlight 5 C# 2012 and have gotten to a point where I could use a little help. I am using the Domain Model to call methods to return information and I discovered that if I hit the same button more than once, the method call happens incrementally. One call for the first click, two calls for the second click, three call for the third, etc.
Please check out the simple code below and if you can provide any insight as to why this is happening, I would greatly appreciate it.
In the Service Layer:
Interface:
[OperationContract]
string GetEnteredString(string strText);
Class:
public string GetEnteredString(string strText)
{
return "You entered: " + strText;
}
In the Silverlight Application:
private void btnGetText_Click(object sender, RoutedEventArgs e)
{
service.GetEnteredStringCompleted += new EventHandler<CARE_WCF.GetEnteredStringCompletedEventArgs>(service_GetEnteredStringCompleted);
service.GetEnteredStringAsync(txtTester.Text);
}
private void service_GetEnteredStringCompleted(object sender, CARE_WCF.GetEnteredStringCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
This seems pretty darn straight forward at first glance, but the call to the GetEnteredString method just keeps stacking on top of itself. I think it's because of the += in the Click event but I could be wrong.
Thank you in advance for any insight! I really appreciate it.
Eric
You have to unsubscribe from the event in Completed function like :
service.GetEnteredStringCompleted -= service_GetEnteredStringCompleted;
I was testing an Asynchronous example I wrote in other post, I modified it to show some info in a textbox. what happened next I was not expecting. I don't know why it does not throw an exception when modifying a control from another thread. am I blind or why I don't see it?
here is the example, it works the same for silverlight and WinForms:
int rand=0;
public MainPage()
{
InitializeComponent();
}
public Func<Action<int, int>, Action<int>> DownloadDataInBackground = (callback) =>
{
return (c) =>
{
WebClient client = new WebClient();
Uri uri = new Uri(string.Format("https://www.google.com/search?q={0}", c));
client.DownloadStringCompleted += (s, e2) =>
{
callback(c, e2.Result.Length);
};
client.DownloadStringAsync(uri);
};
};
private void button1_Click(object sender, RoutedEventArgs e)
{
int callid = rand++;
Debug.WriteLine("Executing CallID #{0}", callid);
DownloadDataInBackground((c3, r3) =>this.textBox1.Text+=string.Format("The result for the callid {0} is {1} \n", c3, r3))(callid);
}
Tap the button pretty fast, it wont fail.
your help will be very appreciated.
Edit: added picture showing that windows forms always execute controls modification from the main thread, but, why if it is supposed to be another one?
The actual answer to why your code doesn't fail in the way you expect it to is that the WebClient invokes its events on the UI thread. Hence you aren't modifying your control on a different thread as you seem to imagine you are.
I am new to silverlight, just wanted know if the steps involved in calling the WCF service same in Silverlight as it was in asp or is there any difference.If there is any difference then request you to help me out.
Thanks in advance.
The main things that you needs aware are
silverlight support only basichttpbinding
you need to have either clientaccesspolicy.xml file or crossdomain.xml in your root folder of wcf host server, then only you can successfully call a webservice from silerlight
http://msdn.microsoft.com/en-us/library/cc197955%28v=vs.95%29.aspx
I got the answer, there is slight difference between asp and silverlight.
In silverlight all service calls are asynchronous, so you have to have an eventhandler for when the async call is completed you get your data.
Just a small e.g
When you call your WCF service from asp, you use
proxy_http.FunctionClient fc = new proxy_http.FunctionClient();
txtDisplay.Text = fc.Add(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text)).ToString();
in case of Silverlight you will use
private void Add_Click(object sender, RoutedEventArgs e)
{
proxy_htt.FunctionClient fc = new proxy_htt.FunctionClient();
fc.AddCompleted += new EventHandler<proxy_htt.AddCompletedEventArgs>(fc_AddCompleted);
fc.AddAsync(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text));
}
void fc_AddCompleted(object sender, proxy_htt.AddCompletedEventArgs e)
{
txtResult.Text = e.Result.ToString();
}
How do I call a webservice in the Application_Exit event?
private void Application_Exit(object sender, EventArgs e)
{
TestWSSoapClient.ReleaseUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(TestWSSoapClient_ReleaseUserCompleted);
TestWSSoapClient.ReleaseUserAsync(UserToken);
}
The method below is no longer executed.
void TestWSSoapClient_ReleaseUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
}
Thank you in advance.
You can't call any web services when exiting by design, however this article might help you with an Javascript alternative.
Do you really need to get the response? You won't be able to get it as all threads will be closed at the end of the Application_Exit function. But if it doesn't matter, you can configure your "ReleaseUser" operation as OneWay.
As I didn't test it, it is only a supposition, but I think it should work (and if it doesn't, well, I will learn something)
Otherwise, as said before, javascript is the only way to go, but it won't work Out of Browser.
I'm calling off asynchronously to a web service (Amazon Web Services) from a Silverlight app and my callback method is never actually triggered after I start the asynchronous call.
I've set up another web service proxy in a console app, and I'm able to make a synchronous call and get a response using the same arguments without any issues.
Am I possibly having problems with the fact that this is called from within a browser? I'm not sure where to start, since I don't get a response at all, much less an error.
Below is the code I'm using:
private void btnQueryAmazon_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(txtQuery.Text))
{
ItemSearch search = new ItemSearch();
/// set authentication and search parameters
AmazonService.AWSECommerceServicePortTypeClient service = new AmazonService.AWSECommerceServicePortTypeClient();
service.ItemLookupCompleted += new EventHandler<AmazonService.ItemLookupCompletedEventArgs>(service_ItemLookupCompleted);
service.ItemSearchAsync(search);
}
}
void service_ItemLookupCompleted(object sender, AmazonService.ItemLookupCompletedEventArgs e)
{
txtError.Text = e.Result.Items.Count().ToString();
grdItems.ItemsSource = e.Result.Items;
}
Well, there's your problem ;)
It looks like you're calling the ItemSearch method on the service, but you're wiring up and handling the ItemLookup method.
I do it all the time.