WCF Rest 服务请求对象的字段始终为空

Posted

技术标签:

【中文标题】WCF Rest 服务请求对象的字段始终为空【英文标题】:WCF Rest Service Request Object' s Fields Always Null 【发布时间】:2016-06-15 15:00:57 【问题描述】:

我创建了一个 WCF 休息服务。我尝试发布方法用法。我从客户端发送请求,从服务方法获取请求。但请求对象(CompanyDTO)字段值为空。问题出在哪里?我找不到。

服务发布方式

public void SaveCompany(CompanyDTO NewCompany)

    try
    
        CompanyManager manager = new CompanyManager();
        manager.Save(NewCompany);

        WebOperationContext ctx = WebOperationContext.Current;
        ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
    
    catch (Exception ex)
    
        throw new FaultException(new FaultReason(ex.Message));
    

合约服务接口

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.Bare,
  UriTemplate = "SaveCompany")]

void SaveCompany(CompanyDTO NewCompany);

CompantDTO

public class CompanyDTO

    public string IDENTIFIER  get; set; 
    public string TYPE  get; set; 
    public string USER_TYPE  get; set; 
    public string FIRST_NAME  get; set; 
    public string MIDDLE_NAME  get; set; 
    public string FAMILY_NAME  get; set; 
    public string COMPANY_NAME  get; set; 
    public Nullable<int> COMPANY_NO  get; set; 
    public string LEGAL_OFFICE  get; set; 
    public Nullable<System.DateTime> FOUNDATION_DATE  get; set; 
    public string BOARDOFTRADE_NAME  get; set; 
    public string BOARDOFTRADE_ID  get; set; 
    public string MERSIS_NO  get; set; 
    public string TAPDK_NO  get; set; 
    public string TRADE_REGISTRATION_NO  get; set; 
    public string TRADE_REGISTRATION_OFFICE  get; set; 
    public string TAX_IDENTIFICATION_NO  get; set; 
    public Nullable<System.DateTime> DIGITAL_SIGN_VALIDITY_DATE  get; set; 
    public string TAX_OFFICE  get; set; 
    public string TAX_OFFICE_CODE  get; set; 
    public Nullable<short> STATUS  get; set; 
    public Nullable<int> SYS_VERSION  get; set; 
    public Nullable<System.DateTime> SYS_LAST_UPDATE  get; set; 
    

客户端请求正文


    "CompanyDTO":
      
        "IDENTIFIER":"34501599398",
        "TYPE":"1",
        "USER_TYPE":"1",
        "FIRST_NAME":"Ebru",
        "MIDDLE_NAME":"sws",
        "FAMILY_NAME":"sd",
        "COMPANY_NAME":"NULL",
        "COMPANY_NO": "123",
        "LEGAL_OFFICE": "DSF",
        "FOUNDATION_DATE":"2015-03-02",
        "BOARDOFTRADE_NAME":"SAD",
        "BOARDOFTRADE_ID":"ASD",
        "MERSIS_NO":"DASD",
        "TAPDK_NO":"NULASDASDL",
        "TRADE_REGISTRATION_NO":"NULL",
        "TRADE_REGISTRATION_OFFICE":"ADS",
        "TAX_IDENTIFICATION_NO":"NUASDSALL",
        "DIGITAL_SIGN_VALIDITY_DATE": "2015-03-02",
        "TAX_OFFICE":"ASDAD",
        "TAX_OFFICE_CODE":"ASDA",
        "STATUS": "1",
        "SYS_VERSION" : "1",
        "SYS_LAST_UPDATE": "2015-03-02"
    

【问题讨论】:

【参考方案1】:

看来你需要去掉 json 中的"CompanyDTO",所以看起来像这样:

  
   "IDENTIFIER":"34501599398",
   "TYPE":"1",
   "USER_TYPE":"1",
   "FIRST_NAME":"Ebru",
   "MIDDLE_NAME":"sws",
   "FAMILY_NAME":"sd",
   "COMPANY_NAME":"NULL",
   "COMPANY_NO": "123",
   "LEGAL_OFFICE": "DSF",
   "FOUNDATION_DATE":"2015-03-02",
   "BOARDOFTRADE_NAME":"SAD",
   "BOARDOFTRADE_ID":"ASD",
   "MERSIS_NO":"DASD",
   "TAPDK_NO":"NULASDASDL",
   "TRADE_REGISTRATION_NO":"NULL",
   "TRADE_REGISTRATION_OFFICE":"ADS",
   "TAX_IDENTIFICATION_NO":"NUASDSALL",
   "DIGITAL_SIGN_VALIDITY_DATE": "2015-03-02",
   "TAX_OFFICE":"ASDAD",
   "TAX_OFFICE_CODE":"ASDA",
   "STATUS": "1",
   "SYS_VERSION" : "1",
   "SYS_LAST_UPDATE": "2015-03-02"

【讨论】:

以上是关于WCF Rest 服务请求对象的字段始终为空的主要内容,如果未能解决你的问题,请参考以下文章

使用 Fiddler 时,Rest WCF Post Json 正文参数始终为空

WCF REST 服务 400 错误请求

在 WCF REST 服务 POST 方法中处理 Json 请求数据

请求实体太大 WCF REST 服务

跨域 jQuery Ajax 请求和 WCF REST 服务

通过 HTTPS 对 WCF 自托管 REST 服务的 POST 请求示例