Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load WCF service by environment in .net core project

I have a problem while adding WCF in the .NET core project. When I used .net in the past I can add multiple environments in web.config so I can load the correct web service at runtime (Dev, Rec, Prod).

The problem in the .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.

like image 881
Adouani Riadh Avatar asked Aug 10 '18 09:08

Adouani Riadh


People also ask

Can we call WCF service from .NET Core?

To use WCF services in . NET Core, you need to create a proxy client of the required service. Proxy is actually a contract equivalent of actual service and contains complete details of the Interface and method exposed by WCF service. One can also use the Channel factory technique to connect to the WCF service easily.

How do I add a WCF service reference in a NET Core 3.1 application?

NET Standard project, this option is available when you right-click on the Dependencies node of the project in Solution Explorer and choose Manage Connected Services.) On the Connected Services page, select Add Service Reference. The Add service reference page opens. Select WCF Web Service, and then choose Next.

How do I call a connected service in C#?

In the Project Type pane, select a Visual C# application. In the Templates pane, select a Windows Forms Application. Enter a project name such as CallingServiceExample, and browse to a storage location. In the Solution Explorer, right-click the project name and click Add Service Reference.


1 Answers

As I understand from this article, this is Microsoft's recommendation:

  1. Add new class file
  2. Add same Namespace of service reference.cs
  3. Add Partial Class to expand reference service class (declared in Reference.cs)
  4. And Partial Method to implement ConfigureEndpoint() (declared in Reference.cs)
  5. Implement ConfigureEndpoint() Method by Setting a new value for Endpoint

Example:

namespace Your_Reference_Service_Namespace
{
    public partial class Your_Reference_Service_Client
    {
        static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
        {
            serviceEndpoint.Address = 
                new System.ServiceModel.EndpointAddress(new System.Uri("http://your_web_service_address"), 
                new System.ServiceModel.DnsEndpointIdentity(""));
        }
    }
}
  1. Here, you can take the value from the appsettings.json file

    new System.Uri(configuration.GetValue("yourServiceAddress")

like image 55
Refael Avatar answered Sep 18 '22 17:09

Refael