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



    }

  

你可能感兴趣的:(silverlight)