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);
                     }

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