转自:使用代码去描述WCF配置文件

在应用程序部署的时候,WCF客户端因为服务器地址的变化,需要修改程序配置文件的地址URL,手动修改很不方便,还会造成错误,所以尽量把描述WCF配置文件的配置使用代码方式进行描述,通过在软件中输入服务器IP地址的方式,动态修改访问的URL,这样比较方便,也不会出错,导致程序运行异常。

下面我将一个WCF部署文件采用代码方式描述:

WCF客户端的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IMainService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8888/DataCenter.Factory/MainService/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMainService"
contract="IMainService" name="WSHttpBinding_IMainService">
<identity>
<servicePrincipalName value="host/WIN-QFIKKT28EHC" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

编程中使用代码描述上述文件:在这里客户端引用的WCF服务命名空间定义为DataCenterFatoryC,客户端访问对象为  DataCenterFatoryC.MainServiceClient MainServiceClient1;

   using System.ServiceModel;  

   WSHttpBinding Bindins;  

   DataCenterFatoryC.MainServiceClient MainServiceClient1;  

   private void MainForm_Load(object sender, EventArgs e)
{
Bindins = new WSHttpBinding();//设置绑定
Bindins.CloseTimeout = TimeSpan.Parse("00:01:00");
Bindins.OpenTimeout = TimeSpan.Parse("00:01:00");
Bindins.ReceiveTimeout = TimeSpan.Parse("00:10:00");
Bindins.SendTimeout = TimeSpan.Parse("00:01:00");
Bindins.BypassProxyOnLocal = false;
Bindins.TransactionFlow = false;
Bindins.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
Bindins.MaxBufferPoolSize = ;
Bindins.MaxReceivedMessageSize = ;
Bindins.MessageEncoding = WSMessageEncoding.Text;
Bindins.TextEncoding = Encoding.UTF8;
Bindins.UseDefaultWebProxy = true;
Bindins.ReaderQuotas.MaxDepth = ;
Bindins.ReaderQuotas.MaxStringContentLength = ;
Bindins.ReaderQuotas.MaxArrayLength = ;
Bindins.ReaderQuotas.MaxBytesPerRead = ;
Bindins.ReaderQuotas.MaxNameTableCharCount = ;
Bindins.ReliableSession.Ordered = true;
Bindins.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
Bindins.ReliableSession.Enabled = false;
Bindins.Security.Mode = SecurityMode.Message;
Bindins.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
Bindins.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
Bindins.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
Bindins.Security.Message.NegotiateServiceCredential = true;
Bindins.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
Bindins.Security.Message.EstablishSecurityContext = true; string address = "http://localhost:8888/DataCenter.Factory/MainService/";//服务终结点的URL
MainServiceClient1 = new DataCenterFatoryC.MainServiceClient(Bindins, new EndpointAddress(address));
MainServiceClient1.Open();
}

通过对Bindins的参数设置,能有效的描述访问过程的一些问题,比如上传下载大文件,大数据表都依赖重要参数的设置,否则运行中会有异常错误发生,这是我们应该注意的。

[转]使用代码去描述WCF配置文件的更多相关文章

  1. WCF 配置文件(三)

    配置文件概述 WCF服务配置是WCF服务编程的主要部分.WCF作为分布式开发的基础框架,在定义服务以及定义消费服务的客户端时,都使用了配置文件的方法.虽然WCF也提供硬编程的方式,通过在代码中直接设置 ...

  2. 30行代码搞定WCF并发性能测试

    [以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main()         {               List< ...

  3. 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11

    这篇博文,我们编写一个C语言的小程序,来解析数据段或者代码段描述符的各个字段.这样我们阅读原书的代码就会方便一点,只要运行这个小程序,就可以明白程序中定义的数据段或者代码段的描述符了. 这段代码,我用 ...

  4. 数据段描述符和代码段描述符(一)——《x86汇编语言:从实模式到保护模式》读书笔记10

    一.段描述符的分类 在上一篇博文中已经说过,为了使用段,我们必须要创建段描述符.80X86中有各种各样的段描述符,下图展示了它们的分类. 看了上图,你也许会说:天啊,怎么这么多段描述符啊!我可怎么记住 ...

  5. 在Maven项目中添加代码目录下的配置文件

    问题 Maven 是约定大于配置的一种工具, 通常约定在 src/main/resources 目录下放配置文件, 当我们想要在 src/main/java 代码目录下放置配置文件用来测试, Mave ...

  6. 通过纯代码方式发布WCF服务

    网络上搜索WCF服务,一般是寄宿在IIS,通过WebConfig方式配服务地址,接口类型等信息,但是对于我这样的懒人,目前项目在开发阶段,实在不愿意每次添加新服务就更新配置文件,于是使用了反射来加载服 ...

  7. WCF配置文件与文件下载之坎坷路

    题外话:本以为我会WCF了,精通WCF了,毕竟刚做过一个WCF的项目,不就是写写契约接口,然后实现接口,改下配置.最后用控制台或者服务发布一下,不就能用了.不就是简单ABC吗?不是So Easy吗?做 ...

  8. WCF配置文件详解

    今天来看看WCF的配置方法. 上图整理了服务配置过程中所用到的基本的元素,大致的步骤主要是首先要在调用服务的程序集中添加服务的一个引用,然后添加一个service并指定服务的名称.终结点,如果添加了b ...

  9. WCF配置文件

    因为要上传较大的图片,WCF传递数组的默认的最大数组16KB就不够了.以下讲解配置内容. 服务端配置 这里一个WCF项目中有1个服务,配置文件如下(位于system.serviceModel标签中): ...

随机推荐

  1. 网站防止用户复制的js方法

    <script type="text/javascript">function stop() {return false;}document.oncontextmenu ...

  2. 9.15NOIP模拟题

    GRYZ 模拟考试套题 9.15 gryz信息组专场 题目名称 最初的最初 太 妃 糖 可执行文件名 eat hwc dance sugar 输入文件 eat.in hwc.in dance.in s ...

  3. $P2126 Mzc家中的男家丁$

    problem #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  4. Android布局属性LayoutParams的理解

    在一次笔试中搞混LayoutParams的用法,特来总结一番: LayoutParams继承于Android.View.ViewGroup.LayoutParams.LayoutParams相当于一个 ...

  5. Linux查找目录下的按时间过滤的文件

    在维护项目中,有时会指定都一些条件进行过滤文件,并对该批文件进行操作:这时我们将使用shell命令进行操作:直接上代码 #!/bin/sh #BEGIN #`find ./ ! -name " ...

  6. synchronized关键字详解(一)

    synchronized官方定义: 同步方法支持一种简单的策略防止线程干扰和内存一致性错误,如果一个对象对多个线程可见,则对该对象变量的所有读取或写入都是通过同步方法完成的(这一个synchroniz ...

  7. 一个例子理解ES6的yield关键字

    yield是什么 yield是ES6的新关键字,使函数暂停执行. 一个简单例子 function *countASb() { console.log('Show0:'); var a1 = yield ...

  8. RAID技术简单分析

    RAID技术解析 RAID:独立磁盘冗余阵列(Redundant Array of Independent Disks) RAID技术就是将许多块硬盘设备组合成一个容量更大.更安全的硬盘组,可以将数据 ...

  9. mvc 类中对应数据库属性

    [StringLength()] //可空 对应数据库可空 [DefaultValue("")] [DisplayName("添加人用户名")] public ...

  10. img图片加载出错处理

    img图片加载出错处理   为了美观当网页图片不存在时不显示叉叉图片 当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验.即使使用alt属性给出了” ...