2012-05-09 58 views
1

我試圖在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=trueIsWrapped=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> 
... 

所以在這一點上,我完全失去了。任何見解將不勝感激。如果還有其他信息可以提供,請告訴我。

+0

+1提供詳細信息和代碼。 –

回答

2

儘管可能,普通的解決方案是假設它只是「另一個數據源」,並使用服務器端的Web引用來提供數據(併爲未來的更改提供隔離)。

Silverlight App <=> Silverlight Web Services <= External/Legacy Web Service 

保持您的Silverlight應用程序纖薄,讓服務器爲您做任何繁重的工作。

+0

謝謝,這種推理是有道理的。所以現在我有一個(愚蠢的)問題。我如何讓我的Silverlight應用程序與ASP.NET應用程序「交談」?我在那邊鬼混,但並沒有真正能夠到達任何地方。 –

+1

你真的想爲任何客戶端/ Web傳輸使用WCF RA服務。它創建您需要的所有代理。以及通常的C.R.U.D.操作你也可以調用任意方法。基本上它以一種非常有用的方式將數據更改包裝起來。 (P.S.使用RIA Services「庫」選項(用於可重用性)並將web.config設置移植到您的網站web.config中)。 –

+0

非常感謝。你已經給了我一個堅實的方向。我花了一天的時間研究WCF RIA服務,我更加確信我可以做到這一點。 –