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 ...
随机推荐
- 好记性不如烂笔头——double
两个数据转换成double型做差,会出现误差,转换成Decimal就OK了.
- LeetCode Algorithm 03_Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 快速搭建REST API——json server
一:全局安装json-server npm install json-server -g 二:在自己项目跟目录下存放mock/data.json,json内容如下: { "roles&quo ...
- JS错误记录 - 微博发布
<style> *{ margin: 0; padding: 0;} #ul1{ width: 400px; height: 400px; border: 1px solid #000; ...
- Day2:购物车小程序
一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...
- 怎样判断一个P2P平台是否靠谱?
判断一个网站,是否靠谱,是有规律可循的,P2P平台算是个新兴的电商类网站. 网上欺诈类的网站,不限于P2P,实在是太多了,真的有必要总结下最关键的几个靠谱指标. 最关键的2个 1.创始人和 ...
- BAT面试常的问题和最佳答案
原标题:BAT面试常的问题和最佳答案 技术面试 1.servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,servlet容器解析url并根据web.xml找到相对 ...
- css盒子模型的宽度问题
最近看css权威指南的时候,发现一个之前特别不清楚的概念——宽度. 每个块级元素都有一个元素框,元素框内包括了元素内容,元素内边距,元素边框,元素外边距. 所以元素框的宽度=元素内容宽度+元素内边距+ ...
- angular的学习参考材料
原文地址:https://www.jianshu.com/p/b9db7bb3d4ec 目的 其实写这篇文章的主要目的是为了提供给那些刚刚入门angular或者有意学习angular的读者准备的. 我 ...
- ios开发runtime学习三:动态添加方法(实际应用少,面试)
#import "ViewController.h" #import "Person.h" /* 1: Runtime(动态添加方法):OC都是懒加载机制,只要 ...