Silverlight 通过反射创建WCF通信

 简单通过代码介绍通过反射机制Silverlight客户端与服务端之间的通讯。

 

Sliverlight端页面

        /// 
        /// 通过WCF(异步)加载事件信息
        /// 
        private void LoadEventByWCFAsync()
        {
            //通过反射获取示例(优点:不需要手动设置终结点)
            SupermapSLDemo.WCFService.EventBasicClient ebClient = (EventBasicClient)ServiceHelper.CreateServiceClient(typeof(SupermapSLDemo.WCFService.EventBasicClient));
            //注册异步方法实现获得菜单事件
            ebClient.GetEventForWCFCompleted += new EventHandler(client_GetEventCompleted);
            //ebClient.GetEventForWCFCompleted += client_GetEventCompleted; //也可以这样注册
            //触发获得菜单事件
            ebClient.GetEventForWCFAsync();
        }
        /// 
        /// 异步回调函数
         /// 
        /// 
        /// 
        void client_GetEventCompleted(object sender, SupermapSLDemo.WCFService.GetEventForWCFCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                dgEvent.ItemsSource = e.Result;
            }
        }




 

 

 

帮助类

using System;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Windows;

namespace HEMS.Common.Util
{
    public static class ServiceHelper
    {
        private const int MAXBUFFERSIZE = 2147483647;
        private const long MAXRECEIVEDMESSAGESIZE = 2147483647;

        /// 
        /// 请保证Type是ClientBase的实现类
         /// 
        /// 
        /// 
        public static object CreateServiceClient(Type type)
        {
            //实例化客户端程序
             object client = System.Activator.CreateInstance(type);
            //在属性中找到需要的字段
              foreach (PropertyInfo prInfo in type.GetProperties())
            {
                //找到ServiceEndpoint类型的字段
                  if (prInfo.PropertyType == typeof(ServiceEndpoint))
                {
                    //得到数据
                       object pValue = prInfo.GetValue(client, null);
                    //判断空值
                       if (pValue != null)
                    {
                        //得到ServiceEndpoint对象
                            ServiceEndpoint endPt = (System.ServiceModel.Description.ServiceEndpoint)pValue;
                        BasicHttpBinding binding = new BasicHttpBinding();
                        binding.MaxBufferSize = ServiceHelper.MAXBUFFERSIZE;
                        binding.MaxReceivedMessageSize = ServiceHelper.MAXRECEIVEDMESSAGESIZE;

                        endPt.Binding = binding;
                        //根据webservice的路径获得新地址
                            string strUri = endPt.Address.Uri.AbsolutePath;
                        if (strUri.StartsWith("/"))
                        {
                            strUri = strUri.Substring(1);
                        }
                        EndpointAddress newepAddress = new EndpointAddress(BrowserHelper.GetCurrentHostPath() + strUri);
                        endPt.Address = newepAddress;
                    }
                }
            }
            return client;
        }
    }
}

 

 浏览器帮助类:

using System;
using System.Windows.Browser;

namespace HEMS.Common.Util
{
    public static class BrowserHelper
    {
        public static string GetCurrentHostPath()
        {
            string rootUrl = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.HttpRequestUrl,
                                                                         UriFormat.UriEscaped);
            rootUrl = rootUrl.ToLower();
            if (rootUrl.EndsWith("default.aspx"))
            {
                rootUrl = rootUrl.Replace("default.aspx", "");
            }
            else
            {
                rootUrl = rootUrl.Substring(0, rootUrl.LastIndexOf('/'));
            }
            if (!rootUrl.EndsWith("/"))
            {
                rootUrl = rootUrl + @"/";
            }
            return rootUrl;
        }
    }
}


 

 

注:在编程过程中,出现:siverlight 无法激活服务,因为它不支持 ASP.NET 兼容性的问题,具体描述如下:

无法激活服务,因为它不支持 ASP.NET 兼容性。已为此应用程序启用了 ASP.NET 兼容性。请在 web.config 中关闭 ASP.NET 兼容性模式,或将 AspNetCompatibilityRequirements 特性添加到服务类型且同时将 RequirementsMode 设置为“Allowed”或“Required”。

截图:

 

解决办法:

修改相应   服务.svc.cs

using System.ServiceModel.Activation ;

[AspNetCompatibilityRequirements (RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]

 

 

 

你可能感兴趣的:(SliverLight,silverlight,wcf,asp.net,sliverlight,webservice,binding)