G G*_* Gr 0 c# soap web-services
嗨,我得到这个代码:不能将类型字符串转换为int.
CalculateSumOfList.ServiceReference1.Service1SoapClient client = new CalculateSumOfList.ServiceReference1.Service1SoapClient();
CalculateSumOfList.ServiceReference1.ArrayOfInt arrayOfInt = new CalculateSumOfList.ServiceReference1.ArrayOfInt();
arrayOfInt.AddRange(listInt);
int result = client.CalculateSum(arrayOfInt); //error here!
label1.Text = Convert.ToString(result);
Run Code Online (Sandbox Code Playgroud)
我打电话的网络方法在这里:
namespace CalculateWebServiceSum
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string CalculateSum(List<int> listInt)
{
int[] sum = listInt.ToArray();
return sum.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息非常清楚.你的方法,CalculateSum()返回一个字符串.在发生错误的行上,您尝试使用字符串结果来设置int的值:
int result = client.CalculateSum(arrayOfInt);
^ type int ^ returns string
Run Code Online (Sandbox Code Playgroud)
选项1
我建议你改变CalculateSum()这个:
public int CalculateSum(List<int> listInt)
{
return listInt.Sum();
}
Run Code Online (Sandbox Code Playgroud)
注意:我不确定你为什么要传递List<int>给Web服务进行总结.如果你有List<int>,只需打电话List<int>.Sum(),不需要WS.
选项2
既然你.ToString()在检索它之后调用了int,为什么不把它作为字符串检索呢?
string result = client.CalculateSum(arrayOfInt);
public string CalculateSum(List<int> listInt)
{
return listInt.Sum().ToString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1637 次 |
| 最近记录: |