[转]使用代码去描述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标签中): ...
 
随机推荐
- Vue Router的params和query传参的使用和区别
			
vue页面跳转有两种方式分别是:name和path this.$router.push({name: 'HelloWorld2}) this.$router.push({path: '/hello-w ...
 - C# 针对文件夹的操作
			
//创建文件夹Directory.CreateDirectory(Server.MapPath("a"));Directory.CreateDirectory(Server.Map ...
 - Netty--数据通信和心跳检测
			
数据通信 概述: netty的ReadTimeOut实现方案3 服务端: public class Server { public static void main(String[] args) th ...
 - 328 Odd Even Linked List 奇偶链表
			
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
 - V-SQL的简单使用
			
V-SQL概述: V-SQL,是对同望V3平台时间多数据支持非常重要的基础引擎.因为各个数据库的查询语句的语法有所不同,V-SQL的功能是把查询语句解析为执行系统连接的数据库(MSSQL,Oracle ...
 - oracle数据库忘记用户名和密码莫着急
			
刚安装完Oracle 11g后,登录的时候没有记住用户名和密码,解决方法:新建一个用户 第一步:以系统身份登录 cmd--->sqlplus 提示输入用户名,然后输入sqlplus/as sys ...
 - Java常用开源jar包
			
转:http://blog.csdn.net/kevingao/article/details/8125683 activation~与javaMail有关的jar包,使用javaMail时应与mai ...
 - CDR如何使用钢笔工具进行完美抠图?【6·18特惠倒计时!】
			
不要以为抠图只能在图像处理软件中实现,矢量图形绘制软件CorelDRAW一样可以,而且方法很多,文章介绍使用CDR钢笔工具抠图的方法. 提示说明: 首先说明一下,CDR中的钢笔工具和其他平面设计软件中 ...
 - ffmpeg从内存读取文件
			
正常情况,ffmpeg直接从文件读取 AVFormatContext * _ctx = NULL; avformat_open_input(&_ctx, _filePath, 0, 0); 我 ...
 - CPU内部组成及原理
			
CPU,Central Processing Unit,翻译过来叫中央处理器.是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心( Control Unit).电脑中所有操作都由C ...