我是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";
}
}
}
不,我看到一個有鏈接的頁面,我可以點擊查看.wsdl文件。它說:「你已經創建了一個服務,爲了測試這個服務,你需要創建一個客戶端並用它來調用服務,你可以使用svcutil.exe工具從命令行使用以下語法來執行此操作:svcutil。 exe文件http:// localhost:8080/greeting?wsdl「 – 2012-08-15 12:54:16
好的,在這種情況下,您可能希望將端點添加到網址,就像我之前提到的那樣,但沒有GreetingService.svc文件。我會更新原來的答案。 – sanpaco 2012-08-15 17:23:01