[转]使用代码去描述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配置文件的更多相关文章
- WCF 配置文件(三)
配置文件概述 WCF服务配置是WCF服务编程的主要部分.WCF作为分布式开发的基础框架,在定义服务以及定义消费服务的客户端时,都使用了配置文件的方法.虽然WCF也提供硬编程的方式,通过在代码中直接设置 ...
- 30行代码搞定WCF并发性能测试
[以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main() { List< ...
- 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11
这篇博文,我们编写一个C语言的小程序,来解析数据段或者代码段描述符的各个字段.这样我们阅读原书的代码就会方便一点,只要运行这个小程序,就可以明白程序中定义的数据段或者代码段的描述符了. 这段代码,我用 ...
- 数据段描述符和代码段描述符(一)——《x86汇编语言:从实模式到保护模式》读书笔记10
一.段描述符的分类 在上一篇博文中已经说过,为了使用段,我们必须要创建段描述符.80X86中有各种各样的段描述符,下图展示了它们的分类. 看了上图,你也许会说:天啊,怎么这么多段描述符啊!我可怎么记住 ...
- 在Maven项目中添加代码目录下的配置文件
问题 Maven 是约定大于配置的一种工具, 通常约定在 src/main/resources 目录下放配置文件, 当我们想要在 src/main/java 代码目录下放置配置文件用来测试, Mave ...
- 通过纯代码方式发布WCF服务
网络上搜索WCF服务,一般是寄宿在IIS,通过WebConfig方式配服务地址,接口类型等信息,但是对于我这样的懒人,目前项目在开发阶段,实在不愿意每次添加新服务就更新配置文件,于是使用了反射来加载服 ...
- WCF配置文件与文件下载之坎坷路
题外话:本以为我会WCF了,精通WCF了,毕竟刚做过一个WCF的项目,不就是写写契约接口,然后实现接口,改下配置.最后用控制台或者服务发布一下,不就能用了.不就是简单ABC吗?不是So Easy吗?做 ...
- WCF配置文件详解
今天来看看WCF的配置方法. 上图整理了服务配置过程中所用到的基本的元素,大致的步骤主要是首先要在调用服务的程序集中添加服务的一个引用,然后添加一个service并指定服务的名称.终结点,如果添加了b ...
- WCF配置文件
因为要上传较大的图片,WCF传递数组的默认的最大数组16KB就不够了.以下讲解配置内容. 服务端配置 这里一个WCF项目中有1个服务,配置文件如下(位于system.serviceModel标签中): ...
随机推荐
- Android点9图的运用
在Android UI设计开发中,我们经常会用到一些图标.图片来做背景等. 相信很多同学都会遇到一个问题,就是我们让美工做好一张图,一个图标,呃,看起来挺好看的,但是放进app中,扩大或缩小.在不同分 ...
- 如何查看jdk的版本
(1)WINDOWS环境下 实验环境:WIN7 64bit 操作指令:cmd命令下输入“java -version” 参考如下:JDK1.7 (2).LINUX环境下 实验环境:CentOS 5.6 ...
- Offer收割_4
1.水题 2.BFS宽搜(使用优先队列priority_queue) 4.题意:给数组a.要求重排列数组,使得数组中的任意相邻的两个元素不同.如果存在多个方案,那么选择字典序最小的方案. 如果不能满 ...
- Intent的调用
//Intent intent=new Intent();//intent.setClass(MainActivity.this, GPSService.class);//以上二条可以合并成如下一条 ...
- Alpha Edition [ Group 1 ]
Deltafish Alpha Edition 一.博客归档(记录人:娄雨禛) 小组会议 DeltaFish 校园物资共享平台 第一次小组会议 DeltaFish 校园物资共享平台 第二次小组会议 D ...
- python自动化测试框架(一)
1.开发环境 名称 版本 系统 windows 7 python版本 2.7.14 IDE pycharm2017 2.大致框架流程 :展示了框架实现的业务流程 3.框架介绍 3.1 ======完善 ...
- java 操作clob
之前在学校的时候做的都是练习,小儿科,遇到的情况完全都在自己的设想范围内.最近老是遇到字段溢出的情况,但是varchar2好像最长也只有4000个字符.所以不得不另辟蹊径,就找上了clob字段. pa ...
- strut2 拦截器 使用
拦截器是strut2里一个很振奋人心的应用.通过配置拦截器可以在action执行之前进行一些初始化或者是其他的操作,但是在action执行之后,返回结果就已经确定,结果是很难改变了(目前我还不知道怎么 ...
- SQL server 2005中无法新建作用(Job)的问题
1.在使用sqlserver2005创建作业时,创建不了,提示 无法将类型为“Microsoft.SqlServer.Management.Smo.SimpleObjectKey”的对象强制转换为类型 ...
- 170925_2 Python socket 创建UDP的服务器端和客户端
[python版本]3.6 UDP服务器端: from socket import * from time import ctime host = '' port = 21567 buf_size = ...