WebService里奇怪的参数值偏移现象?
今日在做一个WebService,在客户端用SOAP直接调用此WebService,但运行时的结果却怪怪的..
以下是WebService端的代码函数:
下面是客户端使用SOAP调用的代码:
下面是一个示例的调用代码:
现在问题就来了.上面的代码运行后,发现参数有移位现象!!即WebService端函数里参数获取到的值为:from="ddd",recipients="from@test.com",subject="to@test.com",body="测试的邮件".而key的值却是null
弄了几次都是一样的情况.搞不懂.不知是什么原因导致的:(
后来再写了一个测试函数,却又发现如果客户端(SOAP)里的传递参数名如果和WebService里的函数的参数名不同,则其值永远获取不到!永远为null!!但又不会出现参数值偏移的问题!!!
下面就是这个古怪的函数:
WebService代码:
客户端代码:
上面运行是正常的,但如果将客户端里的string text 这里改一下名称,如为 string dd 则运行后,WebService端里的text的值一直为null!!
以下是值偏移的截图:
测试代码:
客户端的值载图(recipients参数的值为"recipients"):
WebService端的载图:(recipients参数的值已变成了"from"!!!而key的值是为null的,form的值却为"key"!!):
各位能告诉我为什么吗?是不是我哪里错了???(因为急于需要答案,占用一下博客首页,希望DUDU谅解,谢谢)
以下是WebService端的代码函数:
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="key">验证密匙</param>
/// <param name="from">发件人</param>
/// <param name="recipients">收件人</param>
/// <param name="subject">主题</param>
/// <param name="body">邮件正文</param>
/// <returns>是否发送成功</returns>
[WebMethod]
public bool Send(string key, string from, string recipients, string subject, string body)
{
if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(recipients)) return false;
//验证会话
if (!CheckSessionPermission(key)) return false;
try
{
//new EmailSender().Send(from, recipients, subject, body);
return true;
}
catch
{
return false;
}
}
/// 发送邮件
/// </summary>
/// <param name="key">验证密匙</param>
/// <param name="from">发件人</param>
/// <param name="recipients">收件人</param>
/// <param name="subject">主题</param>
/// <param name="body">邮件正文</param>
/// <returns>是否发送成功</returns>
[WebMethod]
public bool Send(string key, string from, string recipients, string subject, string body)
{
if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(recipients)) return false;
//验证会话
if (!CheckSessionPermission(key)) return false;
try
{
//new EmailSender().Send(from, recipients, subject, body);
return true;
}
catch
{
return false;
}
}
下面是客户端使用SOAP调用的代码:
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="from">发件人</param>
/// <param name="recipients">收件人</param>
/// <param name="subject">主题</param>
/// <param name="body">邮件正文</param>
/// <returns>是否发送成功</returns>
[SoapDocumentMethod("http://temp.uri/Service/Email/Send",
RequestNamespace = "http://temp.uri/Service/Email/",
ResponseNamespace = "http://temp.uri/Service/Email/",
Use = System.Web.Services.Description.SoapBindingUse.Default,
ParameterStyle = SoapParameterStyle.Wrapped
)]
public bool Send(string from, string recipients, string subject, string body)
{
object[] results = this.Invoke("Send", new object[] { this.Key, from, recipients, subject, body });
return (bool)results[0];
}
/// 发送邮件
/// </summary>
/// <param name="from">发件人</param>
/// <param name="recipients">收件人</param>
/// <param name="subject">主题</param>
/// <param name="body">邮件正文</param>
/// <returns>是否发送成功</returns>
[SoapDocumentMethod("http://temp.uri/Service/Email/Send",
RequestNamespace = "http://temp.uri/Service/Email/",
ResponseNamespace = "http://temp.uri/Service/Email/",
Use = System.Web.Services.Description.SoapBindingUse.Default,
ParameterStyle = SoapParameterStyle.Wrapped
)]
public bool Send(string from, string recipients, string subject, string body)
{
object[] results = this.Invoke("Send", new object[] { this.Key, from, recipients, subject, body });
return (bool)results[0];
}
下面是一个示例的调用代码:
EmailServiceClient emailClient = new EmailServiceClient("http://localhost/Service/EmailService.asmx");
emailClient.Key = "ddd";
emailClient.Send("from@test.com", "to@test.com", "测试的邮件", "这是一封测试的邮件,收到吗?");
emailClient.Key = "ddd";
emailClient.Send("from@test.com", "to@test.com", "测试的邮件", "这是一封测试的邮件,收到吗?");
现在问题就来了.上面的代码运行后,发现参数有移位现象!!即WebService端函数里参数获取到的值为:from="ddd",recipients="from@test.com",subject="to@test.com",body="测试的邮件".而key的值却是null
弄了几次都是一样的情况.搞不懂.不知是什么原因导致的:(
后来再写了一个测试函数,却又发现如果客户端(SOAP)里的传递参数名如果和WebService里的函数的参数名不同,则其值永远获取不到!永远为null!!但又不会出现参数值偏移的问题!!!
下面就是这个古怪的函数:
WebService代码:
[WebMethod]
public bool Add(int i, string text, string bb)
{
string s = text;
return false;
}
public bool Add(int i, string text, string bb)
{
string s = text;
return false;
}
客户端代码:
[SoapDocumentMethod("http://temp.uri/Service/Email/Add",
RequestNamespace = "http://temp.uri/Service/Email/",
ResponseNamespace = "http://temp.uri/Service/Email/",
Use = System.Web.Services.Description.SoapBindingUse.Default,
ParameterStyle = SoapParameterStyle.Wrapped
)]
public void Add(int i, string text, string bb)
{
object[] results = this.Invoke("Add", new object[] { i , text, bb});
}
RequestNamespace = "http://temp.uri/Service/Email/",
ResponseNamespace = "http://temp.uri/Service/Email/",
Use = System.Web.Services.Description.SoapBindingUse.Default,
ParameterStyle = SoapParameterStyle.Wrapped
)]
public void Add(int i, string text, string bb)
{
object[] results = this.Invoke("Add", new object[] { i , text, bb});
}
上面运行是正常的,但如果将客户端里的string text 这里改一下名称,如为 string dd 则运行后,WebService端里的text的值一直为null!!
以下是值偏移的截图:
测试代码:
emailClient.Key = "key";
emailClient.Send("from", "recipients", "subject", "body");
emailClient.Send("from", "recipients", "subject", "body");
客户端的值载图(recipients参数的值为"recipients"):
WebService端的载图:(recipients参数的值已变成了"from"!!!而key的值是为null的,form的值却为"key"!!):
各位能告诉我为什么吗?是不是我哪里错了???(因为急于需要答案,占用一下博客首页,希望DUDU谅解,谢谢)