none
MS Knowledge Base: Bagaimana menjalankan WCF Service melalui TCP transport pada Microsoft Silverlight 4? (Part 3) RRS feed

  • Diskusi Umum

  • 4.     Jalankan kode berikut untuk membuka file MainPage.xaml.cs, dan inisialisasi proxy WCF dalam Loaded event: 

     

      public partial class MainPage : UserControl,IWeatherServiceCallback
        {
            public MainPage()
            {
                InitializeComponent();
                Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
     
            bool _subscribed;
            WeatherServiceClient _client;
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                _client = new WeatherServiceClient(
                    new System.ServiceModel.InstanceContext(this));
                _client.SubscribeCompleted += _client_SubscribeCompleted;
                _client.UnSubscribeCompleted += _client_UnSubscribeCompleted;
            }
     
            void _client_UnSubscribeCompleted(object sender, 
            System.ComponentModel.AsyncCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    _subscribed = false;
                    btnSubscribe.Content = "Subscribe weather report";
                    tbInfo.Text = "";
                }else
                    tbInfo.Text = "Unable to connect to service.";
                btnSubscribe.IsEnabled = true;
            }
     
            void _client_SubscribeCompleted(object sender, 
            System.ComponentModel.AsyncCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    _subscribed = true;
                    btnSubscribe.Content = "UnSubscribe weather report";
                    tbInfo.Text = "";
                }
                else
                    tbInfo.Text="Unable to connect to service.";
                btnSubscribe.IsEnabled = true;
            }
     
            // Display report when callback channel get called.
            public void WeatherReport(string report)
            {
                lbWeather.Items.Insert(
                    0,
                    new ListBoxItem
                    {
                        Content = string.Format("{0} {1}",DateTime.Now, report)
                    });
            }
        }

     

    5.     Jalankan kode berikut untuk menambahkan event handler untuk button click event:

     

        private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (!_subscribed)
                {
                    _client.SubscribeAsync();
                }
                else
                {
                    _client.UnSubscribeAsync();
                }
                btnSubscribe.IsEnabled = false;
            }

     

    Langkah 3

     

    Menjalankan file cross domain policy.

     

    1.     Buat sebuah xml file clientaccesspolicy.xml, gunakan kode berikut untuk mengkonfigurasi isi didalamnya:

     

    <access-policy>
      <cross-domain-access>
        <policy>
          <allow-from>
            <domain uri="*"/>
          </allow-from>
          <grant-to>
            <socket-resource port="4502-4506" protocol="tcp" />
          </grant-to>
        </policy>
      </cross-domain-access>
    </access-policy>

     

    Catatan File ini akan memberikan izin kepada klien Silverlight yang berasal dari setiap domain untuk mengakses port server 4502 - 4506.

    2.     Cari tahu letak direktori root fisik (secara default, C: \ inetpub \ wwwroot jika anda menggunakan IIS) dari server web site, tempatkan policy file alamat tersebut. Untuk memverifikasi proses, browse http://localhost/clientaccesspolicy.xml dan periksa apakah anda bisa melihat isi dari policy xml.

     

     

    Kategori Teknologi

     

    ·            Silverlight

     

     

    Bahasa Pemograman

    Sampel kode ini memuat bahasa pemograman dibawah ini:

    Language

    Project Name

    Visual C#

    CSSL4WCFNetTcp

    Visual Basic.NET

    VBSL4WCFNetTcp

     

    Prasyarat

     

    Tags

    ·            WCF, Silverlight

     

    Referensi

    http://support.microsoft.com/kb/2425652/en-us


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Selasa, 12 April 2011 03.15
    Moderator