我是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