NetCoreApp2.0项目调用WCF服务遇System.ServiceModel.Configuration缺失问题咨询
Answers to Your WCF + .NET Core 2.0 Questions
Let me break this down clearly based on your two core questions:
1. Can you target net461 and use .NET Core 2.0 features simultaneously?
Yes, you can set up multi-targeting in your project to build for both netcoreapp2.0 and net461, but with some important caveats:
How to set it up:
Modify your project file to specify both target frameworks, and use conditional references/packages for each:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFrameworks>netcoreapp2.0;net461</TargetFrameworks> </PropertyGroup> <!-- .NET Framework 4.6.1 specific dependencies --> <ItemGroup Condition="'$(TargetFramework)' == 'net461'"> <PackageReference Include="System.ServiceModel.Configuration" Version="4.5.0" /> </ItemGroup> <!-- .NET Core 2.0 specific dependencies --> <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'"> <PackageReference Include="System.ServiceModel.Primitives" Version="4.5.0" /> </ItemGroup> </Project>
Use conditional compilation to separate code paths:
You’ll need to wrap framework-specific code with preprocessor directives to avoid conflicts:
#if NET461 // Use System.ServiceModel.Configuration as you did in .NET Framework var channelFactory = new ChannelFactory<IMyWcfService>("MyWcfEndpointName"); #else // .NET Core 2.0 code: configure WCF via code var binding = new BasicHttpBinding(); var endpoint = new EndpointAddress("https://your-wcf-service-url.svc"); var channelFactory = new ChannelFactory<IMyWcfService>(binding, endpoint); #endif // Shared code to call the WCF service var client = channelFactory.CreateChannel(); var result = await client.MyServiceMethodAsync();
Pros & Cons:
- Pros: You get to use .NET Core 2.0 features (like improved performance, cross-platform support, new ASP.NET Core middleware) when running on
netcoreapp2.0, while retaining the familiarSystem.ServiceModel.Configurationsetup fornet461. - Cons: You’ll have to maintain two code paths, which adds some complexity to your project. Not all .NET Core 2.0 features will work in the
net461build (since it’s still running on the full .NET Framework).
2. How to work around missing System.ServiceModel.Configuration in netcoreapp2.0?
The official .NET Core WCF client libraries (like System.ServiceModel.Primitives) intentionally exclude System.ServiceModel.Configuration because .NET Core favors code-first configuration over XML-based app.config/web.config setups. Here are your best options:
Option 1: Configure WCF directly in code (recommended)
This is the standard approach for .NET Core. You manually define bindings and endpoints in your code instead of reading from a config file:
// Configure your binding settings var binding = new BasicHttpBinding(); binding.MaxReceivedMessageSize = 10485760; // 10MB binding.Security.Mode = BasicHttpSecurityMode.Transport; // Use HTTPS // Define the service endpoint var endpointAddress = new EndpointAddress("https://your-wcf-service-url.svc"); // Create the channel factory and client var channelFactory = new ChannelFactory<IMyWcfServiceContract>(binding, endpointAddress); var client = channelFactory.CreateChannel(); // Call the service var response = await client.GetDataAsync(123);
Option 2: Map custom config to WCF settings
If you prefer keeping configuration in a file, use Microsoft.Extensions.Configuration (the standard .NET Core config system) to read from appsettings.json, then map those values to your WCF setup:
- Create a config class to represent your WCF settings:
public class WcfServiceSettings { public string ServiceUrl { get; set; } public int MaxReceivedMessageSize { get; set; } public bool UseHttps { get; set; } }
- Add settings to
appsettings.json:
{ "WcfServiceSettings": { "ServiceUrl": "https://your-wcf-service-url.svc", "MaxReceivedMessageSize": 10485760, "UseHttps": true } }
- Load the config and build your WCF client:
// Load settings from appsettings.json var wcfSettings = Configuration.GetSection("WcfServiceSettings").Get<WcfServiceSettings>(); // Build binding from config values var binding = new BasicHttpBinding(); binding.MaxReceivedMessageSize = wcfSettings.MaxReceivedMessageSize; binding.Security.Mode = wcfSettings.UseHttps ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None; // Create client var endpoint = new EndpointAddress(wcfSettings.ServiceUrl); var channelFactory = new ChannelFactory<IMyWcfServiceContract>(binding, endpoint);
Important Note:
Not all WCF configuration options from the full .NET Framework are supported in .NET Core 2.0. Advanced features like custom behaviors or certain binding types may require workarounds or alternative implementations.
内容的提问来源于stack exchange,提问作者ChrisCa

