您的位置:首页 > 其它

Silverlight里动态调用WCF服务

2011-05-26 22:12 453 查看
把自己曾经写过的东西,记录以下。

请注意看下面的小蓝色点,点击那个点就可以看到代码了!

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ServiceModel;using System.ServiceModel.Channels;using System.Net.Browser;/********************************** * 创建人:刘跃飞 * 创建时间:2010-01-15 * 功能描述:动态创建WCF服务客户端,可以避免将配置文件中的配置信息暴露给客户端 * **********************************/namespace SLApp{    public class ServiceUtil    {        /// <summary>        /// 动态创建WCF客户端         /// </summary>        /// <returns></returns>        public static ServiceReference1.SilverLightServiceClient GetDynamicClient()        {            try            {                HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();                httpTransport.MaxReceivedMessageSize = 2147483647;                httpTransport.MaxBufferSize = 2147483647;                            BinaryMessageEncodingBindingElement binaryEle = new BinaryMessageEncodingBindingElement();                CustomBinding binding = new CustomBinding(binaryEle, httpTransport);                binding.Name = "CustomBinding_SilverLightService";                Uri myuri = new Uri(Application.Current.Host.Source, "/SLWcfService/SilverLightService.svc");//                string IP = myuri.Host;                string Path = @"http://" + IP + "/SLWcfService/SilverLightService.svc";                             Uri myuri2 = new Uri(Path, UriKind.RelativeOrAbsolute);                //---记录IP地址                SysInfo.SystemInfo.HostIP = IP;                //--测试用WCF地址                Uri testuri = new Uri("http://172.16.1.216/SLWcfService/SilverLightService.svc");//Uri("http://172.16.1.209/SLWcfService/SilverLightService.svc");                 //MessageBox.Show(myuri2.Host + "   DnsSafeHost:" + myuri2.DnsSafeHost + " AbsolutePath:" + myuri2.AbsolutePath + " AbsoluteUri:" + myuri2.AbsoluteUri);                //ServiceReference1.SilverLightServiceClient client = new ServiceReference1.SilverLightServiceClient(binding, new EndpointAddress(myuri2));//myuri2                ServiceReference1.SilverLightServiceClient client = new ServiceReference1.SilverLightServiceClient(binding, new EndpointAddress(testuri));                client.Endpoint.Name = "CustomBinding_SilverLightService";                return client;            }            catch            {                MessageBox.Show("生成WCF服务时出错,请联系管理员修复此问题!","消息提示",MessageBoxButton.OK);                return null;            }        }    }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: