silverlight 동적 생성 WCF 서비스

3627 단어 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



    }


좋은 웹페이지 즐겨찾기