简体   繁体   中英

Why doesn't my POST method work?

I have the WCF service contract:

[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);
}

And I have the service that implements the contract:

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;
    }
}

And I have the configuration:

  <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>

And I am trying to call the POST WCF operation with the following client code:

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");

}

But im getting this error and I cant work out why:

There was no endpoint listening at http://localhost:52587/Api/Content/VLSContentService.svc/SubmitVideoPOST/submit that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

I know when I browse to the GET method in a URL and I pass the correct parameters I am getting xml back but my POST method just doesnt work. Ive copied the example from pluralsight the only difference is um trying to host the service in .svc file instead of service host application...

Can anybody point me in the right direction?

You appear to be posting to the wrong URL. The error shows you posting to the relative address "/SubmitVideoPOST/submit", but your UriTemplate for that method is just "/submit".

You do not need to include the .NET method name in the URL for REST based requests. Only the UriTemplate matters. Mapping to the correct runtime method is done for you by the WCF REST UriTemplate processing engine.

Looks like you have the address of the service wrong

You should be posting to http://localhost:52587/Api/Content/VLSContentService.svc/submit

The UriTemplate is relative to the address of the endpoint which is

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

Change this line of code to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM