Before 2019
I used to generate the clients to consume WCF services with Visual Studio 2017 or Rider (the behavior is the same). What happens is that the generated client inherits of System.ServiceModel.ClientBase
and I can pass an endpoint configuration to this ClientBase
through the generated client. The generated code looks like:
public partial class GeneratedSoapClient : ClientBase<GeneratedSoapClient>
{
public GeneratedSoapClient (string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
// ...
}
That allows to add some endpoints in the host's app.config
:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="MySoap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://dev.my_service.net?asmx"
binding="basicHttpBinding" bindingConfiguration="MySoap"
name="MyService_Dev" />
<endpoint address="http://prod.my_service.net?asmx"
binding="basicHttpBinding" bindingConfiguration="MySoap"
name="MyService_Prod" />
</client>
</system.serviceModel>
Then I construct my client with either "MyService_Dev"
or "MyService_Prod"
to call the dev or prod service.
After 2019
With Visual Studio 2019, the code generation is different. It does not generate svcmap
and webref
files. Instead, it generates a single json
file with the information.
In the C# code, the generated client does not accept an endpoint config as a string anymore, now it takes an enum, and then it resolves the endpoint directly in the code:
public enum EndpointConfiguration
{
ServiceHttpSoap11Endpoint,
ServiceHttpSoap12Endpoint,
}
class GeneratedSoapClient : ClientBase<GeneratedSoapClient>
{
public GeneratedSoapClient(EndpointConfiguration endpointConfiguration) :
base(/* Binding, not related to my issue*/, GetEndpointAddress(endpointConfiguration))
{
}
private static GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
if (endpointConfiguration == EndpointConfiguration.ServiceHttpSoap11Endpoint)
{
return new EndpointAddress("http://prod.my_service.net?asmx");
}
if (endpointConfiguration == EndpointConfiguration.ServiceHttpSoap12Endpoint)
{
return new EndpointAddress("http://prod.my_service.net?asmx");
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
}
In this situation, I cannot configure my own endpoint because I prefer not to modify the generated code as that is a bad practice. It would require to apply the same "patch" after the regeneration everytime the contract changes. In this situation,
How should I use the newest WCF client generator to allow to configure the client endpoint?
As a precision, I did not find any option in the wizard to keep using a string as an endpoint configuration.
GeneratedSoapClient
with your instance of configuredEndpointConfiguration
– Outrank