我刚开始使用 WCF REST Starter Kit。
我创建了一个返回对象数组的简单服务。
使用浏览器,一切正常,但是当我使用 WCF 客户端时,我得到一个 ArgumentException。
我没有使用 IIS,代码如下:
合同:
[ServiceContract]
public interface IGiftService {
[WebGet(UriTemplate="gifts")]
[OperationContract]
List<Gift> GetGifts();
}
public class GiftService : IGiftService {
public List<Gift> GetGifts() {
return new List<Gift>() {
new Gift() { Name = "1", Price = 1.0 },
new Gift() { Name = "2", Price = 1.0 },
new Gift() { Name = "3", Price = 1.0 }
};
}
}
[DataContract]
public class Gift {
[DataMember]
public string Name { get; set; }
[DataMember]
public double Price { get; set; }
}
启动服务:
WebServiceHost2 host = new WebServiceHost2(
typeof(GiftService),
true,
new Uri("http://localhost:8099/tserverservice"));
host.Open();
Console.WriteLine("Running");
Console.ReadLine();
host.Close();
启动客户端:
WebChannelFactory<IGiftService> factory = new WebChannelFactory<IGiftService>(
new Uri("http://localhost:8099/tserverservice"));
IGiftService service = factory.CreateChannel();
List<Gift> list = service.GetGifts();
Console.WriteLine("-> " + list.Count);
foreach (var item in list) {
Console.WriteLine("-> " + item.Name);
}
服务器和客户端在同一个解决方案中,我在两者中使用相同的接口(描述服务合同)。
异常显示:“名称为 'UriTemplateMatchResults' 的属性已存在。” 这就是堆栈跟踪:
触发异常的类-> Microsoft.ServiceModel.Web.WrappedOperationSelector
堆栈跟踪:
at System.ServiceModel.Channels.MessageProperties.UpdateProperty(String name, Object value, Boolean mustNotExist)
at System.ServiceModel.Channels.MessageProperties.Add(String name, Object property)
at System.ServiceModel.Dispatcher.WebHttpDispatchOperationSelector.SelectOperation(Message& message, Boolean& uriMatched)
at System.ServiceModel.Dispatcher.WebHttpDispatchOperationSelector.SelectOperation(Message& message)
at Microsoft.ServiceModel.Web.WrappedOperationSelector.SelectOperation(Message& message) in C:\Program Files\WCF REST Starter Kit\Microsoft.ServiceModel.Web\WrappedOperationSelector.cs:line 42
at Microsoft.VisualStudio.Diagnostics.ServiceModelSink.ServiceMethodResolver.GetOperation()
at Microsoft.VisualStudio.Diagnostics.ServiceModelSink.ServiceMethodResolver..ctor(ContractDescription contract, DispatchRuntime runtime, Message request, InstanceContext instanceContext)
我究竟做错了什么?
更新:我禁用了帮助页面,该服务现在正在运行。它是一个错误吗?
host.EnableAutomaticHelpPage = false;
谢谢!
安德烈·卡鲁奇