Configuring WCF Client with external configuration files
- Dominant language
- C#
- Stars
- 1.8k
- Forks
- 576
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 2
Description
We have WCF Service and Client application written in .NET Framework. Now, there is a requirement to upgrade client application to .NET Core. We learned that the WCF is not supported in .NET Core. And hence we started using this WCF Client NuGet packages in our Client application. So far, it worked very well. But, currently it does not support configuring WCF client via external configuration files which is crucial as our customers often use different configuration based on their requirement.
Since there is no official support for using configuration files in .NET Core, could you please advise on how the client application in .NET Core can still configure WCF client with configuration outside of code? Currently, I had started writing below code that reads configuration file and applies changes to binding programmatically. But looks like it would get more complex as I try to handle more WCF settings.
var xmlDoc = new XmlDocument();
xmlDoc.Load(configPath);
var endpointNodes = xmlDoc.SelectNodes("//system.serviceModel/client/endpoint");
for (int i = 0; i < endpointNodes.Count; i++)
{
var endpointNode = endpointNodes[i];
var addressAttr = endpointNode.Attributes["address"].Value;
var bindingAttr = endpointNode.Attributes["binding"].Value;
var bindingConfigAttr = endpointNode.Attributes["bindingConfiguration"].Value;
if (bindingAttr == null)
continue;
XmlNode bindingNode = xmlDoc.SelectSingleNode($"//system.serviceModel/bindings/{bindingType}/binding[@name='{bindingConfiguration}']");
switch (bindingType)
{
case "basicHttpBinding":
binding = new BasicHttpBinding();
ConfigureBasicHttpBinding((BasicHttpBinding)binding, bindingNode);
break;
case "netTcpBinding":
binding = new NetTcpBinding();
ConfigureNetTcpBinding((NetTcpBinding)binding, bindingNode);
break;
}
}
}
public static void ConfigureNetTcpBinding(NetTcpBinding binding, XmlNode bindingNode)
{
if (bindingNode == null) return;
foreach (XmlAttribute attribute in bindingNode.Attributes)
{
switch (attribute.Name)
{
case "maxBufferSize":
binding.MaxBufferSize = int.Parse(attribute.Value);
break;
case "maxReceivedMessageSize":
binding.MaxReceivedMessageSize = long.Parse(attribute.Value);
break;
// Add more cases to handle other attributes as needed
}
}
var securityNode = bindingNode.SelectSingleNode("security");
if (securityNode != null)
{
foreach (XmlAttribute attribute in securityNode.Attributes)
{
switch (attribute.Name)
{
case "mode":
binding.Security.Mode = (SecurityMode)Enum.Parse(typeof(SecurityMode), attribute.Value, true);
break;
// Add more cases to handle other security attributes as needed
}
}
var transportNode = securityNode.SelectSingleNode("transport");
if (transportNode != null)
{
foreach (XmlAttribute attribute in transportNode.Attributes)
{
switch (attribute.Name)
{
case "clientCredentialType":
binding.Security.Transport.ClientCredentialType = (TcpClientCredentialType)Enum.Parse(typeof(TcpClientCredentialType), attribute.Value, true);
break;
// Add more cases to handle other transport security attributes as needed
}
}
}
}
}
Contributor guide
Assessment
This issue has not been assessed yet.