chdev77
chdev77

Reputation: 555

WCF method not accepting POST

I've spent many hours reading forms all over. Nothing seems to be working. I have a simple ajax enabled WCF service. I can call the Read method and it works find, posting is the problem.

Here is my web.config:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="CompanyNotificationService.NotificationManager.MessagesAspNetAjaxBehavior">
          <webHttp defaultOutgoingResponseFormat="Json"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="CompanyNotificationService.NotificationManager.Messages">
        <endpoint address="" behaviorConfiguration="CompanyNotificationService.NotificationManager.MessagesAspNetAjaxBehavior"
          binding="webHttpBinding" contract="CompanyNotificationService.NotificationManager.Messages" bindingConfiguration="defaultRestJson" />
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="defaultRestJson" crossDomainScriptAccessEnabled="false">
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>

And my service

[ServiceContract(Namespace = "NotificationManager.Messages")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
    public class Messages
    {
        [OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        public List<Message> Read()
        {
            Notifications notifications = Notifications.Get();
            return notifications.Messages;
        }
        [OperationContract, WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public void Create(Message models)
        {
            var i = 1; //I break here, just to inspect everything...not finished
        }

[DataContract]
public class Message : IMessage
{
    [DataMember]
    public string ID { get; set; }
    [DataMember]
    public string Text { get; set; }
    [DataMember]
    public DateTime StartDate { get; set; }
    [DataMember]
    public DateTime ResumeDate { get; set; }
    [DataMember]
    public string CreatedUser { get; set; }
}

And fiddler info

POST http://localhost:64394/NotificationManager/Messages.svc/Create HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://localhost:64394/NotificationManager/Manage.aspx
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:64394
Content-Length: 1130
DNT: 1
Connection: Keep-Alive
Pragma: no-cache
Cookie: __AntiXsrfToken=82ba422caa20485690cc905fe6a4b022

HTTP/1.1 400 Bad Request

And finally the exception

The server encountered an error processing the request.
The exception message is 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Create'. Encountered unexpected character 'm'.'. See server logs for more details.

The exception stack trace is:

at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) .....

oh, I almost for got the json from fiddler

models=[{"ID":"1","Text":"here is my message","StartDate":"2013-12-07T06:54:34.396Z","ResumeDate":"2013-12-07T06:54:34.396Z","CreatedUser":"charbaugh"}]

Any help would be great! I never posted here an I am always able to find my answers fast, but this has me wrecked!

Upvotes: 2

Views: 2303

Answers (2)

YK1
YK1

Reputation: 7602

Sure, exception is because WCF service is expecting a JSON object which begins with a {, but your body begins with m as in models=.... This is incorrect.

For your request, correct JSON to send to the service is:

{"models": {"ID":"1","Text":"here is my message","StartDate":"/Date(1320825600000-0800)/","ResumeDate":"/Date(1320825600000-0800)/","CreatedUser":"charbaugh"}}

Also, note that I have changed the datetime contents/format. For more information, see: How do I format a Microsoft JSON date?

Upvotes: 2

Tony
Tony

Reputation: 7445

The problem might be on a client side. I see you use ajax to call your service. I saw that there might be issues when you pass an object to ajax request, try to pass it as a string.

data: '[{"ID":"1","Text":"here is my message","StartDate":"2013-12-07T06:54:34.396Z","ResumeDate":"2013-12-07T06:54:34.396Z","CreatedUser":"charbaugh"}]'

More info here: http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/

Upvotes: 0

Related Questions