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的更多相关文章

  1. WCF中,通过C#代码或App.config配置文件创建ServiceHost类

    C# static void Main(string[] args) { //创建宿主的基地址 Uri baseAddress = new Uri("http://localhost:808 ...

  2. C# App.config 详解

      读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...

  3. C# App.config全攻略

    读语句:          String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration ...

  4. App.config的学习笔记

    昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config不大可能了. WP具体支持API请查 ...

  5. App.config

      App.config的学习笔记 昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config ...

  6. c#配置文件app.config 与 Settings.settings

    本篇博客将介绍C#中Settings的使用.参考:https://docs.microsoft.com/zh-cn/visualstudio/ide/managing-application-sett ...

  7. App.config配置详解

    经上一篇文章https://www.cnblogs.com/luna-hehe/p/9104701.html发现自己对配置文件很是不了解,同样还是查了半天终于发现另一片宝贵文档https://www. ...

  8. C# 如何添加自定义键盘处理事件 如何配置app.config ? | csharp key press event tutorial and app.config

    本文首发于个人博客https://kezunlin.me/post/9f24ebb5/,欢迎阅读最新内容! csharp key press event tutorial and app.config ...

  9. C# App.config 自定义 配置节

    1)App.config <?xml version="1.0" encoding="utf-8" ?><configuration>  ...

随机推荐

  1. 套接字I/O模型-重叠I/O

    重叠模型的基本设计原理是让应用程序使用重叠的数据结构,一次投递一个或多个WinsockI/O请求.针对那些提交的请求,在它们完成之后,应用程序可为它们提供服务.模型的总体设计以Windows重叠I/O ...

  2. 怎么用CorelDRAW插入、删除与重命名页面

    在绘图之前,页面的各种设置也是一项重要的工作,本文主要讲解如何在CorelDRAW中插入.删除.重命名页面等操作.在CorelDRAW的绘图工作中,常常需要在同一文档中添加多个空白页面,删除一些无用的 ...

  3. 树莓派 2 win 10 IOT

    Setting up Windows 10 for IoT on your Raspberry Pi This week at the BUILD conference in San Francisc ...

  4. TKinter之菜单

    菜单的分类也较多,通常可以分为下拉菜单.弹出菜单. 1.使用Menu类创建一个菜单 2.add_command添加菜单项,如果该菜单是顶层菜单,则添加的菜单项依次向右添加. 如果该菜单时顶层菜单的一个 ...

  5. .NET_RSA加密全接触(重、难点解析)

    .NET_RSA加密全接触(重.难点解析) .NET Framework提供了两个类供我们使用RSA算法,分别是:用于加密数据的RSACryptoServiceProvider和用于数字签名的DSAC ...

  6. 【linux】/etc/fstab修复

    /etc/fstab在修改后,如果配置错误直接重启的话会导致系统崩溃.建议大家重启前执行mount -a ,mount -a是自动挂载 /etc/fstab 里面的东西.若不慎重启了,会出现开机错误, ...

  7. Android外派(安卓外派) — 长年提供安卓开发工程师外派业务(可签合同)

    北京动点飞扬长年提供安卓工程师外派业务. 平均技术情况如下: 1.2~3年以上Android平台开发经验2.熟练掌握java技术,熟悉面向对象编程设计3.熟悉Android应用开发框架及Activit ...

  8. 剑指offer系列50--不用加减乘除做加法

    [题目]写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号 * [思路]1 不计进位,直接位运算(异或方式可实现此运算,即1+0 0+1为1,0+0 1+1位0) * 2 与 ...

  9. IntelliJ IDEA常用快捷键

    双击shift 项目所有目录查找 ctrl+f 文件查找特定内容 ctrl+shift+f 项目查找包含特定内容的文件 ctrl+n 新建类 ctrl+shift+n 查找文件 ctrl+e  最近的 ...

  10. Install and configure Intel NIC teaming on R420

    OS env: windows2008 R2 std 1. Download NIC driver from Dell Website http://www.dell.com/support/home ...