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

namespace Model
{
    public class WCFHelper
    {

        #region Wcf服务工厂

        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);
            }
        }

        public static T CreateDual<T>(string url, string bing, object obj, out object objOut)
        {
            objOut = null;

            if (string.IsNullOrEmpty(url))

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

            try
            {
                objOut = new InstanceContext(obj);

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

               

                EndpointAddress address = new EndpointAddress(url);

                DuplexChannelFactory<T> factory = new DuplexChannelFactory<T>(objOut, 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;

                ws.SendTimeout = new TimeSpan(0,10,0);

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

                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.ClientBaseAddress = new Uri("http://192.168.1.8:8003/Service/VisualService");

                ws.MaxReceivedMessageSize = 65535000;

                ws.Security.Mode = WSDualHttpSecurityMode.None;

                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")
            {
                WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);


                ws.MaxReceivedMessageSize = 65535000;

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

            }
            return bindinginstance;

        }
        #endregion

    }

}