2012-01-13 140 views
1

我想部署一個WCF服務到我的服務器,它的工作方式就像我希望它在本地一樣。但在服務器上,我收到了404消息。JSON WCF部署服務器上的服務404

這是個什麼樣子,當我打電話給我的測試方法本地,如:

Image of the wanted result

當我部署它,我仍然可以成功瀏覽到:

www.my域name.com/ Service1.svc

,但是當我去:

www.my域name.com/Service1.svc/test

我收到404錯誤。什麼可能導致這個? 這是所有相關代碼:

IService1.cs

namespace HighscoreWebService 
{ 
[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "Test", 
     ResponseFormat = WebMessageFormat.Json, 
     BodyStyle = WebMessageBodyStyle.Bare)] 
    string Test(); 
} 
} 

Service1.svc.cs

namespace HighscoreWebService 
{ 
public class Service1 : IService1 
{ 
    public string Test() 
    { 
     return "Hello world!"; 
    } 
} 
} 

Web.config中的部分

<system.serviceModel> 
<services> 
    <service name="HighscoreWebService.Service1" 
      behaviorConfiguration="jsonRestDefault"> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http:/xxxxxx"/> 
     </baseAddresses> 
    </host> 
    <endpoint name="jsonRestEndpoint" 
       behaviorConfiguration="RESTFriendly" 
       binding="webHttpBinding" 
       contract="HighscoreWebService.IService1"> 
    </endpoint> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="jsonRestDefault"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true"/> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="RESTFriendly"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

我懷疑我在web.config中犯了錯誤。我對asp.net非常陌生,所以我可能犯了一些初學者的錯誤。但是因爲這在本地工作,所以我猜可能是服務器配置有問題。或者在web.config中我需要做的一些事情來適應服務器配置。

感謝您閱讀本文。

回答

0

嘗試在您的web.config端點中添加一個「地址」標記。這裏是我在我的WCF測試項目中的一個例子。儘管我會認爲它應該如何運作。這可能聽起來很愚蠢,但請確保您對服務提出的請求實際上是HTTP GET。也許發佈您的客戶端代碼,如果有任何不起作用。

<endpoint binding="webHttpBinding" bindingConfiguration="testBinding" contract="ASMXtoWCF.IWcf" 
     address="test" behaviorConfiguration="RestServiceBehavior">  
     </endpoint> 
+0

謝謝你的迴應!我嘗試添加地址=「測試」,但現在當我在本地導航時,我收到一條消息:「找不到端點」 – Lage 2012-01-13 20:57:05

+0

您可以發佈您用於獲取該服務的客戶端代碼嗎? – Etch 2012-01-13 21:02:00

+0

我還沒有使用客戶端,首先嚐試讓它在瀏覽器中工作。就像在本地工作一樣。 – Lage 2012-01-13 21:31:31

0

您必須添加到您的merhodTest行爲接受GET請求(默認情況下它只接受POST)。要做到這一點通過添加下列屬性你的方法實現添加WebInvoke行爲給它,例如(這是一個行爲)

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)] 

順便說一句,你可以從你的網頁中刪除主機標籤的配置是不必要的

+0

但我有WebGet不WebInvoke?我應該改變嗎?有區別嗎? 謝謝 – Lage 2012-01-17 23:29:01