This is a mirror of official site: http://jasper-net.blogspot.com/

Enabling cross-domain calls for SL apps on self-hosted TCP services

| Sunday, July 25, 2010
On a post a couple of years back, I showed a solution to enable cross-domain calls for Silverlight apps on self-hosted WCF services. In Silverlight 4, we added a new transport (net.tcp), which needs its own policy file as well (see more details at http://blogs.msdn.com/b/silverlightws/archive/2010/04/09/policy-file-for-nettcp.aspx). If you are running IIS (or some other web service) on port 80 the machine on which the TCP service is running, the easiest way to deploy the appropriate policy file is to simply copy the file to its root directory (in IIS it would be something like c:\inetpub\wwwroot), and you're ready to go.

If you don't have IIS, however, you can use a similar solution than the one I had for "normal" (i.e., HTTP-based) WCF services to serve the policy file for the TCP service. Essentially, the code will act as a web server and serve the "policy file" to SL apps when requested. The base address for the WCF REST service would be listening at port 80 on the machine, and its endpoint would be listening at the "" address. Here's the code:

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;

namespace SelfHostedTcpAndSilverlight
{
   [ServiceContract]
   public interface ITest
   {
       [OperationContract]
       string Echo(string text);
   }
   [ServiceContract]
   public interface ITcpPolicyRetriever
   {
       [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
       Stream GetSilverlightPolicy();
   }
   public class Service : ITest, ITcpPolicyRetriever
   {
       public string Echo(string text)
       {
           return text;
       }
       public Stream GetSilverlightPolicy()
       {
           string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
<access-policy>
   <cross-domain-access>
       <policy>
           <allow-from http-request-headers=""*"">
               <domain uri=""*""/>
           </allow-from>
           <grant-to>
               <socket-resource port=""4504"" protocol=""tcp"" />
           </grant-to>
       </policy>
   </cross-domain-access>
</access-policy>";
           WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
           return new MemoryStream(Encoding.UTF8.GetBytes(result));
       }
   }

Read more: Carlos' blog

Posted via email from .NET Info

0 comments: