I have a problem while adding WCF in .NET core project. When I used .net in the past I can add multiple environment in web.config so I can load the correct web service at runtime (Dev, Rec, Prod).
The problem in .net core project when I added a reference of my WCF service as Connected Service it created one file ConnectedService.json that contains a url for the WCF service.
{
"ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
"Version": "15.0.20406.879",
"GettingStartedDocument": {
"Uri": "https://go.microsoft.com/fwlink/?linkid=858517"
},
"ExtendedData": {
"Uri": "*****?singleWsdl",
"Namespace": "Transverse.TokenService",
"SelectedAccessLevelForGeneratedClass": "Public",
"GenerateMessageContract": false,
"ReuseTypesinReferencedAssemblies": true,
"ReuseTypesinAllReferencedAssemblies": true,
"CollectionTypeReference": {
"Item1": "System.Collections.Generic.List`1",
"Item2": "System.Collections.dll"
},
"DictionaryCollectionTypeReference": {
"Item1": "System.Collections.Generic.Dictionary`2",
"Item2": "System.Collections.dll"
},
"CheckedReferencedAssemblies": [],
"InstanceId": null,
"Name": "Transverse.TokenService",
"Metadata": {}
}
}
My question how can I load the correct service based on the used environment.
Note.
In my Project, I did not have an appsettings neither web config. It is a .net core class library and it is called in ASP.NET core Application as Middleware.
Use a
ChannelFactory
to consume your service. WCF ChannelFactory vs generating proxyA
ChannelFactory
allows you to set anEndpointAddress
. How to: Use the ChannelFactoryThe URL for the endpoint can be loaded from a configuration file. In a more advanced setup a directory lookup for the service can be performed to retrieve the URL for the environment where the application is deployed. https://en.wikipedia.org/wiki/Service_provider_interface
https://github.com/jolmari/netcore-wcf-service-proxy
For whom are interested by the solution I added an endpoint for my service in each appseetings.{environment}.json and in Service class I inject new Instance of my service based on the environment variable ASPNETCORE_ENVIRONMENT
Maybe is not the best but it works fine.
As I understand from this article, this is Microsoft's recommendation:
ConfigureEndpoint()
Method by Setting a new value for EndpointExample:
Here, you can take the value from the
appsettings.json
filenew System.Uri(configuration.GetValue("yourServiceAddress")
I am using .Net Core 3.1, this is my way when I call WCF Service.
First, Get the endpoint remote address from appsettings.json
Second, Call web service client using that address in CTOR WCF Client Class parameter
Thanks in advance.