2012-07-16 96 views
0

我正在創建一個簡單的WCF Restful服務。目前,當我瀏覽到:localhost/AzamSharpService.svc時,它顯示了我可以檢查WSDL的Web服務默認頁面。WCF Restful Services URI模板不工作

我想要瀏覽到localhost/AzamSharpService.svc/LatestArticles並從GetLatestArticles方法獲取json。目前,當瀏覽到/最新文章網址它說沒有找到頁面。

實現如下所示:

[ServiceContract] 
    public interface IAzamSharpService 
    { 
     [OperationContract] 
     [WebGet(BodyStyle = WebMessageBodyStyle.Bare, RequestFormat =WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, UriTemplate = "/LatestArticles")] 
     List<ArticleContract> GetArticles(); 
    } 

public class AzamSharpService : IAzamSharpService 
    { 
     public List<ArticleContract> GetArticles() 
     { 
      var articles = new List<ArticleContract>() 
           { 
            new ArticleContract() {Title = "iOS"}, 
            new ArticleContract() { Title="Android"}, 
            new ArticleContract() { Title = "Windows 7"} 

           }; 
      return articles; 
     } 
    } 

的配置如下:

<system.serviceModel> 

     <services> 
     <service name="AzamSharpNewLook.AzamSharpService"> 
      <endpoint address="AzamSharpService.svc" 
        binding="webHttpBinding" 
        contract="AzamSharpNewLook.IAzamSharpService" 
        behaviorConfiguration="webby"/> 

      </service> 
     </services> 
     <behaviors> 

      <endpointBehaviors> 
      <behavior name="webby"> 
       <webHttp/> 
      </behavior> 
      </endpointBehaviors> 

      <serviceBehaviors> 
       <behavior name=""> 
        <serviceMetadata httpGetEnabled="true" /> 
        <serviceDebug includeExceptionDetailInFaults="false" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <serviceHostingEnvironment multipleSiteBindingsEnabled="false" /> 
    </system.serviceModel> 

回答

1

幾件事情嘗試...設置端點地址空字符串...在webHttp節點嘗試啓用幫助...並且您應該能夠導航到localhost/AzamSharpService.svc/help並獲取更多信息。最後,我會使用fiddler並構建一個獲取請求到適當的地址,然後只檢查響應,你應該有你需要的。希望這可以幫助...

+0

我設置地址爲空字符串,它只是工作。那麼,是什麼原因? – azamsharp 2012-07-16 21:32:50

+0

我相信默認情況下WCF是自編址的。如果您按照您的方式添加地址,則只會將其映射到localhost/AzmSharpService.svc,這不是您在Uri模板中所做的。事實上,在.NET 4.0中,我不相信你需要一個服務條目,並且默認情況下爲所實施的每個服務合同創建一個端點。 http://msdn.microsoft.com/en-us/library/ee530014.aspx – Hcabnettek 2012-07-16 21:48:40