根据用户特定访问和安装 app.config

本文关键字:安装 app config 访问 用户 | 更新日期: 2023-09-27 18:31:04

我有两个用于https访问的app.config文件,另一个用于http访问,如果我有两个选项按钮,一个用于http,另一个用于https,如果用户选择第一个选项,即http如何在安装应用程序时存储在C:''program文件中,并参考我的应用程序仅使用httpsapp.config文件而不是httpapp.config文件。

或者我如何使我的 app.config 文件既适用于 https 又适用于 http。

下面是我的 app.config 文件,而目前正在为 http 工作

 <system.serviceModel>
    <standardEndpoints />
    <bindings>
      <webHttpBinding>
        <binding name="Bind1" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="900000" maxBufferSize="900000" />
      </webHttpBinding>
    </bindings>
    <services>
      <service name="PeripheralServiceImpl" behaviorConfiguration="SvcBhvr">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="EndPBhvr" bindingConfiguration="Bind1" contract="Server.Contract.IPeripheralService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:18732/Peripheral/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndPBhvr">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="false" defaultBodyStyle="Wrapped" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="SvcBhvr">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
  </system.serviceModel>

和 app.config 在 https 中工作

      <system.serviceModel>
        <standardEndpoints />
          <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
          <bindings>
                  <basicHttpBinding>
                      <binding name ="soapBinding">
                          <security mode="Transport">
                              <transport clientCredentialType="None"/>
                          </security>
                      </binding>
                  </basicHttpBinding>
                  <webHttpBinding>
                      <!--<binding name="httpBinding">-->
                          <binding name="Bind1" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="900000" maxBufferSize="900000" >
                          <security mode="Transport">
                              <transport clientCredentialType="None"/>
                          </security>
                      </binding>
                  </webHttpBinding>  
        </bindings>
        <services>
          <service name="PeripheralServiceImpl" behaviorConfiguration="SvcBhvr">
              <host>
                  <baseAddresses>
                      <add baseAddress="https://localhost:18732/Peripheral/" />
                  </baseAddresses>
              </host>
              <endpoint address="https://localhost:18732/Peripheral/" binding="webHttpBinding" behaviorConfiguration="EndPBhvr" bindingConfiguration="Bind1" 
                        contract="Server.Contract.IPeripheralService">
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />

          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="EndPBhvr">
                <webHttp /> 
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="SvcBhvr">
              <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

有人可以帮助我吗?

根据用户特定访问和安装 app.config

您可以通过为每个配置文件定义基址来为 http 和 https 使用单个配置文件。

我刚刚尝试了这个,它工作正常:

  <system.serviceModel>
    <services>
      <service name="MyTest.MyService" behaviorConfiguration="ServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:80/WCFtest/service.svc" />
            <add baseAddress="https://localhost:443/WCFtest/service.svc" />
          </baseAddresses>
        </host>
        <endpoint address="/soap" binding="basicHttpBinding" contract="MyTest.IMathService" />
        <endpoint address="/rest" binding="webHttpBinding" contract="MyTest.IMyService" behaviorConfiguration="REST" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="REST">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>