我是WCF Web Services的新手。 我正在尝试测试我的简单hello world Web服务。目前,我正在做自托管。我已启动主机应用程序,打开浏览器并键入资源地址。 我还运行了 Fiddler 并使用作曲家创建了一个请求。 在这两种情况下,我都会得到"您已创建服务"页面,其中包含指向我的.wsdl的链接。
我期待在我的回复或包含"......你好世界"。
我错过了什么?还是我只是误解了这个过程?
应用配置
版<?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();
}
}
}
}
你好世界合同和服务
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 链接
http://localhost:8080/greeting
为了现在调用您的终端节点,您需要像这样将其添加到 url 中
http://localhost:8080/greeting/GetGreeting/
我不完全确定为什么你在那里有 UriTemplate 的东西,除了我猜测你可能只是从示例中复制粘贴它。 除非您有想要定义的特定查询字符串参数,否则您并不真正需要它,并且它往往会使事情复杂化,因此我建议将其删除。 这意味着您的界面看起来像这样...
[ServiceContract]
public interface IGreetingService
{
[OperationContract]
[WebGet]
string GetGreeting();
}
。然后,您可能会丢失 URL 上的最后一个"/"。
我找出了问题所在。 当我使用 url 时:"http://localhost:8080/greeting"服务器会发送临时页面。 当我在 url 末尾添加反斜杠"/"时,它会执行我的服务。因此,"http://localhost:8080/greeting/"有效并向我发送"...你好世界"回来了。