2008-12-18 70 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調試器的整合,但他們似乎是系統 - 這個解決方案是本地的。

相關問題