silverlight动态创建WCF服务

 最近在发布silverlight项目时,服务地址不是固定,因此服务需要动态创建。在网上搜了点资料找到了动态创建服务的方法,直接上代码

  /// <summary>
    /// 根据服务类型创建服务
    /// </summary>
    public static class CreateService
    {
        #region//动态创建服务
        /// <summary>
        /// 动态创建基本服务
        /// </summary>
        /// <typeparam name="serviceType">服务类型</typeparam>
        /// <returns></returns>
        public static T GetBasicService<T>(string serviceType) where T : class
        {
            T instance = null;
            BasicHttpBinding bhb = new BasicHttpBinding();
            string address = GetServerUrl(serviceType);
            EndpointAddress endPointAddress = new EndpointAddress(address);
            object[] paras = new object[2];

            paras[0] = bhb;
            paras[1] = endPointAddress;

            ConstructorInfo constructor = null;

            try
            {
                Type[] types = new Type[2];
                types[0] = typeof(System.ServiceModel.Channels.Binding);
                types[1] = typeof(System.ServiceModel.EndpointAddress);
                constructor = typeof(T).GetConstructor(types);
            }
            catch (Exception)
            {

            }
            if (constructor != null)
                instance = (T)constructor.Invoke(paras);
            return instance;
        }

        /// <summary>
        /// 根据服务类型新建一个自定义服务实例
        /// </summary>
        /// <param name="serviceType">服务类型</param>
        /// <returns></returns>
        public static T GetCustomerService<T>(string serviceType) where T : class
        {
            T _instance = null;
            string serviceUri = GetServerUrl(serviceType);
            if (string.IsNullOrEmpty(serviceUri)) return null;

            object[] paras = new object[2];

            var elements = new List<BindingElement>();
            elements.Add(new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.Default, WriteEncoding = Encoding.UTF8 });
            HttpTransportBindingElement bindingElement = new HttpTransportBindingElement();
            bindingElement.MaxReceivedMessageSize = int.MaxValue;
            bindingElement.MaxBufferSize = int.MaxValue;
            elements.Add(bindingElement);
            CustomBinding binding = new CustomBinding(elements);
            EndpointAddress address = new EndpointAddress(new Uri(serviceUri, UriKind.Absolute));
            paras[0] = binding;
            paras[1] = address;
            ConstructorInfo constructor = null;
            try
            {
                Type[] types = new Type[2];
                types[0] = typeof(System.ServiceModel.Channels.Binding);
                types[1] = typeof(System.ServiceModel.EndpointAddress);
                constructor = typeof(T).GetConstructor(types);
            }
            catch (Exception)
            {
                return null;
            }

            if (constructor != null)
                _instance = (T)constructor.Invoke(paras);

            return _instance;
        }
        #endregion

        #region//获取当前服务的地址
        /// <summary>
        /// 获取当前服务的URL
        /// </summary>
        /// <returns></returns>
        private static string GetServerUrl(string serviceName)
        {
            return App.ServiceUriList[serviceName];
        }
        #endregion

    }

  

转载于:https://www.cnblogs.com/HellenTian/archive/2011/08/31/2160430.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值