Silverlight 動態訪問Webservice

            Webservice服務位於Silverlight服務端的ClientBin目錄下。

            Silverlight 客戶端調用Webservice服務時,首先需要添加服務引用,右鍵如下圖

                                      

            單擊Discover按鈕,自動查找到Webservice服務。

            在Silverlight後臺CS

                using SilverlightChart.EmployeesInfoServiceReference;

                添加動態綁定類

                     public class ServiceUtil
                     {
                         public static EmployeesInfoWebServiceSoapClient GetDynamicClient(string ClientIp)
                          {
                                     BasicHttpBinding binding = new BasicHttpBinding(Application.Current.Host.Source.Scheme.Equals("https",                                  StringComparison.InvariantCultureIgnoreCase) ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);

                                    binding.MaxReceivedMessageSize = int.MaxValue;

                                    binding.MaxBufferSize = int.MaxValue;

                                    EndpointAddress client = new EndpointAddress(GetHostUrl(ClientIp));

                                    return new EmployeesInfoWebServiceSoapClient(binding, client);
                          }

                        public static EmployeesInfoWebServiceSoapClient GetMyService()   //本地
                         {
                             BasicHttpBinding binding = new BasicHttpBinding(Application.Current.Host.Source.Scheme.Equals("https",  StringComparison.InvariantCultureIgnoreCase) ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);

                            binding.MaxReceivedMessageSize = int.MaxValue;

                            binding.MaxBufferSize = int.MaxValue;

                             EndpointAddress client = new EndpointAddress(new Uri(Application.Current.Host.Source, "../ClientBin/EmployeesInfoWebService.asmx"));

                             return new EmployeesInfoWebServiceSoapClient(binding, client);
                         }

                      public static string GetHostUrl(string ClientIp)           //遠程IP
                         {

                            string str = ClientIp + "/ClientBin/EmployeesInfoWebService.asmx";

                           return str;
                        }
                       }


          添加Silverlight動態調用WebService函數:

                   void ShowChart(string RoomId, string ChartCmd, string SeriesCmd, string roomConnString, DateTime fromDateTime, DateTime toDateTime)
                    {
                           EmployeesInfoWebServiceSoapClient webClient = ServiceUtil.GetDynamicClient(ClientIp);

                           webClient.GetWholeChartInfoCompleted += new EventHandler<GetWholeChartInfoCompletedEventArgs>(webClient_GetWholeChartInfoCompleted);

                           webClient.GetWholeChartInfoAsync(RoomId, ChartCmd, SeriesCmd, roomConnString, fromDateTime, toDateTime);
                     }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章