2011-12-12 33 views
0

即時通訊目前未能在與WebGet屬性測試一個簡單的WCF服務提出申請,按照本教程在http://msdn.microsoft.com/en-us/library/bb412178.aspx簡單的WCF服務 - 所有請求都返回空?

香港專業教育學院創建了一個空白的ASP.NET Web應用程序項目,香港專業教育學院則添加了名爲「測試」一個WCF服務,創建了test.svcItest.svc

我然後添加的方法 '你好':

public class test : Itest 
{ 
    public string hello(string s){return "hello " + s;} 
} 

它實現了接口:

[ServiceContract] 
public interface Itest 
{ 
    [OperationContract] 
    [WebGet] 
    string hello(string s); 
} 

然而,任何請求(HTTP的GET),以http://localhost/test.svc/hello?s=hi返回一個空頁。 事實上,根據該網址的任何請求返回一個空白頁面(即http://localhost/test.svc/abcdefg

我錯過了一些基本的東西?

編輯:web.config中完全從Visual Studio除了標準的產生:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBinding_Itest" /> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost/test.svc" binding="basicHttpBinding" 
     bindingConfiguration="BasicHttpBinding_Itest" contract="ServiceReference1.Itest" 
     name="BasicHttpBinding_Itest" /> 
    </client> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
+0

使用[WebGet]屬性,我不想與服務參考使用。 話雖如此,它使用wcftestclient.exe進行測試時仍然有效 – maxp

+0

我測試了其他瀏覽器並以編程方式創建了webrequest ...仍然得到一個空的響應。 – maxp

+0

嘗試將'WebGet'更新爲'[WebGet(UriTemplate =「foo」,BodyStyle = WebMessageBodyStyle.Bare)]' – oleksii

回答

2

您應該使用的WebHttpBinding而不是basicHttpBinding的 下面是從這裏的部分答案:testing wcf service in browser

這取決於你有什麼類型的WCF服務:

如果你使用WCF REST服務(webHttpBinding),那麼你應該能夠只需導航到服務地址,例如http://yourserver/somedir/service.svc

如果您使用其他任何東西,您有一個SOAP服務,並且您無法在Web瀏覽器中測試SOAP服務 - 瀏覽器只是不理解和說SOAP。然而,在您的C:\驅動器中有一個WCF測試客戶端應用程序,您可以將其用於此目的。

從我自己可以添加WCFTestCLient是在這裏: 「C:\ Program Files文件(x86)的\微軟的Visual Studio 10.0 \ Common7 \ IDE \ WcfTestClient.exe」

1

嘗試更換的web上面的部分與下面一個的.config:

<system.serviceModel>  
    <services> 
     <service name="ServiceReference1.test"> 
     <endpoint binding="webHttpBinding" contract="ServiceReference1.Itest" behaviorConfiguration="web"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 

你的服務器需要有服務元素,而不是客戶端元素你的web.config,如上圖所示。還使用webGet你需要使用的WebHttpBinding