如何部署Silverlight及Web Service
一直通過Visual Studio測試Silverlight,本以為部署到服務器上是很簡單的事。
沒想到遇到了很多麻煩,用了整整一天的時間搜索解決方案。
先說部署到xp系統下本地IIS,服務器win2003也一樣。
如圖,右擊虛擬目錄->屬性->HTTP頭->MIME類型。添加
擴展名: .xap
MIME類型:application/x-silverlight-app
擴展名: .xaml
MIME類型:application/xaml+xml
這樣就可以正常顯示Silverlight了。
如果你調用了Web Service,
并且你的Web Service就在承載Silverlight的網站下。如圖
這樣不會有跨域操作的麻煩。
但是你不能直接引用localhost這樣的本地服務。
否則部署在服務器上調用不成。
我的方法是把這個服務先部署在本地IIS
然后添加服務引用。
比如地址是http://127.0.0.1:8088/sl/LinqXmlService.asmx
這時候不存在跨域操作,先測試成功。
之后打開服務引用目錄,如圖
把這里的文件全部用vs打開,然后Ctrl+H做替換,選擇所有打開文檔。
把http://127.0.0.1:8088/sl/LinqXmlService.asmx替換成
http://www.weiqi9d.com/LinqXmlService.asmx
即你的服務器地址。
我也不知道是怎樣想到這樣做的。試了一下,可以。
另外一個問題,服務器上仍然無法訪問.xap不知道為什么。
我只好把.xap修改成.htm然后把這里也改了。
- <param name="source" value="ClientBin/SilverlightApplication2.htm"/>
如圖
這樣,即使你的sl是用vs2010開發的,并且服務器沒有安裝.net 4.0也可以正常顯示。
記錄一下Silverlight調用Web Service的方法。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- namespace SilverlightApplication2.Web
- {
- /// <summary>
- /// WebService1 的摘要說明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
- // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
- // [System.Web.Script.Services.ScriptService]
- public class WebService1 : System.Web.Services.WebService
- {
- [WebMethod]
- public string HelloWorld()
- {
- return "Hello World";
- }
- }
- }
- 調用
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- namespace SilverlightApplication2
- {
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- this.Loaded += new RoutedEventHandler(Page_Loaded);
- }
- //創建Web Service對象
- ServiceReference1.LinqXmlServiceSoapClient ws = new ServiceReference1.LinqXmlServiceSoapClient();
- void Page_Loaded(object sender, RoutedEventArgs e)
- {
- AddEvent();
- Bind();
- }
- //注冊事件,有點類似Ajax的回調
- private void AddEvent()
- {
- ws.HelloWorldCompleted += new EventHandler<ServiceReference1.HelloWorldCompletedEventArgs>(ws_HelloWorldCompleted);
- }
- //回調函數
- void ws_HelloWorldCompleted(object sender, ServiceReference1.HelloWorldCompletedEventArgs e)
- {
- button1.Content += e.Result + " hi";
- //MessageBox.Show(e.Result);
- }
- private void Bind()
- {
- ws.HelloWorldAsync();
- }
- }
- }
希望對正在學習Silverlight的朋友有幫助,同時希望牛人解答我的疑惑。
原文鏈接:http://www.cnblogs.com/greatverve/archive/2010/12/23/silverlight-web-service.html
【編輯推薦】
- ASP.NET MVC Web應用程序工程
- IIS6的ASP.NET ISAPI請求處理過程
- ASP.NET控件的七種用戶管理相關控件
- 對ASP.Net進行RSA加密
- ASP.NET得到數據庫字符串的方法