簡體   English   中英

無法使用主機名 IIS 訪問 WCF 服務

[英]Cannot access WCF service using hostname IIS

三天來,我一直面臨着一個棘手的問題。 我有一個部署到 IIS 8.5 的 WCF web api(.svc 服務文件)。 應用程序正常工作,然后,突然,在我更改了代碼內部的其他一些小東西后,出現了這個問題:我可以從服務器內部(使用本地主機)連接到應用程序。 我可以使用服務器 IP 地址從外部連接。 但是我無論如何都無法使用服務器主機名從外部連接。 通過下面的例子可以最好地理解這個問題:

注意:該應用程序是主網站內的 IIS 應用程序。 服務的 URI 是http://myhostname/api/servicos.svc

我搜索了許多網站和論壇,但都無濟於事。

我的 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="Off" />
  </system.web>

  <connectionStrings>
    <!-- My connection string goes here -->
  </connectionStrings>

  <system.serviceModel>
    <services>
      <service name="ServicoWeb.servico" behaviorConfiguration="Wcf_Behavior">
        <endpoint name="" binding="webHttpBinding" contract="ServicoWeb.IServico" />
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp automaticFormatSelectionEnabled="true" defaultBodyStyle="Bare" />
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>
        <behavior name="Wcf_Behavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors> 

    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="removeSvcDaUrl" type="ServicoWeb.Validacao.UrlCustomizacao, ServicoWeb" />
    </modules>    
    <directoryBrowse enabled="true" />
        <rewrite>        
            <rules>
                <remove name="WordPress: https://myhostname.com" />
            </rules>
        </rewrite>
        <handlers accessPolicy="Read, Execute, Script" />
        <security>
            <requestFiltering>
                <verbs>
                    <add verb="*" allowed="true" />
                </verbs>
            </requestFiltering>
        </security>
  </system.webServer>
</configuration>

嘗試從外部(通過主機名)訪問 API 時出現的錯誤是:

“/Api”應用程序中的服務器錯誤。

無法找到該資源。

描述:HTTP 404。您要查找的資源(或其依賴項之一)可能已被刪除、更改名稱或暫時不可用。 請檢查以下 URL 並確保其拼寫正確。

請求的 URL:/Api/servico.svc/asos/csv/09655055000104

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1069.1

你知道我應該怎么做才能修復這個錯誤嗎?

編輯:更多信息:IIS 中同一網站內的其他應用程序正常工作! 甚至還有其他用 C# 編寫的 web api,就像這里的這個一樣,它們都可以正常工作。

編輯2:另一件重要的事情是我可以訪問服務路徑,直到服務本身(servico.svc)。 因此,例如,當我嘗試訪問“ http://myhostname.com/api/servico.svc?wsdl ”時,我成功獲取了服務的元數據。 因此,只有當我嘗試訪問服務本身時,才會出現上述錯誤。

有一個名為主機文件的文件。 當您嘗試請求 DN 窗口時,請查看該文件。 C:\\WINDOWS\\System32\\drivers\\etc里面的那個文件有主機文件。 在您的主機文件中添加如下內容(使用管理員模式)。

server_ip_address      myhostname.com

我終於解決了! 對於那些在這個問題上苦苦掙扎但沒有成功的人:Amazon EC2 負載平衡是我遇到麻煩的原因。

碰巧設置了負載平衡來過濾所有請求,並使用傳輸層 SSL 安全協議 (HTTPS) 將它們發送到服務器機器。 因此,如果我要請求http://myserver.com/servico,亞馬遜負載平衡會在內部重定向到https://machine-n/servico ,其中“n”是所使用的機器(在我的情況下,我有兩台機器) .

因此,導致我在 WCF 中出現問題的原因是:我的 web.config 不允許該服務接受 HTTPS 請求。 一旦我解決了這個問題,一切正常。

它還解釋了為什么當我使用 HTTP 直接使用機器的 IP 地址請求時一切正常:負載平衡在這種情況下沒有發揮任何作用,過濾請求並使用 HTTPS 重新定向。

所以,這里有我的 web.config,現在允許 HTTPS 請求:

<?xml version="1.0" encoding="UTF-8"?>
...    
<system.serviceModel>
    <services>
        <service name="ServicoWeb.servico" behaviorConfiguration="Wcf_Behavior">
            <endpoint name="" binding="webHttpBinding" contract="IServico" bindingConfiguration="webHttpTransportSecurity" />
        </service>
    </services>
    <bindings>
        <webHttpBinding>
            <binding name="webHttpTransportSecurity">
                <security mode="Transport" />
            </binding>
        </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior>
                <webHttp automaticFormatSelectionEnabled="true" defaultBodyStyle="Bare" />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="Wcf_Behavior">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
...

在我的情況下,以下事情有所幫助:

  1. 檢查協議映射是否正確綁定。

     <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping>
  2. 檢查 443 端口是否打開並正常工作。

暫無
暫無

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

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