views:

272

answers:

1

I am attempting to create and host a simple RESTful WCF Service. The service works perfectly except for 1 situation. I attempt to perform a POST to insert a new object into my static List using the JSON request of:

{"sampleItem":{"Id":1,"StartValue":2,"EndValue":3}}

If I then change the request to be:

{"sampleItemBlah":{"Id":1,"StartValue":2,"EndValue":3}}

I get a 500 response and all future POST's return a 500 error until I recycle my IIS App Pool and then it starts to work again.

It doesn't appear that the service is in a faulted state because I can still perform GET's and get data back. I turned on trace debugging and I do not see any errors in my log file.

Does anyone have any ideas?

Here is my Service Contract:

[ServiceContract]
public interface IWcfRestService
{
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    [OperationContract]
    SampleItem Insert(SampleItem sampleItem);
}

[DataContract]
public class SampleItem
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public int StartValue { get; set; }
    [DataMember]
    public int EndValue { get; set; }
}

And here is my implementation:

public class WcfRestService : IWcfRestService
{
    private static readonly List<SampleItem> Items = new List<SampleItem>();

    public SampleItem Insert(SampleItem sampleItem)
    {
        return BaseInsert(sampleItem);
    }

    private static SampleItem BaseInsert(SampleItem sampleItem)
    {
        if (Items.Exists(x => x.Id == sampleItem.Id))
            Items.RemoveAll(x => x.Id == sampleItem.Id);

        Items.Add(sampleItem);

        return sampleItem;
    }
}

And finally here is my ServiceModel section of my Web.config:

<services>
  <service behaviorConfiguration="Services.ServiceBehavior" 
           name="WcfRestServiceApp.WcfRestService">
    <endpoint address="" 
              behaviorConfiguration="RESTBehavior" 
              binding="webHttpBinding"
              contract="WcfRestServiceApp.IWcfRestService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="RESTBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="Services.ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

Any and all help is greatly appreciated.

A: 

It turned out that the problem was with using the tag:

BodyStyle = WebMessageBodyStyle.Wrapped

When I removed it, the requirement to have:

{"sampleItem":{"Id":1,"StartValue":2,"EndValue":3}}

turned into:

{"Id":1,"StartValue":2,"EndValue":3}

This forced the cast to the correct object type and if the field wasn't present, it set the value to null or the type's default empty value.

Brandon