我試圖在Silverlight 5應用程序中使用SOAP服務,並且完全丟失。這是我的第一個Silverlight應用程序,也是我第二次在.NET應用程序中使用Web服務。在Silverlight中使用SOAP Web服務
在一個單獨的.NET應用程序中,我能夠實現它的唯一方法是將WSDL添加爲Web引用;當我將它作爲服務引用添加時,該應用程序不會生成。在與WSDL提供者交談時,我發現WSDL是使用.NET 2.0框架編譯的......因此需要將其添加爲Web引用。
從我到目前爲止所做的研究中,我看到Silverlight不支持添加Web引用。所以我嘗試將它作爲Web Reference添加到託管ASP.NET應用程序中,然後啓動服務器。
回到我的Silverlight應用程序中,我選擇了添加服務引用的選項,並指向WSDL文件現在位於http://localhost:55265/Web%20References/THINKWebService/SLWebSvc_734_Upgrade.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
(不知道,不過,如果我想如果收到此錯誤子元素也有屬性)。
我做了IsWrapped=true
到IsWrapped=false
的查找/替換Reference.cs
但我得到了同樣的錯誤,但最後一個字是錯誤的,而不是真實的。
我不知道如果我作出任何意義,因爲對我後,所以這裏的生成的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提供詳細信息和代碼。 –