WCF 服务未在 POST 中接收数据

WCF Service not receiving data in POST

我有一个带有 SOAP 端点的 WCF 服务。我添加了一个 REST 端点,Get 方法工作正常。我在使用接受对象的 POST 方法和接受不同对象的 returns 方法时遇到了问题。当我传入我的对象时,我得到了这个错误:

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

调用服务的代码如下:

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.
}

RequestGetInventory对象定义如下:

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

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

}

网络服务的合同定义如下:

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

我尝试使用 WebInvoke 参数,但在所有可行的尝试中都收到了相同的错误消息。

这就是我的 web.config 的设置方式:

<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>

我不完全确定我做错了什么,因为我可以使用 SOAP 访问它。似乎它没有为我传入的对象获取任何值,从而导致对象引用错误。

如有任何帮助,我们将不胜感激!谢谢!

@jstreet 发表了一条最终有效的评论。 我将 BodyStyle = WebMessageBodyStyle.WrappedRequest 更改为 BodyStyle = WebMessageBodyStyle.Bare 并从配置文件中删除了 <enableWebScript/>

完成这些之后,它开始正常工作了!谢谢@jstreet!