<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>With Great Power comes Great Response.write("Ability") : CrossDomain</title><link>http://blogs.msdn.com/phaniraj/archive/tags/CrossDomain/default.aspx</link><description>Tags: CrossDomain</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>ADO.NET Data Services SL Client Library - XDomain and Out Of Browser</title><link>http://blogs.msdn.com/phaniraj/archive/2009/09/04/ado-net-data-services-sl-client-library-xdomain-and-out-of-browser.aspx</link><pubDate>Fri, 04 Sep 2009 05:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9891182</guid><dc:creator>PhaniRajuYN</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/phaniraj/comments/9891182.aspx</comments><wfw:commentRss>http://blogs.msdn.com/phaniraj/commentrss.aspx?PostID=9891182</wfw:commentRss><description>&lt;P&gt;The first in my two part blog posts about&amp;nbsp;the new Silverlight Client Library we released in CTP2 is up on the team blog.&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=entrylistheader&gt;&lt;A id=bp___v___r___postlist___EntryItems_ctl01_PostTitle href="http://blogs.msdn.com/astoriateam/archive/2009/09/03/using-the-ado-net-data-services-silverlight-client-in-x-domain-and-out-of-browser-scenarios-i.aspx" mce_href="http://blogs.msdn.com/astoriateam/archive/2009/09/03/using-the-ado-net-data-services-silverlight-client-in-x-domain-and-out-of-browser-scenarios-i.aspx"&gt;Using the ADO.NET Data Services Silverlight client in x-domain and out of browser scenarios – I&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=entrylistheader&gt;Stay tuned to the team blog for the the second&amp;nbsp; part which talks about using the client library X-Domain and Out Of Browser&lt;BR&gt;to access a data service which is authenticated with Forms Authentication.&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9891182" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Astoria/default.aspx">Astoria</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/CrossDomain/default.aspx">CrossDomain</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/DataServiceContext/default.aspx">DataServiceContext</category></item><item><title>Accessing Cross-Domain ADO.NET Data Services from the Silverlight Client Library</title><link>http://blogs.msdn.com/phaniraj/archive/2008/10/21/accessing-cross-domain-ado-net-data-services-from-the-silverlight-client-library.aspx</link><pubDate>Tue, 21 Oct 2008 05:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9008548</guid><dc:creator>PhaniRajuYN</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/phaniraj/comments/9008548.aspx</comments><wfw:commentRss>http://blogs.msdn.com/phaniraj/commentrss.aspx?PostID=9008548</wfw:commentRss><description>&lt;p&gt;When we built the client library for Silverlight in Silverlight 2.0 , the underlying Network Stack provided by Silverlight core libraries    &lt;br /&gt;was very limited in terms of the Status Codes and Request Verbs that it supported.Our options were :     &lt;br /&gt;a) Use the System.Net libraries provided by the agClr and give a limited subset of the functionality available on the desktop.     &lt;br /&gt;b) Cook up our own Network stack and provide all the functionality provided on the desktop , in Silverlight.&lt;/p&gt;  &lt;p&gt;We went with Option (b) and built a network stack that uses the browser’s underlying XmlHttpRequest object.    &lt;br /&gt;This means that the &lt;a href="http://msdn.microsoft.com/en-us/library/cc197955(VS.95).aspx"&gt;cross domain access available in Silverlight networking stack&lt;/a&gt; is not available in our networking stack.     &lt;br /&gt;    &lt;br /&gt;Forums user Ben Hayat asked a &lt;a href="http://forums.microsoft.com/Forums/ShowPost.aspx?PostID=3995200&amp;amp;SiteID=1"&gt;question&lt;/a&gt; about the guidelines for Cross-Domain access using our Silverlight Client Library .     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Now , this is not supported out of the box . But ,you can use the same solution as you would for an Ajax application ,    &lt;br /&gt;which is building&amp;#160; a server-side proxy that talks to the remote service and is completely transparent to the client library.     &lt;br /&gt;    &lt;br /&gt;Below is a sample Server-Side proxy that would help one talk to Data Services hosted on a different domain than the Silverlight application.     &lt;br /&gt;    &lt;br /&gt;There are a couple of issues that a Server-Side proxy needs to solve for an ADO.NET Data Service ,     &lt;br /&gt;&amp;#160; a) Appear totally transparent     &lt;br /&gt;&amp;#160; b) Resolve Identities of resources to appear as they would from the Proxy service and not the original Data Service.     &lt;br /&gt;    &lt;br /&gt;Issue a) is easy , Issue b) is a little more involved.     &lt;br /&gt;    &lt;br /&gt;When the client library receives a Payload back as part of a query ,we construct the entity’s identity based on the following rules,     &lt;br /&gt;ex: If you browse to &lt;a href="http://ServiceEndpoint/Northwind.svc/"&gt;&lt;strong&gt;http://ServiceEndpoint&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;/Northwind.svc/Customers('ALFKI') ,&lt;/strong&gt;the response looks like this :&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&amp;#160;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;entry&lt;/span&gt; &lt;strong&gt;&lt;font color="#800040"&gt;&lt;span class="attr"&gt;xml:base&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://ServiceEndpointNorthwind.svc/&amp;quot;&lt;/span&gt; 
&lt;/font&gt;&lt;/strong&gt;       &lt;span class="attr"&gt;xmlns:d&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices&amp;quot;&lt;/span&gt; 
       &lt;span class="attr"&gt;xmlns:m&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/ado/2007/08/dataservices/metadata&amp;quot;&lt;/span&gt; 
       &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://www.w3.org/2005/Atom&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;strong&gt;&lt;font color="#800000"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;link&lt;/span&gt; &lt;span class="attr"&gt;rel&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;edit&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;title&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customers&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;href&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Customers('ALFKI')&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt; 
