【发布时间】:2012-05-18 10:29:06
【问题描述】:
我正在尝试在 Silverlight 5 应用程序中使用 SOAP 服务,但我完全迷路了。这是我的第一个 Silverlight 应用程序,也是我第二次在 .NET 应用程序中使用 Web 服务。
在一个单独的 .NET 应用程序中,我能够让它工作的唯一方法是将 WSDL 添加为 Web 引用;当我将其添加为服务参考时,该应用程序不会构建。在与 WSDL 提供者交谈时,我发现 WSDL 是使用 .NET 2.0 框架编译的……因此需要将其添加为 Web 参考。
根据我目前所做的研究,我发现 Silverlight 不支持添加 Web 引用。所以我尝试将它作为 Web 引用添加到托管 ASP.NET 应用程序中,然后启动服务器。
回到我的 Silverlight 应用程序,我选择了添加服务引用的选项并指向现在位于 http://localhost:55265/Web%20References/THINKWebService/SLWebSvc_734_Upgrade.wsdl 的 WSDL 文件。 Visual Studio 似乎可以很好地处理它并生成代理。
这就是我开始陷入困境的地方。如果我的研究是正确的,则创建了 WCF 引用并且应该以这种方式使用。我从来没有使用过 WCF,所以我阅读了一些关于如何发送/接收请求的内容,这是我根据 MSDN 库中的示例提出的最好的代码(我将它插入到按钮单击事件中,所以我会确切知道代码何时执行):
private void Button1Click(object sender, RoutedEventArgs e)
{
var client = new ThinkSoapClient();
var userLoginData = new user_login_data {login = "foo", password = "bar"};
var customerIdentifier = new customer_identifier {customer_id = 6677070};
// the debugger halts on this next line and
// references the "dsn"...it's the 4th argument
client.CustomerLoginInfoSelectAsync(userLoginData, customerIdentifier, "", "myDSN");
// I'm not sure if this next line is even needed
client.CustomerLoginInfoSelectCompleted += CustomerLoginInfoSelectCallback;
MessageBox.Show(string.Format("CustomerLoginInfoSelectAsync({0},{1})", userLoginData, customerIdentifier));
}
// here's the callback method
static void CustomerLoginInfoSelectCallback(object sender, CustomerLoginInfoSelectCompletedEventArgs e)
{
MessageBox.Show(string.Format("CustomerLoginInfoSelect Result: {0}", e.Result));
}
正如我在上面的代码中提到的,调试器在执行client.CustomerLoginInfoSelectAsync 方法时会停止。这是错误消息:XmlSerializer attribute System.Xml.Serialization.XmlAttributeAttribute is not valid in dsn. Only XmlElement, XmlArray, XmlArrayItem and XmlAnyElement attributes are supported when IsWrapped is true.
从我所做的研究来看,我认为这个错误是因为 SOAP 操作元素包含一个属性 dsn(但不确定,如果子元素也有,我是否会收到这个错误)属性)。
我在Reference.cs 中将IsWrapped=true 查找/替换为IsWrapped=false,但我得到了同样的错误,但最后一个词是假而不是真。
我不确定我是否对我所追求的有任何意义,所以如果有帮助,生成的 XML 应该如下所示:
...
<customer_login_info_select_request dsn="myDSN">
<user_login_data>
<login>foo</login>
<password>bar</password>
</user_login_data>
<customer_identifier>
<customer_id>6677070</customer_id>
</customer_identifier>
<login/> <!--corresponds to the empty string in the call to CustomerLoginInfoSelectAsync-->
</customer_login_info_select_request>
...
所以在这一点上,我完全迷失了。任何见解将不胜感激。如果我可以提供任何其他信息,请告诉我。
【问题讨论】:
-
+1 用于提供详细信息和代码。
标签: silverlight .net-4.0 wcf-client silverlight-5.0