Simple WCF Webservice Call from Silverlight (Need Help Please!)
I'm new to Silverlight programming and networking in general and I've been trying to find an answer to this all day. I'm trying to build a very simple Silverlight4 web application in VS2010 that calls a webservice on the ASP.NET website (exact same Solution as the Silverlight client app so there shouldn't be any cross-domain issues???). I wrote a Webservice using VS' "Silverlight-Enabled WCF Service" that simply returns a string.
I then wrote the code to consume the service in Silverlight:
public MainPage() { InitializeComponent();
TestServiceClient proxy = new T开发者_Python百科estServiceClient();
EndpointAddress address = new EndpointAddress("http://localhost:" + HtmlPage.Document.DocumentUri.Port +
"/SilverlightApplication1.web/TestService.svc");
proxy.Endpoint.Address = address;
proxy.GetStringCompleted += new EventHandler<GetStringCompletedEventArgs>(proxy_GetStringCompleted);
proxy.GetStringAsync();
}
void proxy_GetStringCompleted(object sender, GetStringCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
This works great when I run it from VS2010. However, when I publish it to my personal webserver (IIS7) on the same computer, I get an error:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; OfficeLiveConnector.1.4; OfficeLivePatch.1.3; .NET4.0C; .NET4.0E) Timestamp: Mon, 17 May 2010 08:29:51 UTC
Message: Unhandled Error in Silverlight Application An exception occurred during the operation, making the result invalid. Check InnerException for exception details. at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at SilverlightApplication1.TestServiceReference1.GetStringCompletedEventArgs.get_Result() at SilverlightApplication1.MainPage.proxy_GetStringCompleted(Object sender, GetStringCompletedEventArgs e) at SilverlightApplication1.TestServiceReference1.TestServiceClient.OnGetStringCompleted(Object state) Line: 1 Char: 1 Code: 0 URI: http://thunder.webhop.org:8001/home.html
When I catch the exception in App.xaml.cs, I get the following message:
System.InvalidOperationException: Eval failed. at System.Windows.Browser.HtmlWindow.Eval (String code) at SilverlightApplication1.App.ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
I should mention that the webserver otherwise seems to work fine. I can host Silverlight apps on it without any problems through port 8001. I just can't for the life of me figure out how to make a successful webservice call! Any help with this would be GREATLY appreciated. I've wasted an entire afternoon and evening on this. My mind's flipping in circles right now... :-(
Many, many thanks in advance! Luck
P.S. Apologies for cross-posting at Silverlight forums but had some problems there while posting.
I guess this doesn't actually have to do with the web service call as the exception caught in App.cs says something about "Eval failed." I think it might be the code in your proxy_GetStringCompleted handler. MessageBox.Show is nothing else than a JavaScript alert behind the scenes. Maybe you're using a browser that has a JavaScript blocker activated or the like? Anyway; what you could try is to simply use something else than MessageBox.Show(), e.g.
(new ChildWindow { Content = new TextBlock { Text = e.Result.ToString() } }).Show();
.
Cheers, Alex
精彩评论