0

I'm new to using WCF services and i hope you can help me out on some problems invoking an external REST webservice. I have create two applications to try and test my service.

  1. Console application to test my class lib
  2. An umbraco application which hosts my services

Both localhost.

Here is the code of my service:

 namespace HorecaWebservices.Webservices
{
    [ServiceContract]
    public interface IWS_Test
    {
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,     ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
         Agenda GetAgenda(String mobileAppKey);
     }
 }

 namespace HorecaWebservices.Webservices
 {

     public class WS_Test : IWS_Test
     {
         public Agenda GetAgenda(String mobileAppKey)
         {
             return BL_Agenda.GetAgenda(mobileAppKey);
         }
     }
 }

Web config of my service:

   <system.serviceModel>
     <behaviors>
       <serviceBehaviors>
         <behavior name="MyServiceTypeBehaviors">
           <serviceMetadata httpGetEnabled="true" />
           <serviceDebug includeExceptionDetailInFaults="true" />
         </behavior>
       </serviceBehaviors>
     </behaviors>
     <standardEndpoints>
       <webHttpEndpoint>
         <standardEndpoint name="" helpEnabled="true"
             automaticFormatSelectionEnabled="true"
             defaultOutgoingResponseFormat="Json">
         </standardEndpoint>
       </webHttpEndpoint>
     </standardEndpoints>
     <services>
       <service name="HorecaWebservices.Webservices.WS_Test"
              behaviorConfiguration="MyServiceTypeBehaviors">
         <endpoint address="" binding="wsHttpBinding"
              contract="HorecaWebservices.Webservices.IWS_Test"/>
         <endpoint contract="IMetadataExchange"
            binding="mexHttpBinding" address="mex"/>
       </service>
     </services>
   </system.serviceModel>

Here is the code from my console application which invokes the service method:

    public static class BL_Agenda
    {
        public static Agenda GetAgenda()
        {
            Agenda agenda = new Agenda();

            WebRequest request = WebRequest.Create("http://localhost:63462/Webservices/WS_Test.svc/GetAgenda");

            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";

            string postData = "{\"mobileAppKey\":\"HEMDZ\"}"; //encode your data 



            using (Stream s = request.GetRequestStream())
            {
                using (StreamWriter sw = new StreamWriter(s))
                    sw.Write(postData);

                s.Close();
            }


            try
            {
                using (Stream s = request.GetResponse().GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        var jsonData = sr.ReadToEnd();

                        agenda = JsonConvert.DeserializeObject<Agenda>(jsonData);
                    }

                    s.Close();
                }
            }

            catch (WebException e)
            {
                agenda = null;
                WebExceptionStatus status = e.Status;
                Console.WriteLine(status.ToString());
            }

            return agenda;
        }

Now whenever i run this code the request.GetResponse() throws a System.Net.WebException "(415) Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'"

After a couple of hours trying to find out what the problem is i still cant figure it out.. Can someone help me out on this?

Mark Rijsmus
  • 627
  • 5
  • 16
  • 1
    You've defined your **service** in the `web.config` to use `WsHttpBinding` which is a XML-based **SOAP** binding - not the JSON-based **REST**-binding you seem to be expecting on the client-side. For REST, use `webHttpBinding` on the server-side instead – marc_s Jan 12 '14 at 12:02

3 Answers3

1

At a first glance your endpoint uses the wrong binding (wsHttpBinding):

<endpoint address="" binding="wsHttpBinding" contract="HorecaWebservices.Webservices.IWS_Test"/>

Whereas it should be webHttpBinding:

<endpoint address="" binding="webHttpBinding" contract="HorecaWebservices.Webservices.IWS_Test"/>

For a complete webHttp-sample binding take a look at this article: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx

When changing the binding other issues are likely to occur. One thing is, that you'll probably don't need the <standardEndpoints>-node any more. So I'd suggest staying close to the linked sample.

Additionally it's always a good idea to activate tracing. Having tracing activated you'll get more detailed information about your 500 error (and probably others too).

Jan Köhler
  • 5,817
  • 5
  • 26
  • 35
  • Hi JME, first thank you for your quick respons! So i've modified the web.config to webHttpBinding, and now it raises a (500)Internal server error.. :(. I'll try and checkout the link you gave me. Thnx – Mark Rijsmus Jan 12 '14 at 11:59
  • Hi JME, i've removed the node in my webconfig. Thnx for the tip on tracing, once i figured out how to activate this, i hope to get more info on my 400 bad request error. Thnx! – Mark Rijsmus Jan 12 '14 at 12:22
1

This example is not exactly the same, but maybe the Factory attribute suggested will give you some clues.

HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

Community
  • 1
  • 1
William Smith
  • 1,949
  • 4
  • 25
  • 45
  • Hi william, thnx for your response! I've added the factory to my svc file, but still no help.. after setting my bindings to webHttpBinding, i now get a 400 Bad request error.. – Mark Rijsmus Jan 12 '14 at 12:16
1

I've got it! I finally sorted out the bad request error. I had to replace the BodyStyle = WebMessageBodyStyle.Bare setting to BodyStyle = WebMessageBodyStyle.WrappedRequest.

Thnx for all the help guys!

Mark Rijsmus
  • 627
  • 5
  • 16