c# - Need help to write web api wrapper for Silverlight project -
please bear me new silverlight. need write web api wrapper (i've named webclientwrapper
) supposed consume rest services. project uses silverlight 5. facing many problems while writing such wrapper. there lot of examples demonstrating rest service consumption in c#. unfortunately none of them worked me. it's been challenge me done need. below requirements:
1) ui should not freeze while make request,
2) calling methods webclientwrapper
should easier possible.
3) .net framework version of project should not changed.
i tried following things far:
1) use of httpclient. referred link: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client. problem approach cannot call readasasync method. calling method requires change in framework (i.e. replacing dlls or changing framework version) not feasible.
2) use of webclient. http://www.kastory.net/index.php?option=com_content&view=article&id=25:rest-web-service-in-c-with-silverlight-and-asp-net-web-api&catid=32:web-services&itemid=130.
problem (not sure!) have make changes in way call methods webclientwrapper. trying accomplish calling method webclientwrapper this:
var appointments = webclientwrapper.get<appointments>(new dictionary<string, string>() { {"hospitalid", "19465654546"} }, "appointment");
below code snippet tried in webclientwrapper.
private const string baseurl = "http://localhost:63455/api"; private static webclient getclient() { int leadingint = new random().next(10000, 99999); int trailingint = new random().next(1000, 9999); string date = datetime.now.tostring("ddhhmmmmssmmyyyyyss"); string ticketstring = string.format("{0}{1}{2}", leadingint, date, trailingint); var client = new webclient(); client.headers["accept"] = ticketstring; client.headers["useragent"] = "receptionistapp"; return client; } private static void downloadcompletionhandler<t>(object sender, downloadstringcompletedeventargs e) { encoding messageencoding = encoding.utf8; var serializer = new datacontractjsonserializer(typeof (t)); var memorystream = new memorystream(messageencoding.getbytes(e.result)); var objecttoreturn = (t) serializer.readobject(memorystream); } public static t get<t>(dictionary<string, string> paramdictionary, string controller) { string absoluteurl = baseurl + controller + "?"; absoluteurl = paramdictionary.aggregate(absoluteurl, (current, keyvaluepair) => current + (keyvaluepair.key + "=" + keyvaluepair.value + "&")); absoluteurl = absoluteurl.trimend('&'); webclient client = getclient(); client.downloadstringcompleted += downloadcompletionhandler<t>; client.downloadstringasync(new uri(absoluteurl)); }
below things want mention regarding code above:
1) obvious, compiler throws error method get<t>
since i've not returned object of type t
. how shall object downloadstringasync
? know can use downloadstringtaskasync
. not available current framework. changes have make in code appointments
shown in get<appointments>
method call?
2) downloadcompletionhandler<t>
bound return void
want return objecttoreturn
shown in code.
help appreciated. new code snippets fulfill requirements welcome.
restsharp helped me instead of webclient.
Comments
Post a Comment