我遇到了一些问题,不确定这是否是一个问题,但希望得到一些建议。
我在 vs2010 中开发了一个 c# webservice,当我调试该服务时,我在浏览器中收到此错误
The XML element 'VoucherResponse' from namespace 'http://test.org/' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The XML element 'VoucherResponse' from namespace 'test.org' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
现在在我拥有的实际类“VoucherResponse”中查看我的代码,
public class VoucherResponse : AResponse
{
public Voucher Voucher { get; set; }
}
Voucher 对象看起来像这样
public class Voucher
{
public string PIN { get; set; }
public string Serial { get; set; }
public string Batch { get; set; }
}
现在,在我的一种 Web 方法中,我返回了 VoucherResponse,我假设这个错误是在它被反射(reflect)和检查时发生的。
以前有没有人遇到过类似的问题,或者有人可以给我一些建议吗?
谢谢
最佳答案
我发现了引发错误的另一种情况!这是我的代码:
[WebMethod]
public CheckUpdateResponse CheckUpdate()
{
...
}
好吧,我解释一下:CheckUpdateResponse
是我在代码中定义的一个结构体,CheckUpdate()
是一个方法。因此,在 WSDL .NET 中,自动向方法名称 CheckUpdate
添加一个 Response
后缀。
Et voilà:它找到一个重复的元素并给出错误 “使用 WebMethodAttribute 更改方法的消息名称...”
解决方案?我将返回的类型从 CheckUpdateResponse
重命名为 CheckUpdateResult
,现在一切正常!
我希望这会对某人有所帮助!我在这上面浪费了很多时间......
关于c# - c# webservice 的问题,引用方法和类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4397915/