&lt;/font&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;http://ServiceEndpointNorthwind.svc/Customers('ALFKI')&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;id&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/strong&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;content&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;application/xml&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;m:properties&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
     &lt;strong&gt;Properties go here &lt;br /&gt;&lt;/strong&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;m:properties&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;content&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;entry&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;When we get the above payload back as a response , we&amp;#160; create&amp;#160; a Customer object and its identity is the value of the &amp;lt;id&amp;gt; field in the payload. 
  &lt;br /&gt;When you make any edits to this Customer object, &lt;u&gt;the edits are sent to the URI specified in the &amp;lt;link rel=”edit”&amp;gt; value of the payload. 
    &lt;br /&gt;&lt;/u&gt;Which means that the proxy server has to modify the payload it recieves from the real Data Service to make sure that all links , Id and Edit 

  &lt;br /&gt;point to the proxy Data Service and not the real Data Service. The easiest way to do this is to do a String.Replace replacing all occurrences of the 

  &lt;br /&gt;the original Service end point with the Proxy Service end point in the payload.&lt;/p&gt;

&lt;p&gt;Without further stalling&amp;#160; , here is the&amp;#160; &lt;a href="http://blogs.msdn.com/phaniraj/attachment/9008548.ashx"&gt;Proxy Handler&lt;/a&gt;. &lt;/p&gt;

&lt;h5&gt;A couple of words of caution so that I can keep my job, &lt;/h5&gt;

&lt;ol&gt;
  &lt;li&gt;This is only a sample implementation of what a proxy &lt;strong&gt;could&lt;/strong&gt;&lt;em&gt;&lt;/em&gt; look like. &lt;/li&gt;

  &lt;li&gt;Do &lt;strong&gt;NOT&lt;/strong&gt; use this in your production applications. &lt;/li&gt;

  &lt;li&gt;This is &lt;strong&gt;NOT&lt;/strong&gt; an official solution provided by the ADO.NET Data Services team , 

    &lt;br /&gt;this is just me illustrating how to write a proxy. &lt;/li&gt;

  &lt;li&gt;If you download this sample , it means that you understand these rules . &lt;/li&gt;

  &lt;li&gt;For any problems with this sample , leave a comment on my blog , 
    &lt;br /&gt;the ADO.NET Data Services Forums is &lt;strong&gt;NOT &lt;/strong&gt;the right place for any issues with this proxy. &lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;Running the sample&amp;#160; &lt;/h5&gt;

&lt;ol&gt;
  &lt;li&gt;Download the sources from the link above. &lt;/li&gt;

  &lt;li&gt;Open the solution in Visual Studio 2008 and hit F5. &lt;/li&gt;

  &lt;li&gt;You should see the Silverlight application come up and 
    &lt;br /&gt;get the data from the Data Service hosted in a different application. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Cross Domain Proxy is a generic AShx handler which receives the requests from the Client library and forwards them onto the 
  &lt;br /&gt;original Data Service , gets the response , changes the ID and Edit Links and returns the response to the Silverlight Client . &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Client Sends request to Proxy Handler &lt;/li&gt;

  &lt;li&gt;Proxy Handler sends the request data to the Original Data Service &lt;/li&gt;

  &lt;li&gt;Data Service responds with response &lt;/li&gt;

  &lt;li&gt;Proxy handler reads response and changes the ID and EDIT links in the payload &lt;/li&gt;

  &lt;li&gt;In case of POST , the Proxy Handler changes the response “Location” Header 
    &lt;br /&gt;so that the identity points to the Proxy Handler and not the Data Service. &lt;/li&gt;

  &lt;li&gt;Proxy handler returns the response data to the client . &lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;How do I use this in my application ?&lt;/h5&gt;

&lt;br /&gt;

