Referencing Web Services in Different Environment

Julio Bello 221 Reputation points
2022-07-01T21:27:22.56+00:00

Hi, Everybody!...

I created a web service (i.e., MyService). The web service references other dependent web services (i.e., DependentService). I have three environments: LOCAL, TEST and LIVE. I develop my code in the LOCAL environment (i.e., my computer). Then I deploy to the TEST environment (i.e., a TEST server) for testing. Finally, when testing completes successfully, I deploy to the LIVE environment (i.e., a LIVE server).

MyService has an Environment appSettings in the Web.config file. I use this appSettings to tell MyService which drive is the DataDrive and which database to talk to in each environment.

 <appSettings>  
 <!--ENVIRONMENT (LOCAL/TEST/LIVE)-->  
 <add key="Environment" value="LOCAL"/>  
 <!--DATA DRIVE-->  
 <add key="DataDriveLOCAL" value="C:"/>  
 <add key="DataDriveTEST" value="D:"/>  
 <add key="DataDriveLIVE" value="D:"/>  
 </appSettings>  
 <connectionStrings>  
 <!-- LOCAL/TEST/CLOUD -->  
 <add name="OpsConnectionStringLOCAL" connectionString="<LOCAL Connection String>" providerName="System.Data.SqlClient"/>  
 <add name="OpsConnectionStringTEST" connectionString="<TEST Connection String>" providerName="System.Data.SqlClient"/>  
 <add name="OpsConnectionStringLIVE" connectionString="<LIVE Connection String>"System.Data.SqlClient" />  
 </connectionStrings>  
  

I wish to do the same with the dependent web services. However, when I add a dependent web service, a lot of code (Reference.cs) and settings (Settings.Designer.cs) are generated automagically "under the hood".

Reference.cs

//------------------------------------------------------------------------------  
// <auto-generated>  
//     This code was generated by a tool.  
//     Runtime Version:4.0.30319.42000  
//  
//     Changes to this file may cause incorrect behavior and will be lost if  
//     the code is regenerated.  
// </auto-generated>  
//------------------------------------------------------------------------------  
  
//   
// This source code was auto-generated by Microsoft.VSDesigner, Version 4.0.30319.42000.  
//   
#pragma warning disable 1591  
  
namespace MyService.DependentService  
{  
    using System.Diagnostics;  
    using System;  
    using System.Xml.Serialization;  
    using System.ComponentModel;  
    using System.Web.Services.Protocols;  
    using System.Web.Services;  
  
  
    /// <remarks/>  
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "4.8.4084.0")]  
    [System.Diagnostics.DebuggerStepThroughAttribute()]  
    [System.ComponentModel.DesignerCategoryAttribute("code")]  
    [System.Web.Services.WebServiceBindingAttribute(Name = "ServiceSoap", Namespace = "http://MyDomain.com/")]  
    public partial class Service : System.Web.Services.Protocols.SoapHttpClientProtocol  
    {  
        // <SNIP/>  
        public Service()  
        {  
            this.Url = global::MyService.Properties.Settings.Default.MyService_DependentService;  
            if ((this.IsLocalFileSystemWebService(this.Url) == true))  
            {  
                this.UseDefaultCredentials = true;  
                this.useDefaultCredentialsSetExplicitly = false;  
            }  
            else  
            {  
                this.useDefaultCredentialsSetExplicitly = true;  
            }  
        }  
        // <SNIP/>  
    }  
}  
  
#pragma warning restore 1591  
  

Settings.Designer.cs

//------------------------------------------------------------------------------  
// <auto-generated>  
//     This code was generated by a tool.  
//     Runtime Version:4.0.30319.42000  
//  
//     Changes to this file may cause incorrect behavior and will be lost if  
//     the code is regenerated.  
// </auto-generated>  
//------------------------------------------------------------------------------  
  
namespace MyService.Properties  
{  
  
  
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]  
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")]  
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase  
    {  
  
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));  
  
        public static Settings Default  
        {  
            get  
            {  
                return defaultInstance;  
            }  
        }  
  
        [global::System.Configuration.ApplicationScopedSettingAttribute()]  
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]  
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.WebServiceUrl)]  
        [global::System.Configuration.DefaultSettingValueAttribute("http://localhost:/MyService/DependentService/v1.asmx")]  
        public string MyService_DependentService  
        {  
            get  
            {  
                return ((string)(this["MyService_DependentService"]));  
            }  
        }  
    }  
}  
  

So my question is the following... What is the best way to achieve the desired result? Should I create three web references for the dependent service, one for each environment and use the Environment appSettings in the Web.config file to tell MyService which web reference to use? Or is there a way to "override" MyService.Properties.Settings.Default.MyService_DependentService in a similar manner where Web.config can be overridden by a "sub-Web.config" file?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,400 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,280 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,141 Reputation points
    2022-07-02T14:02:18.7+00:00

    I' a little confused by your post because creating a service reference add a web.config node with the URL.

          <endpoint address="http://localhost:49455/WebService.asmx" binding="basicHttpBinding"  
            bindingConfiguration="WebServiceSoap" contract="ServiceReference1.WebServiceSoap"  
            name="WebServiceSoap" />  
        </client>  
    

    You should use web.config transforms to generate the correct configuration per environment.

    Transform web.config

    If you still want to use the current approach then you'll need to set the remote URL programmatically where the proxy is instantiated.

    ServiceReference1.WebServiceSoapClient client = new ServiceReference1.WebServiceSoapClient();  
    client.Endpoint.Address = new EndpointAddress(yourUriFromConfiguration);  
    

    There's also constructor overloads that you can use but the non-standard way you're using configuration might make this approach moot.


1 additional answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2022-07-04T06:29:10.133+00:00

    Hi @Julio Bello ,
    You could create proxy classes. This creates a class instance which you can use in your project. Once you have the file you can just update the value of the variable URL

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments