簡體   English   中英

如何將本地主機 wcf 服務與 asp.net 4.0 連接

[英]how to connect localhost wcf service with asp.net 4.0

我剛剛創建了一個 wcf 服務來為我的本地 web 項目提供一些功能。 這是我的本地主機 wcf 服務 web 配置。

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/staff_care_wcf_ws/RestServiceImpl.svc"
        behaviorConfiguration="WebHttp" binding="webHttpBinding" bindingConfiguration="LargeWeb"
        contract="SC_WCF.IRestServiceImpl" />
    </client>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

最后這是我的本地 web 站點 web 配置,用於處理本地托管的 wcf 服務。

<system.serviceModel>
     <bindings>
      <basicHttpBinding>
        <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/staff_care_wcf_ws/RestServiceImpl.svc"
        behaviorConfiguration="WebHttp" binding="basicHttpBinding" bindingConfiguration="LargeWeb"
        contract="SC_WCF.IRestServiceImpl">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
     <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  <protocolMapping>
      <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
      </webScriptEndpoint>
    </standardEndpoints>
  </system.serviceModel>

這怎么行不通。 發生以下錯誤:

The endpoint at 'http://localhost/staff_care_wcf_ws/RestServiceImpl.svc' does not have a Binding with the None MessageVersion.  'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings."}

這里出了什么問題請幫幫我這些家伙.....

更新

在此處輸入圖像描述

需要刪除endpointBehaviors節點中的webHttp ,因為IIS中用於服務部署的基地址不是“http://localhost/staff_care_wcf_ws/RestServiceImpl.svc”。 您的 WCF 服務只有一個 basicHttpsBinding 端點。 您的客戶端配置文件應如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8000/Service1.svc"
          behaviorConfiguration="WebHttp" binding="basicHttpBinding" name="webHttpBinding_IService1"
          contract="ConsoleApp1.IService1">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WebHttp">
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpBinding" scheme="http" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
        <webScriptEndpoint>
            <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
        </webScriptEndpoint>
    </standardEndpoints>
</system.serviceModel>
</configuration>

如果將WCF服務部署到IIS,其基地址應該如下:

在此處輸入圖像描述

在此處輸入圖像描述

所以你的WCF配置文件和下面的配置文件效果一樣:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHttp">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

ProtocolMapping 可以簡化配置,它會幫助你生成一個 basicHttpsBinding 端點。

如果問題仍然存在,請隨時告訴我。

更新

您的配置文件有點混亂,我建議按照以下步驟重新創建部署在 IIS 中的 WCF 服務:

創建一個 WCF 服務應用項目:

在此處輸入圖像描述

這是項目目錄:

在此處輸入圖像描述

服務的接口保存在IService文件中,服務的實現class保存在Service1文件中。

將web.config中的內容替換為如下配置信息:

<?xml version="1.0"?>
<configuration>

    <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.7.2" />
        <httpRuntime targetFramework="4.7.2"/>
    </system.web>

    <system.serviceModel>
        <services>
            <service name="WcfService2.Service1"
                     behaviorConfiguration="CalculatorServiceBehavior">
                <endpoint address=""
                          binding="basicHttpBinding"
                          contract="WcfService2.IService1"
                          bindingConfiguration="LargeWeb"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="CalculatorServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        
            <bindings>
                <basicHttpBinding>
                    <binding name="LargeWeb" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                        <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                    </binding>
                </basicHttpBinding>
            </bindings>
    

    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
        <directoryBrowse enabled="true"/>
    </system.webServer>

</configuration>

以上配置信息將生成一個 basicHttpBinding 端點。

發布到IIS:

在此處輸入圖像描述

使用瀏覽器訪問服務:

在此處輸入圖像描述

使用Add Service Reference生成代理類來調用服務:

在此處輸入圖像描述

這樣會自動生成代理class和配置文件,直接調用即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM