Quantcast
Channel: Silverlight 5 forum
Viewing all articles
Browse latest Browse all 1083

HTTPs and Silverlight

$
0
0

Hi,

I have an example console application which hosts a service and a policy access file. This is the code:

    public class SelfHostedServiceWithSilverlightPolicy
    {
        [ServiceContract]
        public interface ITest
        {
            [OperationContract]
            string Echo(string text);
        }
        [ServiceContract]
        public interface IPolicyRetriever
        {
            [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
            Stream GetSilverlightPolicy();
            [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
            Stream GetFlashPolicy();
        }
        public class Service : ITest, IPolicyRetriever
        {
            public string Echo(string text) { return text; }
            Stream StringToStream(string result)
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
                return new MemoryStream(Encoding.UTF8.GetBytes(result));
            }
            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><resource path=""/"" include-subpaths=""true""/></grant-to></policy></cross-domain-access></access-policy>";
                return StringToStream(result);
            }
            public Stream GetFlashPolicy()
            {
                string result = @"<?xml version=""1.0""?><!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd""><cross-domain-policy><allow-access-from domain=""*"" /></cross-domain-policy>";

                return StringToStream(result);
            }
        }

        public static void Main()
        {
            bool isHTTP = false;
            if (isHTTP)
            {
                string baseAddress = "http://" + Environment.MachineName;

                ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
                host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "basic");
                host.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);
                host.Open();
                Console.WriteLine("Host opened");

                Console.WriteLine("Press ENTER to close");
                Console.ReadLine();
                host.Close();
            }
            else
            {
                string baseAddress1 = "https://" + Environment.MachineName;

                ServiceHost host1 = new ServiceHost(typeof(Service), new Uri(baseAddress1));
                host1.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(WebHttpSecurityMode.Transport), "basic");
                host1.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(WebHttpSecurityMode.Transport), "").Behaviors.Add(new WebHttpBehavior());
                ServiceMetadataBehavior smb1 = new ServiceMetadataBehavior();
                smb1.HttpsGetEnabled = true;
                host1.Description.Behaviors.Add(smb1);
                host1.Open();

                string baseAddress2 = "http://" + Environment.MachineName;

                ServiceHost host2 = new ServiceHost(typeof(Service), new Uri(baseAddress2));
                host2.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "basic");
                host2.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
                ServiceMetadataBehavior smb2 = new ServiceMetadataBehavior();
                smb2.HttpGetEnabled = true;
                host2.Description.Behaviors.Add(smb2);
                host2.Open();

                Console.WriteLine("Host opened");

                Console.WriteLine("Press ENTER to close");
                Console.ReadLine();
                host1.Close();
            }
        }
    }

If I set isHTTP to true then HTTP is used and I can call this from a Silverlight application as follows:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("http://devsvr6/ITest");
m_silverlightServiceClient = new SilverlightServiceClient(binding, endpointAddress);
m_silverlightServiceClient.BeginGetEquationError(m_credentials, "a|b|c|CALC", "test", "2", EndGetEquationError, null);

doing this causes the GetSilverlightPolicy() in the console application to be called which is what I would expect.

If I set isHTTP to false then HTTPS is used and I can call this from a Silverlight application as follows:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
EndpointAddress endpointAddress = new EndpointAddress("https://devsvr6/ITest");
m_silverlightServiceClient = new SilverlightServiceClient(binding, endpointAddress);

m_silverlightServiceClient.BeginGetEquationError(m_credentials, "a|b|c|CALC", "test", "2", EndGetEquationError, null);

This does not call GetSilverlightPolicy() and I have no idea why. I get the Silverlight cross domain error which means that the policy file cannot be found.

Does anyone know why this does not work?

Thanks

Ian


Viewing all articles
Browse latest Browse all 1083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>