C#最簡單最完整的webservice實例


http://fyinthesky.blog.163.com/blog/static/3555251720110115227337/

 我做java,但最近接觸crm所以必須研究一下C#中的webservice以備后用,其實就是個新手,哈哈,這個實例是我在參考了網上諸多不完整的例子的情況下,自己摸索完成的。期間遇到過一系列的棘手的問題,經過個人努力終於解決了。我把整個步驟都寫完整了,以供以后像我這樣的C#新手少走彎路,程序很簡單,過程有點長,希望有耐心,對於入門還是有些幫助的。如果博客中沒附上源碼的【原因可能沒法添加源碼】,可以留言或郵箱我來發送給大家。

 

 

一、環境 

    我的開發環境是xpsp3,這個對於asp.net的要求是v2.0.50727

如果不是的話會報錯,可以到本地的

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i

運行即可。如果自己的已經安裝過的就可以跳過,否則在webservic的調試中會報一下錯:

名稱以無效字符開頭。處理資源 'http://localhost/ws/Service.asmx' 時出錯。第 1 行,位置: 2

   其次,需要安裝IIS,我測試的版本是iis5.1, 本來裝了iis6 但是用不了,所以版本也很重用,本人在次耗費了部分心血。如果不清楚版本可以看看 百科里的描述以作決定的參考:

http://baike.baidu.com/view/850.htm

 

二、程序

這個過程總共建立2個獨立的項目: 一個webservice 項目,一個調用。

   1 建立和部署webservice 項目:

   vs2008新建webservicesT  :注意是ASP.NET Web 服務應用程序

  

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

 之后系統會自動生成一個 類似於helloworld的一個簡單例子:

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 打開之后,可以看到

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

  

至此最簡單的webservice就建立好了。 我們不需要做任何事情,就用他來測試。

 

 接着需要對整個工程 “生成”和發布。

 生成 只要右鍵點擊項目選擇“生成”即可。

發布也一樣,如下截圖。

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

  

    把發布的文件拷到iis工作路徑中,並把這個文件夾 web共享”。

  在資源管理器中:

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 web

共享:

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

 

iis中查看,我建的文件名為sv:

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

  

 最后來查看一下是不是部署成功,可以在ie地址欄中輸入相關的url:

 我涉及的url  http://localhost/sv/Service1.asmx

 成功的話可以看到下面的截圖

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

  

然后點擊 HelloWorld”鏈接 

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

然后點擊 HelloWorld”鏈接 ,就可以最終的頁面顯示的xml內容:

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 這樣表示整個

webservice的建立和部署是成功的。

 

 

 

 

  2 接下來建立 webservice的調用調試。

   我是建立了一個web應用項目來調用測試的。

 步驟跟建立webservice項目類似,

  注意選擇好類型如圖: 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

其次,最重要的一步就是引用webservice ,

 項目右鍵選擇“添加web引用”,可以看到下圖:

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

 填入之前建立的webservice 對應的url ,此處是 http://localhost/sv/Service1.asmx

  點擊前往即可:

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

  

  然后 把往“Web引用名”中填入一個名字,我填的是kiss,這個名字比較重要,因為下面的步驟中要使用他,當然你可以任意,但請記住。

  然后 在后台中寫入調用的方法 頁面也加入觸發事件:

 后台代碼:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

namespace wbcall

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {   }

        public string GetString()

        {    kiss.Service1 n = new kiss.Service1();

             string ss = n.HelloWorld();

             return ss;

        }

    }

}

 

前台代碼:

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"Inherits="wbcall._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>無標題頁</title>

    <script language="javascript" type="text/javascript">

function B()

{

var a="<%=GetString() %>";

alert (a);

}

</script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

         kkkk

         <br />

         <input id="Bnt1" type="Button" value="button" runat ="server" onclick="B()" />

    </div>

    </form>

</body>

</html>

兩個的截圖分別如下:

 

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

  

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

 

然后把此項目“生成”、“發布”到iis上,這個過程跟 webservice 一樣,不再多說。

假如已經完成,接下來可以做測試了。

很簡單,在瀏覽器的地址欄中輸入: http://localhost/sc/Default.aspx

可以看到

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

 

 點擊 按鈕“button

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 系統會有這么一個提示,提示的內容就是之前在

webservice中的寫的,可以看一下兩者是一致的,都輸出"HelloWorld"。

 

C最簡單最完整的webservice實例 - fyinthesky - Dave Rojas

 

 

 整個過程就結束了。當然調用的形式有多種,這里我只用了這個最簡單的,其余的大家可以一起研究。

 這個webservice的建立和調用測試我都是在本機上,如果有必要可以在分布式環境上來測試。 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM