c# - WCF REST 入门工具包 - 名称为 'UriTemplateMatchResults' 的属性已存在

标签 c# wcf rest wcf-client

我刚开始使用 WCF REST 初学者工具包。

我创建了一个返回对象数组的简单服务。

使用浏览器,一切正常,但是当我使用 WCF 客户端时,我得到一个 ArgumentException。

我没有使用 IIS,这里是代码:

契约(Contract):

[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);
            }

服务器和客户端在同一个解决方案中,我在两者中使用相同的接口(interface)(以描述服务契约)。

异常显示:“名称为‘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;

谢谢!

安德烈·卡鲁奇

最佳答案

有同样的问题,禁用了帮助页面并修复了它。如果按顺序快速调用某些 REST url,则会抛出异常。在通话之间等待时没问题。

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new WebServiceHost2(serviceType, true, baseAddresses) {EnableAutomaticHelpPage = false};
        }

关于c# - WCF REST 入门工具包 - 名称为 'UriTemplateMatchResults' 的属性已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/379162/

相关文章:

c# - 为什么在实现 CompareTo 时必须重载运算符?

php - REST API 实现问题 php/JS

C# - 更换背包中的元素

C# autostart 自动将应用程序添加到启动文件夹

c# - 我可以将包含对象的列表转换为二维数组吗

c# - WCF:通用接口(interface)的序列化是否可能?

.net - ASP.NET 中的套接字编程?

c# - WCF 服务路由和 Tcp

javascript - ajax GET 函数在被另一个函数调用时不返回成功值

java - 如何使 Content-Type header 可选?