2008-12-18 68 views
3

我刚开始使用WCF REST入门工具包。WCF REST入门工具包 - 名称为'UriTemplateMatchResults'的属性已存在

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

使用浏览器,一切工作正常,但是当我使用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; 

谢谢!

安德烈·卡卢奇

+0

如果REST入门工具包的功能为您提供了所需的功能,那么请继续操作。如果你正在学习如何做REST,那就远离它吧。回到标准的RPC风格太容易了。 – 2009-02-10 01:31:21

回答

1

有同样的问题,禁用的帮助页面,并将其固定它。如果某个REST网址很快被按顺序调用,则会引发异常。在通话之间等待时很好。

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

我有同样的探头,但我想看看帮助页面,所以禁用它并不是我的解决方案。我发现WCF REST Toolkit中的URITemplating在它看到模板表中已经有这个模板时就会引发这些问题。基本上,只有当方法的URL根据请求的数据而不同时,才需要模板,毕竟这就是模板的用途。我的POST操作具有相同的URITemplates,因此导致此错误的单独查询之间的URL没有区别。然后我发现实际上根本不需要任何模板,至少对于POST操作,而且如果您的方法需要将复杂对象作为参数传递,则您不会通过URL进行POST查询。所以我从服务接口中的WebInvoke属性中删除了名为parameter的URITemplate参数,我认为解决了这个问题。当然,如果您对服务器进行GET查询并依赖于URITemplating,则您仍然需要忍受或离开帮助页面。

0

就我而言,只有在启用Visual Studio调试器集成的情况下使用WCF通道访问端点时才会出现问题。

我工作围绕这一问题通过添加一些代码从的ChannelFactory删除VS行为:

var vsBehaviour = channelFactory.Endpoint.EndpointBehaviors 
    .FirstOrDefault(i => 
     i.GetType().Namespace == "Microsoft.VisualStudio.Diagnostics.ServiceModelSink"); 
if (vsBehaviour != null) 
{ 
    channelFactory.Endpoint.Behaviors.Remove(vsBehaviour); 
} 

显然,还有其他的方法来禁用WCF Visual Studio调试器的整合,但他们似乎是系统 - 这个解决方案是本地的。

相关问题