Silverlight 通过反射创建WCF通信

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

 

Sliverlight端页面

        /// <summary>
        /// 通过WCF(异步)加载事件信息
        /// </summary>
        private void LoadEventByWCFAsync()
        {
            //通过反射获取示例(优点:不需要手动设置终结点)
            SupermapSLDemo.WCFService.EventBasicclient ebClient = (EventBasicclient)ServiceHelper.CreateServiceClient(typeof(SupermapSLDemo.WCFService.EventBasicclient));
            //注册异步方法实现获得菜单事件
            ebClient.GetEventForWCFCompleted += new EventHandler<GetEventForWCFCompletedEventArgs>(client_GetEventCompleted);
            //ebClient.GetEventForWCFCompleted += client_GetEventCompleted; //也可以这样注册
            //触发获得菜单事件
            ebClient.GetEventForWCFAsync();
        }
        /// <summary>
        /// 异步回调函数
         /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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;

        /// <summary>
        /// 请保证Type是ClientBase的实现类
         /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        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)]

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...