动态配置WCF地址

通过svcutil.exe 命令可以生成wcf服务的代理类,然后再配置webconfig调用代理类能达到调用服务的效果。再就是添加web引用也可以调用WCF服务。

但如果服务太多就必须逐一在webconfig配置服务地址,所以研究了下动态配置WCF服务的主机名:

1、编写WCF服务,并通过svcutil.exe 命令生成代理类

2、拷贝代理类在项目

3、编写调用类代码

 public class InvokeContext
    {

        #region CallService
        private const string wshttpbinding = "wshttpbinding";
        //public static T CreateWCFServiceByURL<T>(string url)
        //{
        //    return CreateWCFServiceByURL<T>(url, "wsHttpBinding", "WSHttpBinding_IDeskBoardProvider");
        //}
        public static T CreateWCFServiceByURL<T>(string url, string bing,string wsName)
        {
            if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");
            EndpointAddress address = new EndpointAddress(url);
            Binding binding = CreateBinding(bing,wsName);
            ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
            return factory.CreateChannel();
        }
        #endregion

        #region Create Protocol
        /// <summary>
        /// Create Protocol
        /// </summary>
        /// <param name="binding">Protocol Name</param>
        /// <returns></returns>
        public static Binding CreateBinding(string binding, string wsName)
        {
            Binding bindinginstance = null;

            if (binding.ToLower() == wshttpbinding)
            {
                WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
                //ws.Name = "WSHttpBinding_IDeskBoardProvider";
                ws.Name = wsName;
                ws.CloseTimeout =new TimeSpan(00, 01, 00);
                ws.OpenTimeout = new TimeSpan(00,01,00);
                ws.ReceiveTimeout = new TimeSpan(00,10,00);
                ws.SendTimeout = new TimeSpan(00,01,00);
                ws.BypassProxyOnLocal = false;
                ws.TransactionFlow = false;
                ws.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                ws.MaxBufferPoolSize = 524288;
                ws.MaxReceivedMessageSize = 65536;
                ws.MessageEncoding = WSMessageEncoding.Text;
                ws.TextEncoding = Encoding.UTF8;
                ws.UseDefaultWebProxy = true;
                ws.AllowCookies = false;
                ws.ReaderQuotas.MaxDepth = 32;
                ws.ReaderQuotas.MaxStringContentLength = 8192;
                ws.ReaderQuotas.MaxArrayLength = 16384;
                ws.ReaderQuotas.MaxBytesPerRead = 4096;
                ws.ReaderQuotas.MaxNameTableCharCount = 16384;
                ws.ReliableSession.Ordered = true;
                ws.ReliableSession.InactivityTimeout = new TimeSpan(00,10,00);
                ws.ReliableSession.Enabled = false;
                ws.Security.Mode = SecurityMode.Message;
                ws.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
                ws.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                ws.Security.Transport.Realm = "";
                ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
                ws.Security.Message.NegotiateServiceCredential = true;
                ws.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
                ws.Security.Message.EstablishSecurityContext = true;
                bindinginstance = ws;
            }
            return bindinginstance;

        }
        #endregion

    }

 

4、页面调用服务

Default.aspx.cs:

 

public partial class _Default : System.Web.UI.Page
    {
        private string url = "http://localhost:888/DeskTest.svc";
        private string wsHttpBinding = "wsHttpBinding";
        private string wshttpInterface = "WSHttpBinding_IDeskBoardProvider";

        protected void Page_Load(object sender, EventArgs e)
        {

     IDeskBoard inface = InvokeContext.CreateWCFServiceByURL<IDeskBoard>(url, wsHttpBinding, wshttpInterface);
            List<T_SYS_DESKBOARD> listDeskBoard = inface.GetDeskBoards().ToList();
            Repeater1.DataSource = listDeskBoard;
            Repeater1.DataBind();

        }

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值