简体   繁体   中英

How to locate the correct Service Endpoint Uri

I am working on an application which is deployed to a TEST and then a LIVE webserver. I want the class library I am working on to use the correct service endpoint when it is deployed. Currently the code is as follows;

                    var data = new SettingsViewModel()
                    {
                        ServiceURI = Constants.LIVE_ENDPOINT_SERVICE_ADDRESS,
                        AutoSync = Constants.DEFAULT_AUTO_SYNC,
                        AppDataFolder = Path.Combine(ApplicationData.Current.LocalFolder.Path, Constants.ROOT_FOLDER, Constants.DATA_FOLDER),
                        MapKey = Constants.BASIC_MAP_KEY,
                        Logging = false
                    };
#if DEBUG
                    data.ServiceURI = Constants.DEV_ENDPOINT_SERVICE_ADDRESS;
#endif

As you can see, this can only pick up the DEV or the LIVE endpoints. This code cannot distinguish whether the webserver is LIVE or TEST I thought about setting up an App.Config file and get the correct Endpoint from there. But when I create a new item, the Config template is not listed. So how do I do this?

For now I could propose this solution :

public static class Constants
{
     public static string GetEndPoint()
     {
         // Debugging purpose
         if (System.Diagnose.Debug.IsAttached)
         {
            return DEV_ENDPOINT_SERVICE_ADDRESS;
         }
         else if ( Environment.MachineName == "Test Server"  ) // You need to know your test server machine name at hand.
         {
            return "Return test Server endpoint"
         }
         else
         {
            return "Return live server endpoint";
         }         
     }
}

You can used it in your SettingsViewModel like this:

var data = new SettingsViewModel()
{
  ServiceURI = Constants.GetEndPoint(),
  AutoSync = Constants.DEFAULT_AUTO_SYNC,
  AppDataFolder = Path.Combine(ApplicationData.Current.LocalFolder.Path, Constants.ROOT_FOLDER, Constants.DATA_FOLDER),
  MapKey = Constants.BASIC_MAP_KEY,
  Logging = false
};

The drawback for this solution is, if you change your test server you need to change is manually in your code.

Having done some research I realise I need to clarify something. The application I am working on is a Windows RT application and this does not allow config files. The solution I am meant to use is to use local settings, but these do not reference an external file like an App.Config. If I want to change the location of an EndPoint then I am going to have to specify where that is in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM