xMetalDetectorx
xMetalDetectorx

Reputation: 160

WCF Service not receiving data in POST

I have a WCF Service with a SOAP endpoint. I added a REST endpoint and the Get methods are working just fine. I am having trouble with a POST method which takes in an object and returns a different object. When I pass in my object, I get this error back:

"Message":"Object reference not set to an instance of an object."

Here's the code to call the service:

string URL = "http://qa.acct.webservice";

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP POST
var request = new RequestGetInventory
     {
         BrandIDs = new string[] { "4", "42" },
         AccountID = "9900003"
     };
var resp = client.PostAsJsonAsync("/AxaptaService.svc/rest/GetInventory", request);
response = resp.Result;
if (response.IsSuccessStatusCode)
{
     var temp = response.Content.ReadAsStringAsync().Result;
     MessageBox.Show(temp); //error message received here.
}

The RequestGetInventory object is defined as follows:

[DataContract]
public class RequestGetInventory
{
    [DataMember]
    public string[] BrandIDs { get; set; }

    [DataMember]
    public string AccountID { get; set; }

}

The contract for the webservice is defined as follows:

[OperationContract]
[WebInvoke(Method = "POST",
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    ResponseFormat = WebMessageFormat.Json)]
ResponseGetInventory GetInventory(RequestGetInventory Request);

I tried playing around with the WebInvoke parameters, but received the same error message for all viable attempts.

And this is how my web.config is set up:

<system.serviceModel>
<services>
  <service behaviorConfiguration="" name="Proj.AxaptaUS.WebService.AxaptaService">
    <endpoint address="rest" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="Proj.Interfaces.Axapta3.IAxaptaService"></endpoint>
    <endpoint address="" binding="basicHttpBinding" contract="Proj.Interfaces.Axapta3.IAxaptaService"></endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webBehavior">
      <webHttp helpEnabled="true" />
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

I am not entirely sure what I'm doing wrong because I can access this using SOAP just fine. It seems like it is not getting any values for the object which I passed in, thus causing the object reference error.

Any help would be greatly appreciated! Thanks!

Upvotes: 1

Views: 1818

Answers (1)

xMetalDetectorx
xMetalDetectorx

Reputation: 160

@jstreet posted a comment which ended up working. I changed BodyStyle = WebMessageBodyStyle.WrappedRequest to BodyStyle = WebMessageBodyStyle.Bare and removed <enableWebScript/> from config file.

After doing those things, it started to work correctly! thanks @jstreet!

Upvotes: 3

Related Questions