Best way to manage multiple Service References in .NET

后端 未结 3 831
南笙
南笙 2021-02-06 18:45

I have a C# ASP.NET project with 15 WCF/ASMX Service References. Each service is deployed to three different servers; test, staging and live

3条回答
  •  我寻月下人不归
    2021-02-06 18:51

    I've had a lot of problems with this, too. But I ended up in finding a nice and easy way. My example with a fictious service address:

    • Create a command line EXE
    • Add service reference with address https://service.somePortal.com/FooConnector.svc and namespace "MyServiceReference"
    • Let's say the service is offering an Interface "IFooConnector". Choose OK to generate (a lot of) code to consume the service.

    After that, in your App.config file you will see a new serviceModel section:

    
        
            
                
                    
                    
                
            
        
        
            
        
    
    

    You can now use a service method like this:

    using TestClient.MyServiceReference;
    
    namespace TestClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var client = new FooConnector())
                {
                    client.DoSomething();
                }
            }
        }
    }
    

    Now the important part:

    To use three inkarnations of the same service, like DEV (Development), TEST (Testing) and PROD (Production) at different addresses, but having the same interface, you only have to manually edit your App.config and use a different constructor to instantiate the client!


    Here's the new App.config with the changed serviceModel section:

    
        
            
                
                    
                    
                
            
        
        
            
    
            
    
            
        
    
    

    As you see, we're now having three endpoint sections with different service addresses. I've also changed the endpoints name properties to match my desired DEV, TEST and PROD naming.

    To call the required service you can now use a different constructor of the client, having one parameter: string endpointConfigurationName. So you can now use the same service method in its three inkarnations like this:

    using TestClient.MyServiceReference;
    
    namespace TestClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var client = new FooConnector("DEV"))
                {
                    //Call method in DEV
                    client.DoSomething();
                }
    
                using (var client = new FooConnector("TEST"))
                {
                    //Call method in TEST
                    client.DoSomething();
                }
    
                using (var client = new FooConnector("PROD"))
                {
                    //Call method in PROD
                    client.DoSomething();
                }
            }
        }
    }
    

    That's it! :-)

    P.S.: In my real project, I have an enum user setting to choose DEV, TEST, PROD to change the used service by configuration.

提交回复
热议问题