 |
|
|
|
Calling an .asmx Web Service in Silverlight 2
|
|
|
 |
|
Location: Blogs Andy's Blog |
|
| Posted by: host |
3/17/2008 6:14 PM |
I wanted to share a couple of tips for calling web services from a Silverlight client.
If you are just beginning to useWeb Services from a Silverlight 2 Client, Tim Heuer has a good introductory post here.
Getting the Binding Url A lot of the web service samples will show the binding created with a hardcoded Uri such as this:
WebServiceSoapClient webSvc = new WebServiceSoapClient(binding, new System.ServiceModel.EndpointAddress("http://localhost/MyWebService.asmx"));
The problem being of course that the EndpointAddress is hardcoded to localhost, and you may not know what server you will ultimately deploy to. You could add the Uri as a configuration setting, but there is a more flexible way.
Chances are, your web service is hosted on the same web server that your Silverlight application downloads from. So when you create your Web Service binding, you can determine the Url for you service to bind to based on the current browser Uri:
public static string GetUrlForResource(string resourcePage) { string webUrl = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
string containerPage = webUrl.Substring(webUrl.LastIndexOf("/") + 1);
webUrl = webUrl.Replace(containerPage, resourcePage);
return webUrl;
}
... So the method above, when passed a resource string such as "MyWebService.asmx" will return a full Url for a page that is in the same virtual directory as the silverlight app.
When binding, you can then use this utility method like so:
// get the full url of the Web Service string webServiceUrl = GetUrlForResource("WebService.asmx");
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
WebServiceSoapClient webSvc = new WebServiceSoapClient(binding, new System.ServiceModel.EndpointAddress(webServiceUrl));
webSvc.HelloWorldCompleted += new EventHandler<HelloWorldCompletedEventArgs>(webSvc_HelloWorldCompleted); webSvc.HelloWorldAsync();
Keep an Eye on MaxReceivedMessageSize If your web service is returning a lot of data, you may blow out the default max message size for a return. If this happens you will get an exception like so:
An exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll but was not handled in user code
Additional information: [MaxReceivedMessageSizeExceeded]
To remedy this, you can up the MaxReceivedMessageSize attribute of the binding like so:
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
// if you are getting a LOT of data back, you will need to up Message Size binding.MaxReceivedMessageSize = int.MaxValue; |
|
| Permalink |
Trackback |
Comments (8)
Add Comment
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
3/19/2008 2:37 AM
|
Why do we need to use BasicHttpBinding or etc while are are able to add as service reference simplye?
I wrote one sample in this post below and I have no problem in using it. http://michaelsync.net/2008/03/10/silverlight-2-beta1-database-operations-with-aspnet-web-service-in-silverlight-2
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By admin on
3/19/2008 10:44 AM
|
The reason you need to add a binding with an EndPoint is for deployment to another server besides your test project. Because if you look at the generated Proxy code (by expanding the Service reference in solution explorer until you find Reference.cs), you will see that the Uri is hardcoded for your test web service. It will look something like this:
private static System.ServiceModel.EndpointAddress defaultAddress = new System.ServiceModel.EndpointAddress("http://localhost:58920/MySLApp_Web/MyService.svc");
... so when you go to actually deploy this SL app and web service, this default binding will no longer work.
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
4/25/2008 2:10 AM
|
not with the portnumber how can we call webservice using localhost (iis)
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
6/2/2008 2:35 AM
|
Hi,
Nice article!
I tried this one out but my web service is returning more than 65000 rows. I already increased the MaxReceivedMessageSize to int's maxvalue. But I'm getting this error:
An exception of type 'System.ServiceModel.ProtocolException' occurred in System.ServiceModel.dll but was not handled in user code
Additional information: [UnexpectedHttpResponseCode] Arguments:Not Found Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=2.0.30226.2&File=System.ServiceModel.dll&Key=UnexpectedHttpResponseCode
But if I would filter the data being returned from the web service, it would not throw an exception.
Do you have any idea on this?
Thanks.
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
6/2/2008 8:04 AM
|
You could try using an HTTP tracing tool such as Fiddler or Nikhil's Web Development Helper to get a capture of the response coming back. There is a good chance that will contain better exception information than what you are seeing.
-Andy
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
11/27/2008 4:49 PM
|
Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Server stack trace: at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint) at System.Net.HttpRequestCreator.Create(Uri Uri) at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase) at System.Net.WebRequest.Create(Uri requestUri) at System.ServiceModel.Channels.HttpChannelFactory.GetWebRequest(EndpointAddress to, Uri via, NetworkCredential credential, TokenImpersonationLevel impersonationLevel, AuthenticationLevel authenticationLevel, SecurityTokenProviderContainer proxyTokenProvider, SecurityTokenContainer clientCertificateToken, TimeSpan timeout) at System.ServiceModel.Channels.HttpChannelFactory.GetWebRequest(EndpointAddress to, Uri via, SecurityTokenProviderContainer tokenProvider, SecurityTokenProviderContainer proxyTokenProvider, SecurityTokenContainer clientCertificateToken, TimeSpan timeout) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.GetWebRequest(EndpointAddress to, Uri via, SecurityTokenContainer clientCertificateToken, TimeoutHelper& timeoutHelper) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.GetWebRequest(EndpointAddress to, Uri via, TimeoutHelper& timeoutHelper) at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at WpfBrowserApplication1.WeatherService.WeatherSoap.GetCityWeatherByZIP(String ZIP) at WpfBrowserApplication1.WeatherService.WeatherSoapClient.GetCityWeatherByZIP(String ZIP) at WpfBrowserApplication1.Page1.Page_Loaded(Object sender, RoutedEventArgs e)
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
11/27/2008 4:52 PM
|
|
Above is the exception I get when I call theASMX from a XBAP. I am using VS 2008 on Vista Ultima with NETFX 3.5 SP1. I'd appreciate if you can help me resolve this.
|
|
|
Re: Calling an .asmx Web Service in Silverlight 2
|
By Anonymous on
8/14/2009 8:54 AM
|
i, I created a Proxy Helper class which instantiates proxies with the endpoint address and proxy configuration found in the app.config file and which then just overwrites the proxy address with the correct one.
internal static class FacadeProxyHelper where TProxyInterface : class where TProxy : ClientBase, new() { #region Public methods
/// /// Creates a new instance of the class. /// /// An instance of the class. public static TProxy GetProxyInstance() { TProxy proxy = new TProxy(); proxy.Endpoint.Address = new EndpointAddress(GetProxyEndpointAddress(proxy.Endpoint.Address)); return proxy; }
#endregion
#region Private methods
/// /// Determines the URL for the proxy based on the current browser Uri. /// /// The proxy endpoint address. /// The URL of the facade proxy. private static string GetProxyEndpointAddress(EndpointAddress endpointAddress) { string address = endpointAddress.ToString(); string baseUrl = HtmlPage.Document.DocumentUri.ToString().Replace(HtmlPage.Document.DocumentUri.LocalPath, string.Empty);
string proxyUri = address.Substring(address.LastIndexOf("/")); return string.Concat(baseUrl, proxyUri); }
#endregion }
Enjoy, Sh@h Mohamod
|
|
|
|
 |
|
 |
|
|