将app.config中的WCF绑定重写为代码

时间:2016-07-19 12:58:16

标签: c# wcf app-config wcf-client custom-binding

我尝试将app.config中的WCF自定义绑定重写为代码。

的App.config

var binding = new CustomBinding();

var issuedTokenParameters = new IssuedSecurityTokenParameters();
issuedTokenParameters.KeyType = SecurityKeyType.BearerKey;
issuedTokenParameters.TokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0";

XNamespace trust = "http://docs.oasis-open.org/ws-sx/ws-trust/200512";

var secondaryParameters = new XElement(trust + "SecondaryParameters",
                                       new XElement(trust + "TokenType") { Value = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0" },
                                       new XElement(trust + "KeyType") { Value = "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Bearer" });
issuedTokenParameters.AdditionalRequestParameters.Add(secondaryParameters.ToXmlElement());

issuedTokenParameters.IssuerAddress = new EndpointAddress("XXX");

var securityElement = SecurityBindingElement.CreateIssuedTokenOverTransportBindingElement(issuedTokenParameters);

securityElement.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Default;
securityElement.IncludeTimestamp = true;
securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
securityElement.LocalClientSettings.DetectReplays = false;
securityElement.LocalServiceSettings.DetectReplays = false;

binding.Elements.Add(securityElement);
binding.Elements.Add(new TextMessageEncodingBindingElement());
binding.Elements.Add(new HttpsTransportBindingElement());

return binding;

代码:

{{1}}

代码绑定不好,因为当我调用服务时,我收到错误:

  

System.ServiceModel.FaultException:包含Action''的消息由于ContractFilter m,无法在接收器处理   在EndpointDispatcher上ismatch。这可能是因为合同不匹配(发件人和r之间的操作不匹配)   eceiver)或发送方和接收方之间的绑定/安全性不匹配。检查发送方和接收方是否具有相同的co       ntract和相同的绑定(包括安全要求,例如消息,传输,无)。

有什么建议吗?我找不到代码绑定中的错误或遗漏。

0 个答案:

没有答案