没有得到WCF REST服务的预期结果(新手)
问题描述:
我是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";
}
}
}
答
如果我理解正确的话,你可以看到你在以下网址
http://localhost:8080/greeting
为了现在打电话给你的端点WSDL链接,你需要把它添加到URL这样
http://localhost:8080/greeting/GetGreeting/
我不完全知道为什么你有UriTemplate事在那里虽然除了我的猜测,你可能只是复制粘贴从一个例子。除非你有特定的查询字符串参数需要定义,否则你并不需要它,它往往会使事情复杂化,所以我建议把它取出来。这意味着你的界面看起来像这样...
[ServiceContract]
public interface IGreetingService
{
[OperationContract]
[WebGet]
string GetGreeting();
}
...然后你可以失去最后的“/”的网址。
答
我找出问题所在。当我使用url:“http:// localhost:8080/greeting”时,服务器发送临时页面。当我在url的末尾添加反斜杠“/”时,它会执行我的服务。 因此,“http:// localhost:8080/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