WCF - 如何创建自定义的编程与通过HTTP二进制编码绑定(S) [英] WCF - how to create programatically custom binding with binary encoding over HTTP(S)
问题描述
我想转换我当前的HTTP / HTTPS的WCF绑定设置使用二进制消息编码和我需要做的是在code - 不是在XML配置。据我所知,有必要建立CustomBinding对象,并设置适当的BindingElements,但我无法弄清楚什么元素,我应该在我的情况下使用。
I'd like to convert my current HTTP/HTTPS WCF binding settings to use binary message encoding and I need to do it in code - not in XML configuration. AFAIK it's necessary to create CustomBinding object and set proper BindingElements, but I'm not able to figure out what elements should I use in my scenario.
在我的WCF配置要点是:
Main points in my WCF configuration are:
- 使用HTTP或HTTPS传输取决于配置(app.config中)
- 使用的用户名信息安全
- TODO:补充,而不是默认的文本二进制编码
我目前的code设置结合起来(的工作,但没有二进制编码):
My current code for setting the binding up (working, but without the binary encoding):
var isHttps = Settings.Default.wcfServiceBaseAddress.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase);
var binding = new WSHttpBinding(isHttps ? SecurityMode.TransportWithMessageCredential : SecurityMode.Message);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
我尝试这个code,但它不工作 - 我不知道如何设置邮件安全元素的用户名信息的安全性:
I was trying this code, but it doesn't work - I don't know how to set message security element for username message security:
var custBinding = new CustomBinding();
custBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
//Transport Security (Not Required)
if (isHttps)
{
custBinding.Elements.Add(SecurityBindingElement.CreateUserNameForSslBindingElement());
}
//Transport (Required)
custBinding.Elements.Add(isHttps ?
new HttpsTransportBindingElement() :
new HttpTransportBindingElement());
任何人知道如何设置呢?我试图搜寻类似问题/溶液,但没有成功...
Anybody knows how to set this up? I tried to search for similar problem/solution, but didn't succeeded...
推荐答案
我几乎忘了这个问题,但这里是我这二进制绑定在HTTP上使用的用户名+密码验证,同时也能自定义绑定类允许打开的GZip COM $对... p $ pssion
I almost forgot this question, but here is my custom binding class which works with binary binding over HTTP with username+password validation and also allows to turn GZip compression on...
public class CustomHttpBinding: CustomBinding
{
private readonly bool useHttps;
private readonly bool useBinaryEncoding;
private readonly bool useCompression;
private readonly HttpTransportBindingElement transport;
public CustomHttpBinding(bool useHttps, bool binaryEncoding = true, bool compressMessages = false)
{
this.useHttps = useHttps;
transport = useHttps ? new HttpsTransportBindingElement() : new HttpTransportBindingElement();
useBinaryEncoding = binaryEncoding;
useCompression = compressMessages;
}
public long MaxMessageSize{set
{
transport.MaxReceivedMessageSize = value;
transport.MaxBufferSize = (int) value;
}}
public override BindingElementCollection CreateBindingElements()
{
BindingElement security;
if (useHttps)
{
security = SecurityBindingElement.CreateSecureConversationBindingElement(
SecurityBindingElement.CreateUserNameOverTransportBindingElement());
}
else
{
security = SecurityBindingElement.CreateSecureConversationBindingElement(
SecurityBindingElement.CreateUserNameForSslBindingElement(true));
}
MessageEncodingBindingElement encoding;
if (useCompression)
{
encoding = new GZipMessageEncodingBindingElement(useBinaryEncoding
? (MessageEncodingBindingElement)
new BinaryMessageEncodingBindingElement()
: new TextMessageEncodingBindingElement());
}
else
{
encoding = useBinaryEncoding
? (MessageEncodingBindingElement) new BinaryMessageEncodingBindingElement()
: new TextMessageEncodingBindingElement();
}
return new BindingElementCollection(new[]
{
security,
encoding,
transport,
});
}
}
这篇关于WCF - 如何创建自定义的编程与通过HTTP二进制编码绑定(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!