Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to POST string to a WCF service

Tags:

c#

post

wcf

I have a simple WCF service with several methods. I use POSTMAN to test them. All the GETs works fine but for some reason I'm unable to POST.

When I try to POST:

{"participants":"BLA"}

to local host (suffixed with ParticipantsStatusService.svc/init and also from a different machine to the one on which the service is deployed) I get:

"400 Bad Request. The request cannot be fulfilled due to bad syntax" and the in the preview:
"

Request Error

The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is:

at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
"

I set a breakpoint to see if I'm able to reach the function but I never got there. It should be something before the WCF. Unfortunately I didn't find any relevant reference for my problem (although it should be very basic). Appreciate your help here. Thanks !

(Attaching the interface and implementation)

namespace ParticipantsService
{
    [ServiceContract]
    public interface IParticipantsStatusService
    {
        [WebInvoke(Method = "GET", UriTemplate = "ping", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        PingResponse Ping();

        /// <summary>
        ///     Get implementation. Returns a list of all the missing participants in the ParticipantsDB.
        /// </summary>
        [WebInvoke(Method = "GET", UriTemplate = "all", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        GetAllParticipantsResponse GetAllMissingParticipants();

        /// <summary>
        ///     Init implementation. Recieves a dictionary of required patticipants and atedance status (false).
        /// </summary>
        /// <param name="participants"></param>
        //[WebInvoke(Method = "POST", UriTemplate = "init", ResponseFormat = WebMessageFormat.Json,
        //    RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        [WebInvoke(Method = "POST", UriTemplate = "init", BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Json)]
        void InitializeMeetingList(string participants);

        /// <summary>
        ///     Update implementation. Returns whether succeeded.
        /// </summary>
        /// <param name="participants"></param>
        [WebInvoke(Method = "POST", UriTemplate = "update", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        void UpdateParticipantsStatus(List<Participant> participants);

    }
}


namespace ParticipantsService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class ParticipantsStatusService : IParticipantsStatusService
    {
        //private IParticipantsDb participantsDb = new InMemoryParticipantsDb(new List<Participant>());
        private IParticipantsDb participantsDb =
            new InMemoryParticipantsDb(new List<Participant>()
            {
                new Participant("par1", true),
                new Participant("par2", false)
            });

        public PingResponse Ping()
        {
            return new PingResponse("ParticipantsStatusService is alive !");
        }

        public GetAllParticipantsResponse GetAllMissingParticipants()
        {
            if (participantsDb == null) return new GetAllParticipantsResponse(new List<Participant>());
            var missingList = participantsDb.GetMissingParticipants();

            return new GetAllParticipantsResponse(missingList);
        }

        public void InitializeMeetingList(string participants)
        {
            participantsDb = new InMemoryParticipantsDb(new List<Participant>());
        }
    }
}
like image 729
Oren Avatar asked Dec 28 '14 15:12

Oren


1 Answers

Looking at the error message ("The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.") I assume you forgot to set the Content-Type header in your post request.

Give it a try with another post and add that header:

Content-Type: application/json

Take a look at the bottom-section "raw": https://www.getpostman.com/docs/requests

like image 86
Jan Köhler Avatar answered Oct 22 '22 21:10

Jan Köhler