WCF客户端C#代码 配置config文件
不多说了,直接上代码吧。。。。
服务端Web.config文件中bindings配置
<bindings>
<wsHttpBinding>
<binding name="httpconf" closeTimeout="10:10:10"
openTimeout="10:10:10" receiveTimeout="10:10:10" sendTimeout="10:10:10"
allowCookies="true" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" >
<reliableSession ordered="true" inactivityTimeout="00:20:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
服务端Web.config文件中behavior配置
<behavior name="Card_WcfService.WCF.CardInfoServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceMetadata httpGetEnabled="true" />
<serviceTimeouts transactionTimeout="00:10:00" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceThrottling maxConcurrentCalls="2147483647" maxConcurrentInstances="2147483647" maxConcurrentSessions="2147483647" />
</behavior>
服务端Web.config文件中service配置
<service behaviorConfiguration="Card_WcfService.WCF.CardInfoServiceBehavior"
name="Card_WcfService.WCF.CardInfoService">
<endpoint address="" binding="wsHttpBinding" contract="Card_SystemAPI.ICardInfoService" bindingConfiguration="httpconf">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
下面才是我想要的东西,弄这个花了我半天时间
客户端代码注册配置文件
public class ClientFactory<TClient> : IDisposable
{
static EndpointAddress serviceAddress;
static WSHttpBinding binding;
ChannelFactory<TClient> factory;
TClient proxy;
OperationContextScope scope;
public TClient CreateClient()
{
factory = new ChannelFactory<TClient>(binding, serviceAddress);
foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
{ DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>()
as DataContractSerializerOperationBehavior; if (dataContractBehavior != null)
{ dataContractBehavior.MaxItemsInObjectGraph = 2147483647; } }
proxy = factory.CreateChannel();
((IClientChannel)proxy).Faulted += new EventHandler(a_Faulted);
scope = new OperationContextScope(((IClientChannel)proxy));
var curId = CacheStrategy.CurUser == null ?Guid.Empty : CacheStrategy.CurUser.AutoID;
MessageHeader<Guid> mhg = new MessageHeader<Guid>(curId);
MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
return proxy;
}
void a_Faulted(object sender, EventArgs e)
{
//todo:此处得不到异常的内容
}
public void Dispose()
{
try
{
scope.Dispose();
((IClientChannel)proxy).Close();
factory.Close();
}
catch
{
}
}
static ClientFactory()
{
var surl = ConfigurationManager.AppSettings["ServiceURL"];
var iname = typeof(TClient).FullName.Substring("Card_SystemAPI.I".Length);
var sname = string.Format("{0}.svc", iname);
var url = Path.Combine(surl, sname);
serviceAddress = new EndpointAddress(url);
binding = new WSHttpBinding();
binding.CloseTimeout = new TimeSpan(10, 10, 10);
binding.OpenTimeout = new TimeSpan(10, 10, 10);
binding.SendTimeout = new TimeSpan(10, 10, 10);
binding.ReceiveTimeout = new TimeSpan(10, 10, 10);
binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;
binding.AllowCookies = true;
}
}
QQ群号: 242251580 身份认证:.NET技术讨论
如有转载,请保留原有地址:http://www.cnblogs.com/hank-hu/p/4663568.html
WCF客户端C#代码 配置config文件的更多相关文章
- PHP 后台程序配置config文件,及form表单上传文件
一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...
- WCF客户端简单动态配置服务地址
本来想实现WCF服务无论放到哪个机器上,我的客户端都不需要重新编译,只需要配置一个服务的地址即可.各种百度找到了很多解决方案.但都比较繁琐,(只要因为个人小菜看不懂太多的代码)我对WCF内部机制还不了 ...
- winform客户端程序实时读写app.config文件
新接到需求,wcf客户端程序运行时,能实时修改程序的打印机名称: 使用XmlHelper读写 winform.exe.config文件修改后始终,不能实时读取出来,查询博客园,原来已有大神解释了: 获 ...
- git配置config文件
1.Git有一个工具被称为git config,它允许你获取和设置变量:这些变量可以控制Git的外观和操作的各个方面.这些变量以等级的不同可以被存储在三个不同的位置: (1) /etc/gitconf ...
- Linux内核配置.config文件
在命令行中,进入顶层内核目录,并输入命令make menuconfig,就可以启动一个基于菜单的内核配置编辑器.从这里开始,你可以访问每个可用的配置参数,并生成一个定制的内核配置. 当你退出配置编辑器 ...
- silverlight 生成图表 WCF 解析XML代码.svc.cs 文件
silverlight 调用wcf 文件代码 private ListItem AnalyzeXML(string XMLCode, string Reportdate, string ChartNa ...
- 【IIS】IIS6.1配置 *.config 文件 的MIME类型用于升级程序
参考:http://blog.csdn.net 1. 2. 请求筛选中允许config文件下载, 3. 添加.config到 MIME类型. 3.注意:筛选项.
- HDFS的Java客户端操作代码(HDFS删除文件或目录)
1.HDFS删除文件或目录 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.hadoo ...
- WCF客户端关闭代码
Close不一定会成功,所以需要Abort. ChannelFactory channel = new ChannelFactory<IService1>("bindingN ...
随机推荐
- amazeui学习笔记--css(基本样式3)--文字排版Typography
amazeui学习笔记--css(基本样式3)--文字排版Typography 一.总结 1.字体:amaze默认非 衬线字体(sans-serif) 2.引用块blockquote和定义列表:引用块 ...
- Android 最简洁的获取联系人头像的代码
ContentResolver cr = view.getContext().getContentResolver(); Uri uri = ContentUris.withAppendedId(Co ...
- VMWare中的三种联网模式图解
网络基础及局域网配置 1.简单的局域网结构 2.VMWare中的三种联网模式 NAT模式 桥接模式 VMnet1
- Mybatis的使用中的一些不太注意的技巧
以下就总结一下Mybatis的使用中的一些不太注意的技巧,算是Mybatis的总结笔 1.插入时主键返回 我们向数据库插入一条记录是,使用Mybatis的<insert>是无法返回插入的主 ...
- 原生js大总结二
011.if语句的优化 1.把次数多的条件和执行结果放到最前面 2.减少第一次无用的判断,可以用嵌套判断 3.判断语句禁止出现三次嵌套 012.谈谈你对switch的理解 1. ...
- UVA 10917 Walk Through the Forest SPFA
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...
- Java的压缩、解压及压缩加密、解密解压 样例
为了节约带宽.加快传送速度,http协议支持gzip的压缩,但假设我们的app与后台不是通过http协议通讯的.那么压缩.解压这个流程须要自己写.以下给出compress和decompress的代码: ...
- tc
[em_Compare]cmd=d:\Apps\BeyondCompare4\now\BCompare.exeparam="""%X%P%S"" &q ...
- java痛苦学习之路[十二]JSON+ajax+Servlet JSON数据转换和传递
1.首先client须要引入 jquery-1.11.1.js 2.其次javawebproject里面须要引入jar包 [commons-beanutils-1.8.0.jar.commons-c ...
- swift学习第一天:认识swift以及swift的常量和变量
一:认识swift // 1.导入框架 //#import <UIKit/UIKit.h> import UIKit // 2.定义一个标识符 // int a = 10; // swif ...