2017-04-05 66 views
0

我一直面對三天的難題。我有一個WCF web api(.svc服務文件)部署到IIS 8.5。該應用程序正常工作,然後,突然間,我改變了代碼內的其他小事情後,出現這個問題:我可以從服務器內部(使用本地主機)連接到應用程序。我可以使用服務器IP地址從外部連接。但我無法通過任何方式使用服務器主機名從外部連接。通過以下示例可以最好地理解問題:無法使用主機名訪問WCF服務IIS

注意:應用程序是主網站內的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

你知道我應該怎麼做才能解決這個bug?

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

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

回答

0

我終於把它解決了!對於那些無法成功解決這個問題的人來說:亞馬遜EC2負載平衡是我的煩惱的原因。

碰巧負載平衡的建立是爲了過濾所有請求,並使用傳輸層的SSL安全協議(HTTPS),並將它們發送到服務器的機器。因此,如果我要請求http://myserver.com/servico Amazon Load Balance將在內部重定向到https://machine-n/servico,其中'n'是使用的機器(在我的情況下,我有兩臺機器)。

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

這也解釋了爲什麼一切工作的時候,我要求直接使用HTTP使用本機的IP地址:負載均衡並沒有在這種情況下起到任何作用過濾請求,並使用HTTPS redireting。

所以,我們在這裏有我的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> 
... 
0

有一個名爲host文件的文件。當您嘗試請求一個DN窗口查看該文件時。 C:\WINDOWS\System32\drivers\etc裏面有主文件。在你的主機文件中添加如下所示(使用管理員模式)。

server_ip_address  myhostname.com 
+1

您好,我剛纔編輯自己的帖子有新的信息:同一網站內的其他應用程序在IIS上工作得很好!甚至還有用C#編寫的其他Web API,就像這裏一樣,它們都正常工作。所以,我想這個問題不是Windows,而是web.config中的一些東西。 – mhkgalvez