我正在使用 ASP.NET C# 创建 Web 服务。我从网络服务发送各种数据类型,因此我使用以下结构。
public enum WS_ServiceResponseResult
{
Success,
Failure,
}
public class WS_ServiceResponse
{
public WS_ServiceResponseResult result { get; set; }
public object data { get; set; }
}
public class WS_User
{
public long id{ get; set; }
public string name{ get; set; }
}
Web服务示例方法
[WebMethod(EnableSession = true)]
public WS_ServiceResponse LogIn(string username, string pasword)
{
WS_ServiceResponse osr = new WS_ServiceResponse();
long userID = UserController.checkLogin(username, pasword);
if (userID != 0)
{
osr.result = WS_ServiceResponseResult.Success;
osr.data = new WS_User() { id = userID, name = username };
}
else
{
osr.result = WS_ServiceResponseResult.Failure;
osr.data = "Invalid username/password!";
}
return osr;
}
我使用两种客户端类型:javascript 和 C#.NET Windows 窗体。当我从 javascript 调用时,我没有遇到任何问题,并且 osr.data 已填充 WS_User。所以我可以轻松使用 osr.data.id 。但是当我从 C#.NET 使用时(代理是使用“添加 Web 引用”生成的),我可以成功调用,但当结果到达时,我收到 Soap 异常
{System.Web.Services.Protocols.SoapException:
System.Web.Services.Protocols.SoapException:
服务器无法处理请求。
---> System.InvalidOperationException:生成 XML 时出错
文档。 ……
我缺少什么?我猜对象没有明确定义并导致了问题。有哪些解决方法?
Thanks
Maksud
添加:
如果添加以下虚拟方法,则效果很好。希望它能帮助您找到解决方案。
[WebMethod]
public WS_User Dummy()
{
return new WS_User();
}