2

I'd like to get the baseaddress specified in my app.config file which is "http://localhost:8733/SmgService/" from C#.

I looked into Microsoft's documents however, currently I don't know how to access this baseAddress from the C# code behind. Any help is appreciated.

 <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
          <add key="aspnet:UseTaskFriendlySynchronizationContext"value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" />
      </system.web>
      <system.serviceModel>
        <services>
              <service name="SmgService.PrintService">
                <host>
                  <baseAddresses>
                    <add baseAddress = "http://localhost:8733/SmgService/"/><!--i need this address-->
                  </baseAddresses>
                </host>
                <endpoint address="" binding="basicHttpBinding" contract="SmgService.IPrintService">
                  <identity>
                    <dns value="localhost"/>
                  </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
              </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
  </system.serviceModel>

</configuration>
  • you need this on service side or on the client side ? – Manoj Choudhari May 08 '19 at 08:51
  • Service Side . behind code on c# – Uğur Turna May 08 '19 at 08:54
  • The config file has to be in the same folder as the executable. On a server where clients only have GUEST privileges the config file file read permission has to be set to allow GUEST to read the file unless the executable service is being run as ADMIN – jdweng May 08 '19 at 09:09
  • BASER address is a string an can be changed dynamically at run time like any other string. So I normally would use the string method SUBSTRING like following : string input = "http://localhost:8733/SmgService/hello"; string baseAddress = input.Substring(0, input.LastIndexOf("/")); – jdweng May 08 '19 at 09:30
  • This address dynamically – Uğur Turna May 08 '19 at 09:55

1 Answers1

3

Solved

 ServicesSection servicesSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
        foreach (ServiceElement service in servicesSection.Services)
        {
            if (service.Name == "SmgService.PrintService")
            {
                foreach (BaseAddressElement item in service.Host.BaseAddresses)
                {                       
                    MessageBox.Show(item.BaseAddress.ToString());
                }

            }
        }







    string resultBase = "";
        ServicesSection services = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
        foreach (ServiceElement service in services.Services)
        {
            if (service.Name == whichService)
            {
                BaseAddressElementCollection baseElement = service.Host.BaseAddresses;
                foreach (BaseAddressElement item in baseElement)
                {
                    resultBase = item.BaseAddress;
                }

            }
        }
        return resultBase;