2012-08-14 77 views
0

我是WCF Web服務的新手。我試圖測試我簡單的hello world web服務。 現在,我正在做自我託管。我處於啓動主機應用程序的位置,打開瀏覽器並在我的資源中輸入地址。我也運行了Fiddler並使用Composer創建了一個請求。在這兩種情況下,我都會看到「您創建了一項服務」。頁面上有一個鏈接到我的.wsdl。沒有得到WCF REST服務的預期結果(新手)

我曾期待在我的Response或具有「... Hello world」的網頁中看到「Hello World」文本。

我錯過了什麼?或者我只是誤解了這個過程?

App.Config中

<?xml version="1.0"?> 
<configuration> 

    <system.serviceModel> 
     <services> 
     <service name="My.Core.Services.GreetingService" behaviorConfiguration="MyServiceTypeBehaviors"> 
      <host> 
       <baseAddresses> 
       <add baseAddress="http://localhost:8080/greeting"/> 
       </baseAddresses> 
      </host> 
      <endpoint name="GreetingService" binding="webHttpBinding" contract="My.Core.Services.IGreetingService"/> 
      <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
     </service> 
    </services> 

    <behaviors> 
     <serviceBehaviors> 
      <behavior name="MyServiceTypeBehaviors" > 
       <serviceMetadata httpGetEnabled="true" /> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration> 

主機代碼

using System; 
using System.ServiceModel; 
using My.Core.Services;  

namespace My.Service.Host 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (var host = new ServiceHost(typeof(GreetingService))) 
      { 
       host.Open(); 
       Console.WriteLine("The service is ready."); 
       Console.WriteLine("Press <ENTER> to terminate service."); 
       Console.WriteLine(); 
       Console.ReadLine(); 
       host.Close(); 
      } 
     } 
} 

}

的Hello World合同和服務

using System.ServiceModel; 
using System.ServiceModel.Web; 

namespace My.Core.Services 
{ 
    [ServiceContract] 
    public interface IGreetingService 
    { 
     [OperationContract] 
     [WebGet(UriTemplate = "/")] 
     string GetGreeting(); 
    } 
} 

using System.Collections.Generic; 

namespace My.Core.Services 
{ 
    public class GreetingService : IGreetingService 
    { 
     public string GetGreeting() 
     { 
      return "Greeting...Hello World"; 
     } 
    } 
} 

回答

1

如果我理解正確的話,你可以看到你在以下網址

http://localhost:8080/greeting 

爲了現在打電話給你的端點WSDL鏈接,你需要把它添加到URL這樣

http://localhost:8080/greeting/GetGreeting/ 

我不完全知道爲什麼你有UriTemplate事在那裏雖然除了我的猜測,你可能只是複製粘貼從一個例子。除非你有特定的查詢字符串參數需要定義,否則你並不需要它,它往往會使事情複雜化,所以我建議把它取出來。這意味着你的界面看起來像這樣...

[ServiceContract] 
public interface IGreetingService 
{ 
    [OperationContract] 
    [WebGet] 
    string GetGreeting(); 
} 

...然後你可以失去最後的「/」的網址。

+0

不,我看到一個有鏈接的頁面,我可以點擊查看.wsdl文件。它說:「你已經創建了一個服務,爲了測試這個服務,你需要創建一個客戶端並用它來調用服務,你可以使用svcutil.exe工具從命令行使用以下語法來執行此操作:svcutil。 exe文件http:// localhost:8080/greeting?wsdl「 – 2012-08-15 12:54:16

+0

好的,在這種情況下,您可能希望將端點添加到網址,就像我之前提到的那樣,但沒有GreetingService.svc文件。我會更新原來的答案。 – sanpaco 2012-08-15 17:23:01

0

我找出問題所在。當我使用url:「http:// localhost:8080/greeting」時,服務器發送臨時頁面。當我在url的末尾添加反斜槓「/」時,它會執行我的服務。 因此,「http:// localhost:8080/greeting /」起作用並將「... Hello World」發回給我。