Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my POST method work?

Tags:

c#

wcf

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?

like image 909
Exitos Avatar asked Jul 20 '26 02:07

Exitos


1 Answers

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

like image 151
Richard Blewett Avatar answered Jul 21 '26 16:07

Richard Blewett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!