为什么我的POST方法不起作用?

时间:2011-06-17 15:02:25

标签: c# wcf

我有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,if   目前,了解更多详情。

我知道当我浏览到URL中的GET方法时,我传递了正确的参数,我得到了xml,但我的POST方法不起作用。我复制了来自pluralsight的例子,唯一的区别是你试图在.svc文件而不是服务主机应用程序中托管服务......

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:3)

您似乎发布了错误的网址。错误显示您发布到相对地址“/ SubmitVideoPOST / submit”,但您的该方法的UriTemplate只是“/ submit”。

您不需要在基于REST的请求的URL中包含.NET方法名称。只有UriTemplate很重要。 WCF REST UriTemplate处理引擎为您完成了映射到正确的运行时方法。

答案 1 :(得分:3)

看起来您的服务地址错误

您应该发布到http://localhost:52587/Api/Content/VLSContentService.svc/submit

UriTemplate相对于端点的地址

http://localhost:52587/Api/Content/VLSContentService.svc

将此行代码更改为

WebChannelFactory cs = new WebChannelFactory(new Uri(“http:// localhost:52587 / Api / Content / VLSContentService.svc /”));