当指定了WCF REST操作模板时,使用WebChannelFactory访问该操作
根据下面的代码片段,我有一个WCF Rest服务当指定了WCF REST操作模板时,使用WebChannelFactory访问该操作,wcf,rest,uritemplate,webchannelfactory,Wcf,Rest,Uritemplate,Webchannelfactory,根据下面的代码片段,我有一个WCF Rest服务 [ServiceContract] public interface IService { [OperationContract] [WebInvoke(UriTemplate="/SaveList/Foos/",Method="POST")] bool SaveList(List<Foo> myFoos); } [服务合同] 公共接口设备 { [经营合同] [WebInvoke(UriT
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(UriTemplate="/SaveList/Foos/",Method="POST")]
bool SaveList(List<Foo> myFoos);
}
[服务合同]
公共接口设备
{
[经营合同]
[WebInvoke(UriTemplate=“/SaveList/Foos/”,Method=“POST”)]
bool保存列表(列出myFoos);
}
该服务是一个自托管服务,并使用webHttpBinding公开端点。我有一个客户端控制台应用程序,它有以下代码段:
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 });
}
}
}
public void Send()
{
var myObj=newfoo{Id=1,Name=“Test”};
使用(WebChannel Factory cf=新的WebChannel Factory(“WebHttpBinding\u iSeries设备”))
{
IUtranetService通道=cf.CreateChannel();
使用(新OperationContextScope(通道作为IContextChannel))
{
var status=channel.SaveList(新[]{myObj});
}
}
}
客户端代码引发异常,如下所示:
“上没有端点侦听”http://localhost:1133/Service/SaveList“这可以接受这个信息
我不确定我会错在哪里。如果我从服务合同中删除该模板,客户机将返回正确的响应
任何帮助都将不胜感激
问候
Ruchitra您的URI模板不正确。您需要将其更改为:
[WebInvoke(UriTemplate="/SaveList/Foos/SaveList",Method="POST")]
它不会自动附加方法名
另外,根据我留下的评论,您不需要Method=“Post”您不需要Method=Post-webinvoke已经是Post了,但是删除Method=Post不会解决问题。我在这里做的有什么不对吗?这也没有解决问题,我仍然得到相同的异常:-(并且您的Foo对象被标记为[DataContract]等-或者至少是可序列化的,对吗?再次道歉-应该是UriTemplate=“/SaveList”是的,我的Foo对象被标记为DataContract属性。在这种情况下,这与没有UriTemplate是一样的。但是我想使用UriTemplate提供一个嵌套的Uri。这可能吗?当然可以,但是您需要更改您正在调用的url。