&lt;p&gt;The source available for download contains “CrossDomainHandlerBase” which is the class that contains the functionality required 
  &lt;br /&gt;for recieving requests from clients and forwarding the requests onto the Data Service. &lt;/p&gt;

&lt;h5&gt;Setting up the Server-Side Proxy &lt;/h5&gt;

&lt;br /&gt;

&lt;ol&gt;
  &lt;li&gt;Add a Generic ASHX Handler to the application that contains the Web page hosting the Silverlight application , 
    &lt;ol&gt;
      &lt;li&gt;Right-Click on Project &lt;/li&gt;

      &lt;li&gt;Add –&amp;gt; New Item –&amp;gt; Generic Handler 
        &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/AccessingCrossDomainDataServicesfromtheS_C375/image_2.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="449" alt="image" src="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/AccessingCrossDomainDataServicesfromtheS_C375/image_thumb.png" width="644" border="0" /&gt;&lt;/a&gt; &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; 2. This will create a Generic Handler which implements IHttpHandler.&lt;/p&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; 3. Remove the code stubs for IHttpHandler methods and properties . 
  &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; 4. Replace IHttpHandler with “CrossDomainHandlerBase”.&amp;#160; &lt;br /&gt;&amp;#160;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;    [WebService(Namespace = &lt;span class="str"&gt;&amp;quot;http://tempuri.org/&amp;quot;&lt;/span&gt;)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CrossDomainHandler : CrossDomainHandlerBase
    {
        &lt;span class="kwrd"&gt;public&lt;/span&gt; CrossDomainHandler()
            : &lt;span class="kwrd"&gt;base&lt;/span&gt;()
        {
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.DataServiceURI = &lt;span class="str"&gt;&amp;quot;URI FOR THE DATA SERVICE&amp;quot;&lt;/span&gt;;
            &lt;span class="kwrd"&gt;this&lt;/span&gt;.ProxyServiceURI = &lt;span class="str"&gt;&amp;quot;URI FOR THE PROXY SERVICE&amp;quot;&lt;/span&gt;;
        }
     }&lt;/pre&gt;

&lt;p&gt;&amp;#160;&amp;#160;&amp;#160; 4.From this point on , for all purposes , the Proxy Handler service IS the Data Service&lt;/p&gt;

&lt;h5&gt;Setting up the Silverlight application for Cross-Domain access&lt;/h5&gt;

&lt;ol&gt;
  &lt;li&gt;Right-Click on “References” in the Project &lt;/li&gt;

  &lt;li&gt;Select &amp;quot;Add Service Reference&amp;quot; &lt;/li&gt;

  &lt;li&gt;In the “Address” field , enter the URI for the proxy service , &lt;/li&gt;

  &lt;li&gt;&lt;a href="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/AccessingCrossDomainDataServicesfromtheS_C375/image_6.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="484" alt="image" src="http://blogs.msdn.com/blogfiles/phaniraj/WindowsLiveWriter/AccessingCrossDomainDataServicesfromtheS_C375/image_thumb_2.png" width="519" border="0" /&gt;&lt;/a&gt; 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;In case of the sample solution provided, the Provider is called “DataProvider”, hence 
    &lt;br /&gt;the code-gen produces the code with a type “DataProvider” derived from DataServiceContext. &lt;/li&gt;

  &lt;li&gt;When you instantiate the Context in the Silverlight application , always specify the Base URI 
    &lt;br /&gt;as the Proxy Handler’s URI, 

    &lt;br /&gt;

    &lt;pre class="csharpcode"&gt;DataProvider dataProvider = &lt;span class="kwrd"&gt;new&lt;/span&gt; DataProvider(
                &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;RELATIVE URI FOR THE PROXY SERVICE&amp;quot;&lt;/span&gt;, 
                            UriKind.RelativeOrAbsolute)
                  );&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;This is the most important step of all &lt;strong&gt;, Party on !!&lt;/strong&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To do in the near future :&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Convert the Handler to be an IHttpAsyncHandler so that we dont block on waiting for the response from the Data Service &lt;/li&gt;

  &lt;li&gt;Provide better mechanisms for Identity Resolution other than String.Replace . &lt;/li&gt;

  &lt;li&gt;Write a better demo app than something which binds all the parts of my name to a Grid. &lt;/li&gt;

  &lt;li&gt;Your suggestion goes here . &lt;/li&gt;
&lt;/ol&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9008548" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/phaniraj/attachment/9008548.ashx" length="2976444" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Prototype/default.aspx">Prototype</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Astoria/default.aspx">Astoria</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/CrossDomain/default.aspx">CrossDomain</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Demo/default.aspx">Demo</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Documentation/default.aspx">Documentation</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/DataServiceContext/default.aspx">DataServiceContext</category></item></channel></rss>