using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;

namespace RGDWCSServices.Common
{
    public class WCFHelper
    {
        #region Wcf服务工厂

        public static T Create<T>(string url)
        {
            return Create<T>(url, "WSHttpBinding");
        }

        public static T Create<T>(string url, string bing)
        {
            if (string.IsNullOrEmpty(url))

                MessageBox.Show("连接服务失败！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            try
            {
                EndpointAddress address = new EndpointAddress(url);

                System.ServiceModel.Channels.Binding binding = CreateBinding(bing);

                ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);

                return factory.CreateChannel();
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接服务失败！\n" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return default(T);
            }
        }

        #endregion

        #region 创建传输协议
        /// <summary>
        /// 创建传输协议
        /// </summary>
        /// <param name="binding">传输协议名称</param>
        /// <returns></returns>
        public static System.ServiceModel.Channels.Binding CreateBinding(string binding)
        {
            System.ServiceModel.Channels.Binding bindinginstance = null;

            if (binding.ToLower() == "basichttpbinding")
            {
                BasicHttpBinding ws = new BasicHttpBinding();

                ws.MaxReceivedMessageSize = 65535000;

                //System.Xml.XmlDictionaryReaderQuotas quotas = new System.Xml.XmlDictionaryReaderQuotas();

                //quotas.MaxStringContentLength = 6553500;

                //ws.ReaderQuotas = quotas;

                ws.SendTimeout = new TimeSpan(0, 5, 1);

                ws.ReceiveTimeout = new TimeSpan(0, 5, 1);

                bindinginstance = ws;
            }
            else if (binding.ToLower() == "netnamedpipebinding")
            {
                NetNamedPipeBinding ws = new NetNamedPipeBinding();
                //  ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            //else if (binding.ToLower() == "netpeertcpbinding")
            //{
            //    NetPeerTcpBinding ws = new NetPeerTcpBinding();
            //    ws.MaxReceivedMessageSize = 65535000;
            //    bindinginstance = ws;
            //}
            else if (binding.ToLower() == "nettcpbinding")
            {
                NetTcpBinding ws = new NetTcpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                ws.Security.Mode = SecurityMode.None;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "wsdualhttpbinding")
            {
                WSDualHttpBinding ws = new WSDualHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;

                bindinginstance = ws;
            }
            //else if (binding.ToLower() == "webhttpbinding")
            //{
            //    WebHttpBinding ws = new WebHttpBinding();
            //    ws.MaxReceivedMessageSize = 65535000;
            //    bindinginstance = ws;
            //}
            else if (binding.ToLower() == "wsfederationhttpbinding")
            {
                WSFederationHttpBinding ws = new WSFederationHttpBinding();
                ws.MaxReceivedMessageSize = 65535000;
                bindinginstance = ws;
            }
            else if (binding.ToLower() == "wshttpbinding")
            {
                BasicHttpBinding ws = new BasicHttpBinding(BasicHttpSecurityMode.None);

                ws.MaxReceivedMessageSize = 65535000;

                ws.SendTimeout = new TimeSpan(0, 5, 1);

                ws.ReceiveTimeout = new TimeSpan(0, 5, 1);

                bindinginstance = ws;
                // ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
                // ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;

            }
            return bindinginstance;

        }
        #endregion

    }
}
