[英]Why doesn't my POST method work?
我有WCF服务合同:
[ServiceContract]
public interface IVLSContentService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetCategoriesGET/{userIdArg}", BodyStyle = WebMessageBodyStyle.Bare)]
List<Video> GetVideosGET(string userIdArg);
[WebInvoke(Method = "POST",BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate = "submit")]
[OperationContract]
void SubmitVideoPOST(Video videoArg, string userId);
}
我有实施合同的服务:
public class VLSContentService : IVLSContentService
{
List<Video> catsForUser1 = new List<Video>();
List<Video> catsForUser2 = new List<Video>();
public List<Video> GetVideosGET(string userIdArg)
{
List<Video> catsToReturn = new List<Video>();
if (Int32.Parse(userIdArg) == 1)
{
catsToReturn = catsForUser1;
}
else if (Int32.Parse(userIdArg) == 2)
{
catsToReturn = catsForUser2;
}
return catsToReturn;
}
public void SubmitVideoPOST(Video videoArg, string userId)
{
int i = 0;
}
}
我有配置:
<system.serviceModel>
<services>
<service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
<endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="VLSContentServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="VLSContentServiceEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
我试图用以下客户端代码调用POST WCF操作:
static void Main(string[] args)
{
WebChannelFactory<IVLSContentService> cs = new WebChannelFactory<IVLSContentService>(new Uri("http://localhost:52587/Api/Content/VLSContentService.svc/SubmitVideoPOST"));
IVLSContentService client = cs.CreateChannel();
Video videoToAdd = new Video("My First Video");
client.SubmitVideoPOST(videoToAdd,"1");
}
但我得到这个错误,我无法解决原因:
在http:// localhost:52587 / Api / Content / VLSContentService.svc / SubmitVideoPOST / submit中没有可以接受该消息的端点 。 这通常是由错误的地址或SOAP操作引起的。 有关更多详细信息,请参阅InnerException(如果存在)。
我知道当我浏览到URL中的GET方法时,我传递了正确的参数,我得到了xml,但我的POST方法不起作用。 我已经复制了来自pluralsight的例子,唯一的区别是你试图在.svc文件而不是服务主机应用程序中托管服务...
任何人都能指出我正确的方向吗?
您似乎发布到错误的网址。 错误显示您发布到相对地址“/ SubmitVideoPOST / submit”,但您的该方法的UriTemplate只是“/ submit”。
您不需要在基于REST的请求的URL中包含.NET方法名称。 只有UriTemplate很重要。 WCF REST UriTemplate处理引擎为您完成了映射到正确的运行时方法。
看起来你的服务地址有误
您应该发布到http:// localhost:52587 / Api / Content / VLSContentService.svc / submit
UriTemplate是相对于端点的地址
HTTP://本地主机:52587 /原料药/内容/ VLSContentService.svc
将此行代码更改为
WebChannelFactory cs = new WebChannelFactory(new Uri(“http:// localhost:52587 / Api / Content / VLSContentService.svc /”));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.