服务器返回的my为空,WCF REST服务:方法参数(对象)为空

为什么我的WCF Rest服务方法的参数始终为空?....我确实访问了服务的方法,并且得到了wcf方法返回的字符串,但该参数保持为空。WCF REST服务:方法参数(对象)为空

经营合同:

[OperationContract]

[WebInvoke(UriTemplate = "AddNewLocation",

Method="POST",

BodyStyle = WebMessageBodyStyle.WrappedRequest,

ResponseFormat = WebMessageFormat.Json,

RequestFormat = WebMessageFormat.Json)]

string AddNewLocation(NearByAttractions newLocation);

AddNewLocation方法的实现

public string AddNewLocation(NearByAttractions newLocation)

{

if (newLocation == null)

{

//I'm always getting this text in my logfile

Log.Write("In add new location:- Is Null");

}

else

{

Log.Write("In add new location:- ");

}

//String is returned even though parameter is null

return "59";

}

客户端代码:

WebClient clientNewLocation = new WebClient();

clientNewLocation.Headers[HttpRequestHeader.ContentType] = "application/json";

JavaScriptSerializer js = new JavaScriptSerializer();

js.MaxJsonLength = Int32.MaxValue;

//Serialising location object to JSON

string serialLocation = js.Serialize(newLocation);

//uploading JSOn string and retrieve location's ID

string jsonLocationID = clientNewLocation.UploadString(GetURL() + "AddNewLocation", serialLocation);

我也试过这个代码在我的客户端,但仍然得到一个空参数

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(NearByAttractions));

MemoryStream ms = new MemoryStream();

ser.WriteObject(ms, newLocation);

String json = Encoding.UTF8.GetString(ms.ToArray());

WebClient clientNewLocation = new WebClient();

clientNewLocation.Headers[HttpRequestHeader.ContentType] = "application/json";

string r = clientNewLocation.UploadString(GetURL() + "AddNewLocation", json);

Console.Write(r);

后来我也改变了BodyStyle选项“裸”但后来我得到了以下错误(与两个客户端代码):

The remote server returned an error: (400) Bad Request.

任何帮助吗?感谢

编辑1: 我使用getURL()方法装载来自web配置文件web服务的IP地址,并返回类型的对象的Uri

private static Uri GetURL()

{

Configuration config = WebConfigurationManager.OpenWebConfiguration("~/web.config");

string sURL = config.AppSettings.Settings["serviceURL"].Value;

Uri url = null;

try

{

url = new Uri(sURL);

}

catch (UriFormatException ufe)

{

Log.Write(ufe.Message);

}

catch (ArgumentNullException ane)

{

Log.Write(ane.Message);

}

catch (Exception ex)

{

Log.Write(ex.Message);

}

return url;

}

服务地址存储在web配置如下:

这是我NearByAttraction类如何定义

[DataContractAttribute]

public class NearByAttractions

{

[DataMemberAttribute(Name = "ID")]

private int _ID;

public int ID

{

get { return _ID; }

set { _ID = value; }

}

[DataMemberAttribute(Name = "Latitude")]

private string _Latitude;

public string Latitude

{

get { return _Latitude; }

set { _Latitude = value; }

}

[DataMemberAttribute(Name = "Longitude")]

private string _Longitude;

public string Longitude

{

get { return _Longitude; }

set { _Longitude = value; }

}

2013-04-03

drew

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值