Translate this app.config xml to code? (WCF) z
http://stackoverflow.com/questions/730693/translate-this-app-config-xml-to-code-wcf
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MyService"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://server.com/service/MyService.asmx"
binding="basicHttpBinding" bindingConfiguration="MyService"
contract="MyService.MyServiceInterface"
name="MyService" />
</client>
</system.serviceModel>
改为
BasicHttpBinding binding = new BasicHttpBinding();
Uri endpointAddress = new Uri("https://server.com/service/MyService.asmx"); ChannelFactory<MyService.MyServiceInterface> factory = new ChannelFactory<MyService.MyServiceInterface>(binding, endpointAddress); MyService.MyServiceInterface proxy = factory.CreateChannel();
完善为
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
Uri endpointAddress = new Uri("https://server.com/Service.asmx"); ChannelFactory<MyService.MyServiceInterface> factory = new ChannelFactory<MyService.MyServiceInterface>(binding, endpointAddress.ToString());
factory.Credentials.UserName.UserName = "username";
factory.Credentials.UserName.Password = "password"; MyService.MyServiceInterface client = factory.CreateChannel(); // make use of client to call web service here...
示例2:
<bindings>
<customBinding>
<binding name="lbinding">
<security authenticationMode="UserNameOverTransport"
messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11"
securityHeaderLayout="Strict"
includeTimestamp="false"
requireDerivedKeys="true"
keyEntropyMode="ServerEntropy">
</security>
<textMessageEncoding messageVersion="Soap11" />
<httpsTransport authenticationScheme ="Negotiate" requireClientCertificate ="false" realm =""/>
</binding>
</customBinding>
</bindings>
改为:
Uri epUri = new Uri(_serviceUri);
CustomBinding binding = new CustomBinding();
SecurityBindingElement sbe = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
sbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11;
sbe.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
sbe.IncludeTimestamp = false;
sbe.SetKeyDerivation(true);
sbe.KeyEntropyMode = System.ServiceModel.Security.SecurityKeyEntropyMode.ServerEntropy;
binding.Elements.Add(sbe);
binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8));
binding.Elements.Add(new HttpsTransportBindingElement());
EndpointAddress endPoint = new EndpointAddress(epUri);
辅助函数:
public static TResult UseService<TChannel, TResult>(string url,
EndpointIdentity identity,
NetworkCredential credential,
Func<TChannel, TResult> acc)
{
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
var endPointAddress = new EndpointAddress(new Uri(url), identity,
new AddressHeaderCollection());
var factory = new ChannelFactory<T>(binding, address);
var loginCredentials = new ClientCredentials();
loginCredentials.Windows.ClientCredential = credentials; foreach (var cred in factory.Endpoint.EndpointBehaviors.Where(b => b is ClientCredentials).ToArray())
factory.Endpoint.EndpointBehaviors.Remove(cred); factory.Endpoint.EndpointBehaviors.Add(loginCredentials);
TChannel channel = factory.CreateChannel();
bool error = true;
try
{
TResult result = acc(channel);
((IClientChannel)channel).Close();
error = false;
factory.Close();
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return default(TResult);
}
finally
{
if (error)
((IClientChannel)channel).Abort();
}
}
调用方法:
usage
NameSpace.UseService("http://server/XRMDeployment/2011/Deployment.svc",
Identity,
Credentials,
(IOrganizationService context) =>
{
... your code here ...
return true;
});
示例3:
//end point setup
System.ServiceModel.EndpointAddress EndPoint = new System.ServiceModel.EndpointAddress("http://Domain:port/Class/Method");
System.ServiceModel.EndpointIdentity EndpointIdentity = default(System.ServiceModel.EndpointIdentity); //binding setup
System.ServiceModel.BasicHttpBinding binding = default(System.ServiceModel.BasicHttpBinding); binding.TransferMode = TransferMode.Streamed;
//add settings
binding.MaxReceivedMessageSize = int.MaxValue;
binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
binding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
binding.ReaderQuotas.MaxDepth = int.MaxValue;
binding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = int.MaxValue; binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.MaxBufferSize = int.MaxValue;
binding.MaxBufferPoolSize = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.SendTimeout = new TimeSpan(0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0); //setup for custom binding
System.ServiceModel.Channels.CustomBinding CustomBinding = new System.ServiceModel.Channels.CustomBinding(binding);
What I do to configure my contract:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), System.ServiceModel.ServiceContractAttribute(Namespace = "http://MyNameSpace", ConfigurationName = "IHostInterface")]
public interface IHostInterface
{
}
Translate this app.config xml to code? (WCF) z的更多相关文章
- WCF中,通过C#代码或App.config配置文件创建ServiceHost类
C# static void Main(string[] args) { //创建宿主的基地址 Uri baseAddress = new Uri("http://localhost:808 ...
- C# App.config 详解
读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...
- C# App.config全攻略
读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration ...
- App.config的学习笔记
昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config不大可能了. WP具体支持API请查 ...
- App.config
App.config的学习笔记 昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config ...
- c#配置文件app.config 与 Settings.settings
本篇博客将介绍C#中Settings的使用.参考:https://docs.microsoft.com/zh-cn/visualstudio/ide/managing-application-sett ...
- App.config配置详解
经上一篇文章https://www.cnblogs.com/luna-hehe/p/9104701.html发现自己对配置文件很是不了解,同样还是查了半天终于发现另一片宝贵文档https://www. ...
- C# 如何添加自定义键盘处理事件 如何配置app.config ? | csharp key press event tutorial and app.config
本文首发于个人博客https://kezunlin.me/post/9f24ebb5/,欢迎阅读最新内容! csharp key press event tutorial and app.config ...
- C# App.config 自定义 配置节
1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration> ...
随机推荐
- lua堆栈操作常用函数学习二
/* ** basic stack manipulation */ LUA_API int <strong> (lua_gettop) (lua_State *L); </str ...
- css float对于之后布局的影响
后面的元素不浮动,即便设置了宽度,表面上只占了一定的宽度,但实际上占了全屏.(所以设置了overflow之后,并且之后的div设置了宽度,再设置margin-left可能不起作用). 高度对浮动的影响 ...
- dede数据库文件导入失败的可能原因是数据表前缀不同,这里的失败指的是mysql添加了数据,但后台不显示
利用dede提供的数据备份还原功能,还原数据,出现失败的可能原因是数据表前缀不同,改过来就可以了
- KindEditor ---富编辑器
关于 演示 下载 文档 成功案例 English 文档 Documentation http://kindeditor.net/doc3.php 当前位置: 首页 > 文档 文档 Docum ...
- linux包之sysstat之mpstat与pidstat命令
概述 [root@localhost ~]# rpm -qa|grep sysstsysstat-9.0.4-22.el6.x86_64mpstat是MultiProcessor Statistics ...
- Openjudge计算概论-角谷猜想
/*===================================== 角谷猜想 总时间限制: 1000ms 内存限制: 65536kB 描述 所谓角谷猜想,是指对于任意一个正整数,如果是奇数 ...
- OpenJudge计算概论-求出e的值
/*======================================================================== 求出e的值 总时间限制: 1000ms 内存限制: ...
- scanf与scanf_s
scanf的使用 使用scanf需要记住下面两条简单规则: 如果使用scanf来读取某种基本变量类型(%d,%c,%f,%lf)的值,请在变量名之前加上一个& 如果使用scanf把一个字符串( ...
- mysql toolkit 用法[备忘] (转)
命令列表 /usr/bin/pt-agent /usr/bin/pt-align /usr/bin/pt-archiver /usr/bin/pt-config-diff /usr/bin/pt-de ...
- NoSQL之基础篇
NoSQL(NoSQL = Not Only SQL ),泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2. ...