2010-02-16 16 views
1

我嘗試在WCF聯合資源庫模板中添加OperationContract,但URI模板似乎不匹配(或者我可能缺少其他內容)。未能將操作合同添加到wcf聯合圖書館模板

當我嘗試訪問到http://myserver:8738/Design_Time_Addresses/SyndicationServiceLibrary2/ShowDocument?url=http://www.test.com

功能ShowDocument不火,我得到一個404錯誤。

任何幫助,將不勝感激。

IFeed1.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Syndication; 
using System.ServiceModel.Web; 
using System.Text; 

namespace SyndicationServiceLibrary2 
{ 
    // NOTE: If you change the interface name "IFeed1" here, you must also update the reference to "IFeed1" in App.config. 
    [ServiceContract] 
    [ServiceKnownType(typeof(Atom10FeedFormatter))] 
    [ServiceKnownType(typeof(Rss20FeedFormatter))] 
    public interface IFeed1 
    { 

     [OperationContract] 
     [WebGet(UriTemplate = "*", BodyStyle = WebMessageBodyStyle.Bare)] 
     SyndicationFeedFormatter CreateFeed(); 

     [OperationContract] 
     [WebInvoke(UriTemplate = "/ShowDocument?*", BodyStyle = WebMessageBodyStyle.Bare)] 
     int ShowDocument(); 

     // TODO: Add your service operations here 
    } 
} 

Feed1.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Syndication; 
using System.ServiceModel.Web; 
using System.Text; 

namespace SyndicationServiceLibrary2 
{ 
    // NOTE: If you change the class name "Feed1" here, you must also update the reference to "Feed1" in App.config. 
    public class Feed1 : IFeed1 
    { 
     public int ShowDocument() 
     { 
      int test = 0; 
      return test; 
     } 

     public SyndicationFeedFormatter CreateFeed() 
     { 
      // Create a new Syndication Feed. 
      SyndicationFeed feed = new SyndicationFeed("Feed Title", "A WCF Syndication Feed", null); 
      List<SyndicationItem> items = new List<SyndicationItem>(); 

      // Create a new Syndication Item. 
      SyndicationItem item = new SyndicationItem("An item", "Item content", new Uri("http://myserver:8738/Design_Time_Addresses/SyndicationServiceLibrary2/ShowDocument?url=http://www.test.com")); 

      items.Add(item); 
      feed.Items = items; 

      string query = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"]; 
      SyndicationFeedFormatter formatter = null; 

      if (query == "atom") 
      { 
       formatter = new Atom10FeedFormatter(feed); 
      } 
      else 
      { 
       formatter = new Rss20FeedFormatter(feed); 
      } 

      return formatter; 
     } 
    } 
} 
+0

@Bruno:如果您發佈代碼或XML,請**突出顯示這些行,並利用編輯器工具欄上的「代碼」按鈕(101 010)正確格式化這些行(並使它們很好地符合語法 - 也突出顯示) - 謝謝! – 2010-02-16 14:05:42

回答

0

現在無法自己試試吧 - 只是一些想法從我的頭頂:

你有沒有嘗試使用Url編碼參數?

http://......./ShowDocument?url=http%3a%2f%2fwww.test.com 

這可能幫助 - 使用HttpUtility.UrlEncode方法從System.Web命名空間URL,它需要進入的網址本身編碼(和解碼)的參數。

另一種想法:如何創建一個URL模板並將URL作爲字符串傳遞給ShowDocument方法?

[OperationContract] 
[WebInvoke(UriTemplate = "/ShowDocument?url={target}", BodyStyle = WebMessageBodyStyle.Bare)] 
int ShowDocument(string target); 

看看能不能工作呢?

0

我很慚愧地告訴你,我的問題是:

new Uri("http://myserver:8738/Design_Time_Addresses/SyndicationServiceLibrary2... 

其中「MYSERVER」是不好的一個。

我終於設法使用帶參數的url模板來做整件事情。