我想创建一个服务,它接收一种类型的json对象并发送回不同的对象。当我使用相同的对象类型进行请求和响应时,一切正常。但是,当我更改我发回的对象时,我得到错误请求400 可以帮助一些吗?
public interface IOrderService
{
[OperationContract]
[WebInvoke(UriTemplate = "/GetTablesDataResponse",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, Method = "POST")]
MetaDataResponseContract GetTablesDataResponse(TablesMetaDataContract TablesRequest);
}
}
TablesMetaDataContract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace GetMetaDataWcfSvc
{
[DataContract]
public class TablesMetaDataContract
{
[DataMember]
public Header Header { get; set; }
[DataMember]
public Body Body { get; set; }
}
[DataContract]
public class Header
{}
[DataContract]
public class Body
{
[DataMember]
public get_MetaData_request get_MetaData_request { get; set; }
}
[DataContract]
public class get_MetaData_request
{
[DataMember]
public service_control service_control { get; set; }
[DataMember]
public service_data service_data { get; set; }
}
[DataContract]
public class service_control
{
[DataMember]
public string application_src { get; set; }
[DataMember]
public string transaction_id { get; set; }
[DataMember]
public string process_name { get; set; }
[DataMember]
public string request_datetime { get; set; }
}
[DataContract]
public class service_data
{
[DataMember]
public tables_name tables_name { get; set; }
}
[DataContract]
public class tables_name
{
[DataMember]
public List<string> table_name { get; set; }
}
}
MetaDataResponseContract
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace GetMetaDataWcfSvc
{
[DataContract]
public class MetaDataResponseContract
{
[DataMember]
public service_status service_status { get; set;}
[DataMember]
public OutputParameters OutputParameters {get; set; }
}
[DataContract]
public class service_status
{
[DataMember]
public string return_code { get; set; }
[DataMember]
public string return_desc { get; set; }
}
[DataContract]
public class Column
{
[DataMember]
public string name { get; set; }
[DataMember]
public string sqltype { get; set; }
[DataMember]
public string text { get; set; }
}
[DataContract]
public class Row
{
[DataMember]
public List<Column> Column { get; set; }
}
[DataContract]
public class Table
{
[DataMember]
public string name { get; set; }
[DataMember]
public List<Row> Row { get; set; }
}
[DataContract]
public class OutputParameters
{
[DataMember]
public Table Table { get; set; }
}
}
服务功能
public MetaDataResponseContract GetTablesDataResponse(TablesMetaDataContract TablesRequest)
{
TablesRequest.Body.get_MetaData_request.service_control.process_name = "server";
string ProcessName = TablesRequest.Body.get_MetaData_request.service_control.process_name;
string RequestDateTime = TablesRequest.Body.get_MetaData_request.service_control.request_datetime;
List<string> TablesList = new List<string> { };
GetDataTable(ProcessName);
MetaDataResponseContract TablesListResponse = new MetaDataResponseContract();
TablesListResponse.service_status.return_desc = "susccess";
//if i return TablesRequest and change return type in function it is working
return TablesListResponse;
}
客户端
DataContractJsonSerializer ser =
new DataContractJsonSerializer(typeof(TablesMetaDataContract));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, TablesRequest);
string data =
Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
try
{
string t = webClient.UploadString("http://localhost:63702/OrderService.svc/GetTablesDataResponse", "POST", data);
}
catch (Exception ex)
{
string x = ex.ToString();
}