用于服务合同会话的 wsHttpBinding 应用程序配置

本文关键字:wsHttpBinding 应用程序 配置 会话 服务 合同 用于 | 更新日期: 2023-09-27 18:32:15

Hallo 我想用户SessionMode = SessionMode.Required,我读到 WCF 服务必须具有wsHttpBinding,因为支持会话。现在我做了一个配置app.config,我在 Windows 窗体中做了一个函数来启动服务。当我单击按钮并调用StartWCFServer()时,我收到以下异常:

InvalidOperationException was unhandled

Could not find a base address that matches scheme https for the endpoint with binding WSHttpBinding. Registered base address schemes are [http].

这是我的代码:

应用配置

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="WCF_Server.WCFService">
        <endpoint  address="" behaviorConfiguration="web" binding="wsHttpBinding" bindingConfiguration="wsHttpBindings" contract="WCF_Server.IWCFService"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <serviceDebug httpsHelpPageEnabled="true" httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="wsHttpBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindings">
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

StartWCFServer()

     ServiceHost host;
     private void StartWCFServer()
            {
                if (host == null)
                {
                    Uri baseAddress = new Uri("http://localhost:8000/");
                    host = new ServiceHost(typeof(WCFService), baseAddress);
                    host.AddServiceEndpoint(typeof(IWCFService), new WSHttpBinding("wsHttpBinding"), "Services");
                        host.Open();//<-- Exception here
                }
        }

用于服务合同会话的 wsHttpBinding 应用程序配置

在这里您将其设置为 <add binding="wsHttpBinding" scheme="https"/>但在主机中使用Uri baseAddress = new Uri(http+ your site's URL);

所以把scheme="https"改成scheme="http"