【问题标题】:Accessing WCF REST operation using WebChannelFactory when the operation has UriTemplate specified当操作指定了 UriTemplate 时,使用 WebChannelFactory 访问 WCF REST 操作
【发布时间】:2012-08-06 05:46:43
【问题描述】:

按照下面的代码 sn-p,我有一个 WCF Rest 服务

[ServiceContract]
public interface IService
{
     [OperationContract]
        [WebInvoke(UriTemplate="/SaveList/Foos/",Method="POST")]
        bool SaveList(List<Foo> myFoos);

}

该服务是自托管服务,并使用 webHttpBinding 公开一个端点。我有一个客户端控制台应用程序,它具有以下代码 sn-p:

public void Send()
{
   var myObj = new Foo{Id=1, Name="Test"};
   using (WebChannelFactory<OurService.Services.IOurService> cf = new WebChannelFactory<IOurService>("WebHttpBinding_IService"))
   {
       IUtranetService channel = cf.CreateChannel();
       using (new OperationContextScope(channel as IContextChannel))
       {
           var status = channel.SaveList(new[] { myObj });

        }

    }

} 

客户端代码抛出异常如下: 在“http://localhost:1133/Service/SaveList”处没有可以接受消息的端点侦听。

我不确定我哪里出错了。如果我从服务合同中删除 UriTemplate,客户端会返回正确的响应。

任何帮助将不胜感激。

问候 鲁奇特拉

【问题讨论】:

  • 你不需要 method=post - webinvoke 已经是一个帖子
  • 但删除 method=post 并不能解决问题。我在这里做错什么了吗?

标签: wcf rest uritemplate webchannelfactory


【解决方案1】:

您的 URI 模板不正确。您需要将其更改为:

 [WebInvoke(UriTemplate="/SaveList/Foos/SaveList",Method="POST")] 

它不会自动附加您的方法名称。

根据我留下的评论也仅供参考,您不需要 Method="Post"

【讨论】:

  • 那也没有解决问题,我仍然遇到同样的异常:-(
  • 并且您的 Foo 对象被标记为 [DataContract] 等 - 或者至少是可序列化的,对吧?
  • 再次道歉 - 应该只是 UriTemplate ="/SaveList"
  • 是的,我的 Foo 对象标有 DataContract 属性。在这种情况下,它将与没有 UriTemplate 相同。但我想使用 UriTemplate 提供一个嵌套的 Uri。这可能吗?
  • 当然 - 但您需要更改您正在调用的网址。
猜你喜欢
  • 2015-08-10
  • 2014-02-04
  • 1970-01-01
  • 2011-12-15
  • 2013-04-02
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
相关资源
最近更新 更多