Skip to main content
Microsoft Silverlight
Home Forums Silverlight Programming Programming with .NET - General Webservice problem System.Net.WebException
17 replies. Latest Post by einar on November 6, 2007.
(0)
elisnet.org
Member
42 points
35 Posts
07-06-2007 4:44 AM |
I wrote the Silverlight component that access the webservice. When I run it in my local web-server all is ok. But when I uploaded it on the remote test server I have received the following exception.
I use the following address http://maps.elisnet.org/TestNativeReal.aspx. The address of web-service is http://maps.elisnet.org/wms.asmx. Implementation includes asynchronous web service call. What is the problem?
Thanks.
Silverlight error message
ErrorCode: 1001
ErrorType: RuntimeError
Message: System.Net.WebException: Error invoking service. ---> System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at System.Net.WebHeaderCollection..ctor(String headers)
at System.Windows.Browser.Net.BrowserHttpWebRequest.CreateResponse()
at System.Windows.Browser.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Windows.Browser.Net.SoapHttpClientProtocol.GetResponseAsyncCallback(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
Jubber
40 points
24 Posts
07-06-2007 7:53 AM |
Hi,
I wrote a quick desktop app to connect to your web service, and made a call to GetMapCanvases, with some dummy data I came up with. I received an array of MapCanvasInfo objects. This time, I got 1 object which looks like it had valid data in it including Xaml.
The error that you describe looks like you have some problem with your web service, and not silverlight. Check to see what you store in a Dictionary, or some other collection. Otherwise, start putting in some debug info into your webservice (eg. event log), and narrow down where the error is occuring.
07-07-2007 2:26 AM |
I also tested web-service with desktop application. But the problem is not in web service. It returns static data without extra analisys and logic. I think, the problem is in silverlight class WebHeaderCollection and the specific answer from server.This class uses Environment.NewLine to parse string headers. May be server response was in different format (with other Environment.NewLite value)?
geetanjali
52 points
32 Posts
07-09-2007 9:23 AM |
hi, can u send me sample code for interacting with web -service from SilverLight,plz.
Thanx.
y_makram
Contributor
6594 points
1,299 Posts
07-09-2007 9:38 AM |
You can find a good tutorial here http://peterkellner.net/2007/06/18/silverlightdebugwebservicedotnet/
07-09-2007 10:38 PM |
Hi, this is the sample code:
//Web service call
IAsyncResult iar = wsAsync.BeginGetMapCanvases(geoBounds.Left, geoBounds.Top, geoBounds.Right, geoBounds.Bottom, controlBounds.Width, controlBounds.Height, kvp.Value, Thread.CurrentThread.CurrentUICulture.Name, new AsyncCallback(OnMapObjectsDownload), wsAsync);
//Simple call from desktop application (checks that web service work correctly)
//request.Method = "POST";//request.ContentType = "application/json";//StringBuilder sb = new StringBuilder();//sb.Append("{\"minLat\":42.875118318318329,\"minLng\":74.6064367286995,\"maxLat\":42.884881681681691,\"maxLng\":74.6135632713005,\"controlWidth\":1000,\"controlHeight\":1000,\"layers\":\"2bb00010-bde5-48a7-a694-b86d3a80e899,7807bc0f-b909-46f2-97c9-bb0850710971\",\"uiCulture\":\"en-US\"}");
//Response processing
public void OnMapObjectsDownload(IAsyncResult iar) { MapCanvasInfo[] response; try { response = ((WmsWebService)iar.AsyncState).EndGetMapCanvases(iar); //Remove all map canvases from requested layers RemoveAllMapCanvasesFromLayers(((WmsWebService)iar.AsyncState).RequestLayers); _lastRequestGeographicBounds = ((WmsWebService)iar.AsyncState).RequestGeoBounds; } catch (Exception e) { _map.tb.Text = "Exc:" + e.ToString(); return; }...
//Class for wrapper
public class WmsWebService : SoapHttpClientProtocol { private Rect _requestGeoBounds; public Rect RequestGeoBounds { get { return _requestGeoBounds; } set { _requestGeoBounds = value; } } private Rect _requestControlBounds; public Rect RequestControlBounds { get { return _requestControlBounds; } set { _requestControlBounds = value; } } private string _requestLayers; public string RequestLayers { get { return _requestLayers; } set { _requestLayers = value; } } // Methods public WmsWebService() { base.Url = "http://localhost/geo/wms.asmx"; } public IAsyncResult BeginGetMapCanvases(double minX, double minY, double maxX, double maxY, double width, double height, string layers, string uiCulture, AsyncCallback callback, object asyncState) { TestSoapHttpClientProtocol.ServiceParameter[] parameters = new TestSoapHttpClientProtocol.ServiceParameter[] { new TestSoapHttpClientProtocol.ServiceParameter("minLat", minX), new TestSoapHttpClientProtocol.ServiceParameter("minLng", minY), new TestSoapHttpClientProtocol.ServiceParameter("maxLat", maxX), new TestSoapHttpClientProtocol.ServiceParameter("maxLng", maxY), new TestSoapHttpClientProtocol.ServiceParameter("controlWidth", width), new TestSoapHttpClientProtocol.ServiceParameter("controlHeight", height), new TestSoapHttpClientProtocol.ServiceParameter("layers", layers), new TestSoapHttpClientProtocol.ServiceParameter("uiCulture", uiCulture), }; return base.BeginInvoke("GetMapCanvases", parameters, typeof(MapCanvasInfo[]), callback, asyncState); } public MapCanvasInfo[] EndGetMapCanvases(IAsyncResult asyncResult) { object[] results = base.EndInvoke(asyncResult); return ((MapCanvasInfo[])(results[0])); } public MapCanvasInfo[] GetMapCanvases(double minX, double minY, double maxX, double maxY, double width, double height, string layers, string uiCulture) { TestSoapHttpClientProtocol.ServiceParameter[] parameters = new TestSoapHttpClientProtocol.ServiceParameter[] { new TestSoapHttpClientProtocol.ServiceParameter("minLat", minX), new TestSoapHttpClientProtocol.ServiceParameter("minLng", minY), new TestSoapHttpClientProtocol.ServiceParameter("maxLat", maxX), new TestSoapHttpClientProtocol.ServiceParameter("maxLng", maxY), new TestSoapHttpClientProtocol.ServiceParameter("controlWidth", width), new TestSoapHttpClientProtocol.ServiceParameter("controlHeight", height), new TestSoapHttpClientProtocol.ServiceParameter("layers", layers), new TestSoapHttpClientProtocol.ServiceParameter("uiCulture", uiCulture), }; object[] results = base.Invoke("GetMapCanvases", parameters, typeof(MapCanvasInfo[])); return ((MapCanvasInfo[])(results[0])); } } public struct MapCanvasInfo { //public MapObject(Guid layerId, Guid id, string name, string description, string xaml) //{ // _layerId = layerId; // _id = id; // _name = name; // _description = description; // _xaml = xaml; //} private Guid _layerId; public string LayerId { get { return _layerId.ToString(); } set { _layerId = new Guid(value); } } private Guid _id; public string Id { get { return _id.ToString(); } set { _id = new Guid(value); } } private string _xaml; public string Xaml { get { return _xaml; } set { _xaml = value; } } }
07-10-2007 9:23 AM |
This problem is reproduced also with direct BrowserHttpWebRequest use. The problem is not only with web services but also with static content (for example simple xml-file).
<b>Using of Downloader class resolves the problem</b>. It works correctly in two cases: in local and remote servers. What does it mean?
luisabreu
Participant
1676 points
612 Posts
07-10-2007 6:20 PM |
hello.
the browserhttperbrequest can only make local web services calls. trying to perform a cross-domain call results in getting a cross-domain exception. i'm not sure on what can be happening here...do you have asp.net ajax installed on your server?
shoemaker3
2 points
1 Posts
07-14-2007 2:47 PM |
I've got the same error, when i try to run Silverlight page and web service on remote server. Have you managed to solve that problem?
Regards,
Mark
pkellner
204 points
147 Posts
07-14-2007 11:08 PM |
I've done a lot of testing around the webservice api and found lots of bugs. the only thing that worked for me was follow the examples on the web site and return just structures of string[] type data. I lost a lot of hours trying to debug failed web service connections so my advice is don't loose as many hours as I have. I'm hoping it works much better in the next pre-release which I've heard is september-ish.
07-16-2007 11:31 PM |
After non successful attempt with I decided to workaround. In my situation I can not use neither SoapHttpClientProtocol nor BrowserHttpWebRequest because the same error occurred. The possible problem is automatic archived content of server response that is not recognized in above classes.
The workaround is to write ashx-hanler on server side and in Silverlight to use Downloader Xaml class. But all the parameters you should pass through Url. Please, note, that you can use JSON serialization to simplify object creation.
Example:
~/wms/wms.ashx
....
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json";
...
//Some call to return some objects
MapCanvasInfo[] mcis = GetMapCanvases(minLat, minLng, maxLat, maxLng, controlWidth, controlHeight, layers, uiCulture);
//Use using System.Web.Script.Serialization; before
JavaScriptSerializer serializer = new JavaScriptSerializer(); context.Response.Write(serializer.Serialize(mcis));
}
Silverlight call:
// Async call
Downloader downloader = new Downloader(); downloader.Completed += OnMapObjectsDownload; downloader.Open("GET", new Uri(GetWmsUrl(kvp.Key, geoBounds, controlBounds, kvp.Value, Thread.CurrentThread.CurrentUICulture.Name), UriKind.Relative), true); downloader.Send();
//Async response hanler
public void OnMapObjectsDownload(object o, EventArgs eventArgs) { JavaScriptSerializer serializer = new JavaScriptSerializer(); Downloader downloader = (Downloader)o; response = serializer.Deserialize<MapCanvasInfo[]>(downloader.GetResponseText(""));....
Best regards
Russss
18 points
7 Posts
08-06-2007 12:00 PM |
I also have the same "an item with the same key has already been added" when calling my web service if I develop using IIS (Windows XP - I haven't tried the Win 2003 Server yet). If I switch to the Visual Studio local server using static port numbers, everything works fine.
Russ
PerumalR
14 points
12 Posts
09-06-2007 4:44 AM |
Can anyone please provide me full sample code (dummi-code) for calling web service in silverlight projects.
I am starving with the problem of web service in silverlight.
I am getting JSON_IllegalPrimitive error at runtime when calling the web method (that is a ScriptMethod).
Thank you,
swarren
10-09-2007 1:07 PM |
I had a SL1.1 alpha refresh issue with the same error, and found that -- for me at least -- the data I was trying to recieve from my web service method was exceeding the maximum length supported by the JSON script serializer. I found this blog entry helpful in diagnosing the problem.
http://sartorial.spaces.live.com/blog/cns!AE52F94DB08EC0AB!118.entry
gabouy
219 points
45 Posts
10-16-2007 10:42 AM |
I'm experiencing the same issue when trying to invoke json webservices asynchronously, that is, exception with Error invoking service. message, at System.Windows.Browser.Net.SoapHttpClientProtocol.EndInvoke(IAsyncResult asyncResult)
But only when trying to run within SharePoint.
I'm sniffing the requests/responses and the response to the web service invocation is well formed json, but still it breaks the infamous exception.
Any idea?
mvmsastry
8 points
6 Posts
10-17-2007 6:00 PM |
I had the same problem (an item with same key has already been added error) when I tried to access a handler from SilverLight 1.1 code behind class. I cleared all the Context.Response object's Content and Header in my handler class which worked for me. Hope this helps you.
Thanks,Venu
MrCyprom
256 points
112 Posts
10-18-2007 6:05 AM |
I have the same issue. I made a simple test. In my WS i read a file (bigger than 100ko) and in my SL via an asynch call i try to get the result string. But it fails with the same exeption/stack trace. It seems that it's a problem with JSON serialization which is limited.
Does someone knows how to grow the size of the string to serialize ?
Regards.
einar
20 points
11-06-2007 12:06 PM |
I had the exact some problem - getting the same exception and stack trace.
I tried both with web services using a VS generated proxy class and with a "direct" BrowserHttpWebRequest.
Using Fiddler, I saw that the response from IIS (6.0 on Win2003 with .net 2 and 3.5 beta installed) had duplicated "X-Powered-By" headers:...X-Powered-By: ASP.NET 2.0X-Powered-By: ASP.NET...
(I guess that the latter header was added by the .Net 3.5 beta setup)
Then, the error made perfect sense. Silverlight was trying to add the duplicated header values to it's header values dictionary and that operation threw an exception. Shouldn't Silverlight catch this exception and just ignore one of the headers?
It's quite easy to remove these headers in IIS. Just click Properties on your web site. Go to the HTTP Headers tab and remove one of the custom headers.
After removing one of the headers in IIS both my web service calls and BrowserHttpWebRequests work!
This might solve this problem for some of you.
Cheers,Einar Hauksson