为什么不';我的POST方法不起作用
本文关键字:POST 方法 不起作用 我的 为什么不 | 更新日期: 2023-09-27 17:59:41
我有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动作。如果现在,了解更多详细信息。
我知道当我浏览到URL中的GET方法并传递正确的参数时,我会得到xml,但我的POST方法不起作用。我从pluralsight复制了这个例子,唯一的区别是尝试将服务托管在.svc文件中,而不是服务主机应用程序中。。。
有人能给我指正确的方向吗?
看起来服务的地址是错误的
您应该张贴到http://localhost:52587/Api/Content/VLSContentService.svc/submit
UriTemplate是相对于作为的端点的地址
http://localhost:52587/Api/Content/VLSContentService.svc
将这行代码更改为
WebChannelFactory cs=新WebChannelFactory(新Uri("http://localhost:52587/Api/Content/VLSContentService.svc/"));
您似乎发布到了错误的URL。该错误显示您发布到相对地址"/SubmitVideoPOST/submit",但该方法的UriTemplate仅为"/submit"。
对于基于REST的请求,您不需要在URL中包含.NET方法名称。只有UriTemplate很重要。WCF REST UriTemplate处理引擎为您完成了到正确运行时方法的映射。