Converting an endpoint from a config file to code -
01-02-2008
, 01:25 PM
As a baseline, I have a working client and service, each of which uses an
app.config file to specify the connections in the <system.serviceModel>
section.
What I need to do, however, is connect to the WCF service via a COM
interface. As I see it, my client then does not have access to the
configuration file so I must convert it to equivalent code. Below I show the
original <system.serviceModel> section from app.config, followed by the code
I have written so far. I have not found how to hook in the <endpointBehavior>.
As I write this the service is down so I cannot run my latest trial;
however, the error from my prior run was:
"System.IdentityModel.Selectors.PolicyValidationEx ception: The incoming
policy could not be validated." That led me to think I might need to
explicitly say something about the endpointBehavior...
Am I on the right track? What code am I missing? I should mention that this
is my first plunge into WCF and it is still quite murky to me.
The original configuration:
========================================
<system.serviceModel>
<client>
<endpoint address="http://.../Services/ClientService"
behaviorConfiguration="ClientServiceBehavior"
binding="wsFederationHttpBinding"
bindingConfiguration="ClientServiceHttpBinding"
contract="...Service.WCF.Interfaces.IClientService "
name="ClientServiceHttpBinding_IClientService">
<identity>
<dns value="MyServices.com" />
</identity>
</endpoint>
</client>
<bindings>
<wsFederationHttpBinding>
<binding name="ClientServiceHttpBinding"
maxReceivedMessageSize="524288">
<security mode="Message">
<message algorithmSuite="Default" issuedKeyType="SymmetricKey"
issuedTokenType="http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1"
negotiateServiceCredential="true">
<issuer
address="http://.../Services/UsernameSecurityTokenService"
binding="wsHttpBinding"
bindingConfiguration="http://.../Services/UsernameSecurityTokenService">
<identity>
<dns value="AIQSTSServiceAuthorization.com" />
</identity>
</issuer>
<issuerMetadata address="http://.../Services/metaData" />
</message>
</security>
</binding>
</wsFederationHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="ClientServiceBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="PeerOrChainTrust"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
========================================
The code version:
========================================
WSFederationHttpBinding b = new WSFederationHttpBinding();
b.Security.Mode = WSFederationHttpSecurityMode.Message;
b.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
b.Security.Message.NegotiateServiceCredential = true;
b.Security.Message.IssuedKeyType = SecurityKeyType.SymmetricKey;
b.Security.Message.IssuedTokenType =
"http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#samlv1.1";
EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity(DNS_IDENTITY);
b.Security.Message.IssuerAddress =
new EndpointAddress(new Uri(SECURITY_ISSUER_ADDRESS), identity);
b.Security.Message.IssuerMetadataAddress =
new EndpointAddress(new Uri(SECURITY_ISSUER_METADATA_ADDRESS), identity);
b.MaxReceivedMessageSize = 524288;
ContractDescription contract = new ContractDescription(CONTRACT_NAME);
ServiceEndpoint m_endpoint = new ServiceEndpoint(
contract, b, new EndpointAddress(MY_URI));
ChannelFactory<IClientService> factory =
new ChannelFactory<IClientService>(m_endpoint);
factory.Credentials.UserName.UserName = USER_NAME;
factory.Credentials.UserName.Password = PASSWORD;
_service = factory.CreateChannel();
ICommunicationObject channel = (ICommunicationObject)_service;
if (channel.State == CommunicationState.Closed) { channel.Open(); }
======================================== |