【问题标题】:How to get Service Base Address in app.config?如何在 app.config 中获取服务基地址?
【发布时间】:2019-09-25 23:02:37
【问题描述】:

我想从 C# 中获取我的 app.config 文件中指定的基地址,即“http://localhost:8733/SmgService/”。

但是,我查看了 Microsoft 的文档,目前我不知道如何从后面的 C# 代码访问这个 baseAddress。任何帮助表示赞赏。

 <?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>

【问题讨论】:

  • 您需要在服务端还是客户端?
  • 服务端。 c#背后的代码
  • 配置文件必须与可执行文件位于同一文件夹中。在客户端只有 GUEST 权限的服务器上,必须将配置文件文件读取权限设置为允许 GUEST 读取文件,除非可执行服务以 ADMIN 身份运行
  • BASER 地址是一个字符串,可以像任何其他字符串一样在运行时动态更改。所以我通常会使用字符串方法 SUBSTRING,如下所示: string input = "localhost:8733/SmgService/hello"; string baseAddress = input.Substring(0, input.LastIndexOf("/"));
  • 这个地址是动态的

标签: c# xml wcf app-config servicemodelex


【解决方案1】:

解决了

 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2010-11-23
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多