GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.servicestack.net — Silverlight Client Silverlight ClientThe recommended way to get Silverlight builds of ServiceStack client libraries is from NuGet: These binaries are custom builds of the full ServiceStack Service Clients providing JSON, JSV and XML ServiceClients. Requires PCL support in v4.x ServiceStack.Client NuGet package.INFO Due to restrictions in Silverlight only the Async operations are supported. HelloMobileSee the HelloMobile project for more info on ServiceStack's Silverlight and PCL client support including showing re-use of shared libraries between each of the different mobile platforms. Building Silverlight JSON Service Client from ScratchAlthough it is recommended to use the published binaries above, for illustrative purposes, this section below walks through creating a Silverlight client from scratch. This example below shows how to create a JSON ServiceClient. Note: The code below has been tested in Silverlight 5. It should work fine in other versions of Silverlight as well. Please be aware of the following considerations:Uses HttpWebRequest with ClientHttp handlingThe Silverlight REST Services Quickstart describes the different mechanisms of how http calls can be made from Silverlight. There is the easier-but-heavier WebClient implementation, and the lighter-but-harder HttpWebRequest implementation. The code below uses the HttpWebRequest implementation, primarily because you cannot manipulate cookies with the WebClient version, but also because it provides better hooks for serializing and deserializing the JSON we want to send to our ServiceStack service. It also uses the ClientHttp handling instead of BrowserHttp handling. See the differences here. You may need to put a clientaccesspolicy.xml file in the root of your host. It depends on the domains of the host and client, and the trust level of the particular Silverlight client application (differs between out-of-browser and between Silverlight versions). See MSDN documentation for more info. Is Cookie-AwareIn the code below, you will see how cookies can be enabled or disabled. Because we are using ClientHttp handling, when cookies are enabled, we have to create a custom CookieContainer. In order to share these cookies with the browser, we are populating from HtmlPage.Document.Cookies before the request, and then setting the cookies back to the browser after getting a response. This technique allows things like ServiceStack's built-in authentication to work, even if you authenticate in the browser before launching your Silverlight app. If you don't want to share cookies with the browser, you can set EnableCookies = false, or you can modify the code to persist the CookieContainer in some other way. Talks only JSONThe request and response streams are assumed to be JSON, and the Accept and Content-Type headers are set accordingly. If you want to implement xml or jsv, you will have to adjust the headers and the content appropriately. Using the DataContractJsonSerializerThe only JSON serializer that comes with Silverlight is the DataContractJsonSerializer. While not as performant as ServiceStack's serializers, performance on the client usually tends not to be as important as on the server (due to load). Note that if you decide to apply a [DataContract] attribute to your DTO that you must specifically mark all members that you want to serialize with [DataMember]. If you leave them off, all public members will be serialized. The code below uses this serializer by default. If you use it, be aware that there is a bug in the DataContractJsonSerializer related to handling of dates that are not of type DateTimeKind.Local. Read more here Using the ServiceStack JsonSerializerThe Silverlight build of ServiceStack.Text (the one that comes with the ServiceStack.Client.Silverlight) is easy to implement. There is code in the below sample that is commented out, showing how to do this. Simply uncomment the code and remove the DCJS implementation, then reference ServiceStack.Text and you now have a faster serializer (one without the date bug). Example UsageSetting up your DTOsThe regular ServiceStack example DTOs should work:
Your DTO projects will need to be accessible from both Silverlight and your other .Net projects. There are two ways to accomplish this: Shared ProjectsYou can use a set of shared project for porting your DTOs to Silverlight. One should be a regular .Net Class Library (perhaps named MySilverlightApp.DTOs), and another should be a Silverlight Class Library (perhaps named MySilverlightApp.DTOs.Silverlight). You can use the project linking technique documented here. You may also want to use the Visual Studio Project Linker add-in, which is available here and documented here. Portable Class LibrariesYou can put your DTOs in a single project that is set up as a Portable Class Library. This is a much easier solution, but you will not be able to take on any dependencies. This means that you cannot use the [Route] attribute on your DTOs with this approach. Instead, use one of the other ways to register services, such as The ServiceClient ImplementationHere is the actual code that implements the ServiceClient. If you've followed all of the above instructions, you should be able to use it easily in your application. Simply change the namespace as you desire.
Community ResourcesOn This Page
|
| You are visitor 01402809 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |