http://blogs.msdn.com/b/carlosfigueira/archive/2010/07/25/enabling-cross-domain-calls-for-sl-apps-on-self-hosted-tcp-services.aspx
http://forums.silverlight.net/forums/p/113172/259067.aspx
Example which I tried,
(Download)
WCF Service :
File Name : IService1.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Runtime.Serialization;
5: using System.ServiceModel;
6: using System.Text;
7: using System.IO;
8: using System.ServiceModel.Description;
9: using System.ServiceModel.Web;
10:
11: namespace ConsoleApplication1
12: {
13: [ServiceContract]
14: public interface IService1
15: {
16: [OperationContract]
17: string GetData(int value);
18:
19: }
20: [ServiceContract]
21: public interface IPolicyRetriever
22: {
23: [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
24: Stream GetSilverlightPolicy();
25:
26: [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
27: Stream GetFlashPolicy();
28: }
29:
30: }
File Name : Service1.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Runtime.Serialization;
5: using System.ServiceModel;
6: using System.Text;
7: using System.IO;
8: using System.ServiceModel.Description;
9: using System.ServiceModel.Web;
10:
11: namespace ConsoleApplication1
12: {
13:
14: public class Service1 : IService1, IPolicyRetriever
15: {
16: public string GetData(int value)
17: {
18: return string.Format("You entered: {0}", value);
19: }
20:
21: public Stream GetSilverlightPolicy()
22: {
23: string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
24: <access-policy>
25: <cross-domain-access>
26: <policy>
27: <allow-from http-request-headers=""*"">
28: <domain uri=""*""/>
29: </allow-from>
30: <grant-to>
31: <resource path=""/"" include-subpaths=""true""/>
32: </grant-to>
33: </policy>
34: </cross-domain-access>
35: </access-policy>";
36: WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
37: return new MemoryStream(Encoding.UTF8.GetBytes(result));
38: }
39:
40: public Stream GetFlashPolicy()
41: {
42: string result = @"<?xml version=""1.0""?>
43: <!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
44: <cross-domain-policy>
45: <allow-access-from domain=""*"" />
46: </cross-domain-policy>";
47:
48: WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
49: return new MemoryStream(Encoding.UTF8.GetBytes(result));
50: }
51:
52:
53: }
54: }
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.ServiceModel;
6: using System.IO;
7: using System.ServiceModel.Description;
8: using System.ServiceModel.Web;
9:
10: namespace ConsoleApplication1
11: {
12: class Program
13: {
14: static void Main(string[] args)
15: {
16: string baseAddressHttp = "http://" + Environment.MachineName + ":8090";
17:
18: ServiceHost host = new ServiceHost(typeof(Service1), new Uri(baseAddressHttp));
19: host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "myService");
20: host.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
21:
22: ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
23: smb.HttpGetEnabled = true;
24: host.Description.Behaviors.Add(smb);
25:
26: host.Open();
27: Console.WriteLine("Host opened at " + baseAddressHttp);
28: Console.Write("Press ENTER to close");
29: Console.ReadLine();
30: host.Close();
31: }
32: }
33: }
Silverlight Code
File Name : MainPage.xaml
1: <UserControl x:Class="SilverlightApplication1.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400">
8:
9: <Grid x:Name="LayoutRoot" Background="White">
10: <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="72,40,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
11: <TextBox Height="23" HorizontalAlignment="Left" Margin="211,83,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
12: </Grid>
13: </UserControl>
File Name : MainPage.xaml.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Net;
5: using System.Windows;
6: using System.Windows.Controls;
7: using System.Windows.Documents;
8: using System.Windows.Input;
9: using System.Windows.Media;
10: using System.Windows.Media.Animation;
11: using System.Windows.Shapes;
12:
13: namespace SilverlightApplication1
14: {
15: public partial class MainPage : UserControl
16: {
17: public MainPage()
18: {
19: InitializeComponent();
20: }
21:
22: private void button1_Click(object sender, RoutedEventArgs e)
23: {
24: ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
25: sc.GetDataAsync(Int32.Parse(textBox2.Text));
26: sc.GetDataCompleted += new EventHandler<ServiceReference1.GetDataCompletedEventArgs>(sc_GetDataCompleted);
27: }
28:
29: void sc_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)
30: {
31: textBox2.Text = e.Result;
32: }
33: }
34: }
FileName : ServiceReferences.ClientConfig
1: <configuration>
2: <system.serviceModel>
3: <bindings>
4: <basicHttpBinding>
5: <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
6: maxReceivedMessageSize="2147483647">
7: <security mode="None" />
8: </binding>
9: </basicHttpBinding>
10: </bindings>
11: <client>
12: <endpoint address="http://win-cnzdnl3hxvy:8090/myService" binding="basicHttpBinding"
13: bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
14: name="BasicHttpBinding_IService1" />
15: </client>
16: </system.serviceModel>
17: </configuration>