VS2008中Web Reference和vs webservicee Reference的区别

上一篇文章,已經做好一個 Web Service了。
現在要介紹的,就是「呼叫(Call)那個已經完成的Web Service」來使用。
第一,要加入「Web參考(Web Reference)」。
第二,就是寫兩行程式。..............打完收工!真簡單~
上一篇文章,已經做好一個 Web Service了。
現在要介紹的,就是「呼叫(Call)那個已經完成的Web Service」來使用。
Web Service / WCF Service 收錄在下面這本書籍 &
1.& VS 2005 / 2008 / 2010的右上角,在「方案總管」裡面,按下滑鼠「右鍵」,
&&&& 加入Web參考& (這是專為 Web Service使用的!)
.NET 4.0 ( VS 2010 )起,請改用下列方法
引用別人提供的 Web Service。
如果您在不同的專案與網站之間
Web Service ( Server 與 Client)不在同一台電腦上
Dim ws As ServiceReference1.WebServiceSoapClient = New&ServiceReference1.WebServiceSoapClient()
& & & & & & & & 命名空間.WebService類別名稱SoapClient &
2.& 「加入Web參考」的步驟,也很簡單。跟著精靈畫面來操作即可。
最重要的就是兩個設定,我用紅筆框出來給大家看!
第一, Web Service的URL網址。
第二,設定為參考名稱,本範例名為 localhost
3.& 完成後,各位在「方案總管」裡面會看到一些變化。
例如: App_WebReferences目錄會多出一些東西。
不要擔心,多出來的東西,就是上一個畫面介紹的兩個重點,不是嗎?
第一, Web Service的URL網址。
第二,設定為參考名稱,本範例名為 localhost
==================================================================
完成上面的設定之後,我們的 ASP.NET程式,就能夠「呼叫」這些 Web Service了。
依照我們寫ASP.NET的習慣,新增一個 ASP.NET程式吧(副檔名 .aspx)
HTML畫面很簡單,只有一個 TextBox文字方塊。
我們呼叫 (上一篇文章寫好的)Web Service,計算結果會出現在此。
<font color="#& & &form id=&form1& runat=&server&&
<font color="#& & &div&
<font color="#& & & & Using WebService_2----
<font color="#& & & & &asp:TextBox ID=&TextBox1& runat=&server&&&/asp:TextBox&
<font color="#& & &/div&
<font color="#& & &/form&
後置程式碼(Code Behind)也很簡單。短短幾行,沒學問啦!照著模仿、照著作....照本宣科!
Web Service ( Server 與 Client)在同一台電腦上才能這樣做
&&& Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
&&&&&&&&&&& Dim ws As New localhost.Service_2
&&&&&&&&&&& &#39;--註解:localhost是關鍵字,不建議您使用。
&&&&&&&& & && & &#39;-- 另外,後面的 Service_2是您的Web Service檔案名稱
&&&&&&&&&&& Dim return_answer As Integer = ws.Compute_it(20, 30)
&&&&&&&&&&& TextBox1.Text = return_answer
&&&&&&&&&&& &#39;-- WS使用之後,別忘了關閉資源。
&&& End Sub
註解:本範例,我們的程式寫著& ws.Compute_it(20, 30)&直接把輸入值,寫死在程式內。
&&&&只是為了讓初學者,能體驗一下 Web Service的用法。
&&& 學會之後,慢慢地增加一點點難度,下一個範例才讓使用者手動輸入兩個數值。&.....慢慢來,不要急
&&& 把難度降低,循序漸進.....是我慣用的教學方法。
超簡單!對吧!
呼叫人家寫好的 Web Service,我們就不用自己寫程式囉!
Web Service版的檔案上傳(FileUpload) --
---------------------------------------------------------------------------------------------------------------------------------------------------
&關於本網站的 Web Service,已經發表一系列文章,
補充:& 本系列 Web Service文章已經延伸許多範例與程式,並撰寫成書本裡面的一章,
&&&&& 將會發表在這一本書裡面,請看:
=====& 廣& 告 =====================================
我寫文章,向來以「簡單」、「好用」為主。
&&&& 圖片 多一點!初學者才跟得上............
這些 Web Service的內容,已經集結出書。
我將思想傳授他人, 他人之所得,亦無損於我之所有;
猶如一人以我的燭火點燭,光亮與他同在,我卻不因此身處黑暗。----Thomas Jefferson
寫信給我--&&mis2000lab (at) .台灣&&或是 &school (at) mis2000lab.net
................&&&facebook社團& && &......................
................&&&Google+& &&........
................&&YouTube (ASP.NET) 線上教學影片&&
Blog文章 &附的範例& 無法下載,請看&
請看我們的(嚴格認定)。
......................................................................................................................................................
......................................................................................................................................................
& 從頭學起、學習 [各種]控制項,瞭解ASP.NET生命週期與變革,請上這一班:& & & &&
ASP.NET 遠距教學,免出門,在家上課 & 上課時,老師 親自 線上講解!並非事先錄影!&& &
事先錄製好的影片,並非上課時側錄! & 觀看影片時,有如我「一對一」跟您面對面講課。
& &&&**免費觀賞**
Please enable JavaScript to view the[架构设计] vs2012.netWebService简单的创建、使用、验证、发布后更改引用地址、webreference、servicereference | 软件世界网
-& 正文阅读
[架构设计]vs2012.netWebService简单的创建、使用、验证、发布后更改引用地址、webreference、servicereference
&& 19:53:04
工作中使用到较多的webservice,学习了一段时间,在这里总结一下在vs中使用webservice的过程和理解。
一 创建(服务器端)
在vs中穿件webservice是比较简单的。下面记录一下步骤。
步骤1:新建一个web项目,注意不能是类库。
步骤2:在新建的web项目上,添加新项--web服务;
自动生成的代码如下: /// &summary&
/// WebService1 的摘要说明
/// &/summary&
[WebService(Namespace = &http://tempuri.org/&)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ponentModel.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&;
步骤3: &编写webservice服务代码;
步骤4:发布一下编写好的代码,运行后的地址就是当前webservice的地址咯;
二 调用(客户端)
直接上过程:
步骤1:在要添加webservice的项目上,右键引用--添加服务引用;
步骤2:注意这里有两种方式来添加webservice引用,一种是web reference(web引用),另一种叫:service reference(服务引用);
在添加的时候注意一下添加的是哪个,这个不大好描述;直接上图咯。下图表示服务引用↓(命名空间请自定义,就是代理类的命名空间):
如果点击上图的&高级&按钮,那么将会添加web引用↓(ps:根据下图的说明,web reference是net2.0版本的的引用方式,为了兼容性才保留下来;):
步骤3:添加完引用,并设置自定义的命名空间后,vs2012会使用工具自动根据URL去读取wsdl文件,然后生成代理类,你就可以用代理类实例化对象,然后接续工作咯????
三& 验证调用者身份(webservice的登录)
使用webservice的验证规则大体有这么几种:验证调用者Ip地址、利用soap头验证调用者的帐号密码、利用ssl加密验证;这里要说的是利用soap header进行验证的方法。
步骤一:在webservice服务中添加一个类(这个类作为传递soap头帐号密码的载体;继承自抽象类:System.Web.Services.Protocols.SoapHeader)。代码如下namespace PointStoreWebService
/// &summary&
/// 这个代码网上一大片,可以根据需要自己修改
/// &/summary&
public class MySoapHeader:System.Web.Services.Protocols.SoapHeader
/// &summary&
/// 属性咯;
/// Tips:不想用自动属性,有可以快捷生成属性的方法:限定下环线开头的私有成员,然后Ctrl+R+E,vs2012会自动构造出属性咯
/// &/summary&
private string _UserId = string.E
public string UserId
get { return _UserId; }
set { _UserId = }
private string _PassWord = string.E
public string PassWord
get { return _PassW }
set { _PassWord = }
/// &summary&
/// 无参构造咯
/// &/summary&
public MySoapHeader()
/// &summary&
/// 有参构造构造咯
/// &/summary&
/// &param name=&id&&&/param&
/// &param name=&pwd&&&/param&
public MySoapHeader(string id, string pwd)
Initial(id, pwd);
/// &summary&
/// 初始化咯
/// &/summary&
/// &param name=&id&&&/param&
/// &param name=&pwd&&&/param&
public void Initial(string id, string pwd)
PassWord =
/// &summary&
/// 验证咯
/// &/summary&
/// &param name=&id&&&/param&
/// &param name=&pwd&&&/param&
/// &param name=&msg&&&/param&
/// &returns&&/returns&
public bool IsValid(string id, string pwd,out string msg)
if (id == &admin& && pwd == &admin&)
msg = &log error!&;
/// &summary&
/// 重载咯
/// &/summary&
/// &param name=&msg&&&/param&
/// &returns&&/returns&
public bool IsValid(out string msg)
return IsValid(_UserId, _PassWord, out msg);
步骤二:在webservice服务类中添加上面类型的成员;
MySoapHeader header = new MySoapHeader();
步骤三:在需要进行验证的方法上添加属性:(这一步不要忘记,否则在调用的时候是 点 不出来MySoapHeader类的)&
[SoapHeader(&header&)]
步骤四:在需要验证的方法中调用MySoapHeader类中的方法进行流程控制咯。。。。
步骤五:在调用端(译并更新引用
(/ □ \)记得编),先实例化MySoapHeader类的一个对象,然后给对象的验证属性(这里就是UserId、PassWord进行赋&#20540;;)
步骤六:在使用代理类对象的方法的时候将&MySoapHeader类对象传递过去;
ps:这里需要注意的是:根据vs2012添加webservice方式的不同,传递soap头的方法也不一样;具体如下:
①当你是web reference方式时:在实例化代理类后(假设名为:ServiceOBJ),可以使用ServiceOBJ.MySoapHeaderValue=soap头对象名;
②当你是Service
reference方式时:代理类对象是无法找到MySoapHeaderValue这个属性的。在需要验证的方法上,作为参数传递soap头对象:ServiceOBJ.MethodName(soap头对象)
当这个方法本来就有参数的时候,那么这个方法就自动多了个参数,且在第0个参数的位置;
四 在软件发布后,动态的修改引用地址的配置方法
注意这里我使用的是新版本的service 引用方式;
当你添加一个service reference后,会在对应的项目下生成一个app.config文件:(如下)&?xml version=&1.0& encoding=&utf-8& ?&
&configuration&
&configSections&
&/configSections&
&system.serviceModel&
&bindings&
&basicHttpBinding&
&binding name=&PointStoreServiceSoap& /&
&/basicHttpBinding&
&/bindings&
&endpoint address=&http://localhost:25548/PointStoreService.asmx&
binding=&basicHttpBinding& bindingConfiguration=&PointStoreServiceSoap&
contract=&PointStoreService.PointStoreServiceSoap& name=&PointStoreServiceSoap& /&
&/system.serviceModel&
&/configuration&其中需要注意的是:
①basicHttpBinding节点下的&binding &,每天加一个service reference后,会生成一个binding节点。
②client节点下的endpoint,每添加service reference,也会增加一个endpoint节点。
如果添加webservice引用的项目是一个类库,那么就需要在网站的web.config下添加响应的模块来供类库读取。即在web.config配置文件中添加如下部分:&system.serviceModel&
&bindings&
&basicHttpBinding&
&binding name=&1& closeTimeout=&00:01:00& openTimeout=&00:01:00&
receiveTimeout=&00:10:00& sendTimeout=&00:01:00& allowCookies=&false&
bypassProxyOnLocal=&false& hostNameComparisonMode=&StrongWildcard&
maxBufferSize=&6553600& maxBufferPoolSize=&& maxReceivedMessageSize=&6553600&
messageEncoding=&Text& textEncoding=&utf-8& transferMode=&Buffered&
useDefaultWebProxy=&true&&
&readerQuotas maxDepth=&320& maxStringContentLength=&8192000& maxArrayLength=&163840&
maxBytesPerRead=&40960& maxNameTableCharCount=&163840& /&
&security mode=&None&&
&transport clientCredentialType=&None& proxyCredentialType=&None&
realm=&& /&
&message clientCredentialType=&UserName& algorithmSuite=&Default& /&
&/security&
&/binding&
&/basicHttpBinding&
&/bindings&
&endpoint address=&http://1.asmx&
binding=&basicHttpBinding& bindingConfiguration=&&
contract=&1111& name=&& /&
&/system.serviceModel&以上xml节的结构就是app.config配置文件中的相同,只是添加了一些属性进行限制(对于每个属性的含义,大家有需要可以internet 查查。)显然,binding 节点的name属性和endpoint节点是可以直接从app.config
copy过来的;
19:51:07&查看原网页[1]&
&&网站联系: qq: email:&
软件世界网Add Web Reference in Visual Studio 2010
No new messages.
Password :
Remember me
Login using
In this article we will be seeing how to add web reference in Visual studio 2010.
In this article we will be seeing how to add web reference in Visual studio
I have created a console application to access the _vti_bin/excelservice.asmx
web service. To do that I need to add the web reference, here we will be seeing
how to add the web reference.
To add the web reference do the following steps: Go to the console application, right click on the References folder and click on "Add Service reference".
&Click on "Advanced".
&Click on "Add Web Reference".
&Enter the URL of the web service (For example http://&servername&/_vti_bin/excelservice.asmx) and click on Enter.
&Change the web reference name if you wish and click on "Add Reference".
&You could see the web reference added to your solution as shown in the following.
&For more information on difference between the web reference and service reference refer the following URL .&在 Web.config 中或動態加入 Web Reference- 藍色小舖 BlueShop
台灣最大程式設計社群網站
會員總數:238418
討論主題:185059
歡迎您免費加入會員
討論區選單
行動裝置開發
多媒體 / 網管
&& 在 Web.config 中或動態加入 Web Reference
在 Web.config 中或動態加入 Web Reference
價值 : 30 QP&&點閱數:2624 回應數:8
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
一般在加入 Web Reference 都會在方案上按右鍵然後選 Add Web Reference,VS2005 就會自動將 Web Services 加入到 Web.config 裡,如下:
&appSettings&
&add key=&WebServer_Query.query& Value=&http://192.168.100.3/MyWebservices/query.asmx&/&
&/appSettings&
然後在 Code Behind 中宣告來使用:
Dim query As New WebServer_Query.query
一旦網站完成後將所有的檔案發佈到網站上或是先編譯然後將編譯好的檔案放到網站根目錄下。我一般都是採用後者的方式發佈網站,現在問題來了,當 Web Server 的 IP 位置改變的時候,我到 Web.config 中將 IP 更改成新的 IP 如 192.168.100.5 ,這時會發生 query沒被定義的錯誤。是不是只要 Web Server 的 IP 改變就得在 VS2005 中重新給一次 Reference 呢?如果直接去改的話是不是因為 VS2005 並沒將新的 Web Server 註冊呢?謝謝!
本篇文章發表於 10:06
不錯的參考
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
因為你的Web Server以經Run了..所以值以經讀到記憶體去了..改完Server重新啟動看看...如果你用程式去改的時候也要記得...除了修改以經存在記憶體中的資料,也要對Web.config這個檔案做修改..否則下次Server重新啟動..還是舊值...(應該是這樣子...吧....)
本篇文章回覆於 13:59
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
謝謝你阿達猴!但是有點不太懂你的意思,你是說重新啟動 IIS 以清空記憶體中的資料對嗎?
本篇文章回覆於 21:03
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
在網路上找到一個解決方案,他說要在 Reference.vb 或 Reference.cs 中更改 this.url 的屬性,問題是在我的專案 App_WebReference 底下並沒有此一檔案,於是又在網路上收尋,有位仁兄說在「專案總管」(我想他指的應該是 Solution Explorer)上按下「顯示所有檔案」(Show All Files)的按鈕就可以看到隱藏的檔案了(Reference.vb 預設值是隱藏檔),問題是在我的 Solution Explorer 上並沒有所謂的「顯示所有檔案」的按鈕,這樣一來叫我怎麼開啟 Reference.vb 來作修改啊??? 請問各位的「專案總管」上有這一個按鈕嗎?或者是否有人知道要怎麼叫出隱藏檔案呢?謝謝!
本篇文章回覆於 21:50
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
不錯的參考
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
方案總管下方 有一排圖形按鈕的第二個
本篇文章回覆於 22:28
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
請問 mArk 你是在安裝完 VS 2005 以後就有這個按鈕,還是你有去作設定將此案鈕放在其上?因為我「方案總管」下方是有一排圖形按鈕,但我的並沒有「顯示所有檔案」的按鈕。謝謝!
本篇文章回覆於 22:58
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
記得裝好的時候就有 印象中並沒有去做設定
本篇文章回覆於 23:24
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
最有價值解答
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
我今天正好有在寫類似的,關於您說的this.url 屬性確實是有的,由於您一開始並不了解url的位置在那裡,所以才要寫成動態的,我得做法是先做一個「假」的WebServices然後使您的專案先參考這個假的WebServices,其次,您對於這個假的WebServices裡面的每個方法先訂義好,定議的名稱要與您真實的WebServices一樣,而內容完全不用寫,接來下就是重點囉請看程式碼來說明(我主要是寫一個ap的程式來呼叫我很多的服務,但每個服務名稱雖然不同,但呼叫的方法都是相同的,都叫做Run)
private void Auto_Load(object sender, EventArgs e)
oFlowDS = new FlowDS();
wfAbsAppMTA.FillByState3(oFlowDS.wfAbsAppM);
wfAbscAppMTA.FillByState3(oFlowDS.wfAbscAppM);
wfCardAppMTA.FillByState3(oFlowDS.wfCardAppM);
wfOtAppMTA.FillByState3(oFlowDS.wfOtAppM);
wfRepairAppMTA.FillByState3(oFlowDS.wfRepairAppM);
DynamicSrv.Service oService = new asWebServices.DynamicSrv.Service();//這是我假的WebServices
oService.Url = new AbsWS.AbsWS().U//填入正確的url
wfAbsAppMTA.Update((FlowDS.wfAbsAppMDataTable)RunWS(oFlowDS.wfAbsAppM, oService));//呼叫真的服務
oService.Url = new CardWS.CardWS().U//填入正確的url
wfCardAppMTA.Update((FlowDS.wfCardAppMDataTable)RunWS(oFlowDS.wfCardAppM, oService));//呼叫真的服務
oService.Url = new OtWS.OtWS().U//填入正確的url
wfOtAppMTA.Update((FlowDS.wfOtAppMDataTable)RunWS(oFlowDS.wfOtAppM, oService));//呼叫真的服務
oService.Url = new RepairWS.RepairWS().U//填入正確的url
wfRepairAppMTA.Update((FlowDS.wfRepairAppMDataTable)RunWS(oFlowDS.wfRepairAppM, oService));//呼叫真的服務
public DataTable RunWS(DataTable dt, DynamicSrv.Service ws)
foreach (DataRow r in dt.Rows)
ws.Run(Convert.ToInt32(r[&idProcess&]));//假的服務裡有一個跟真的服務一樣的名字
r[&sState&] = &6&;
所以您要做的動作就是怎麼填入正確的url囉…
本篇文章回覆於 00:26
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
130){this.width=130;};if(this.height>130){this.height=130;}' onerror="this.src='/images/nophoto.gif'" onmousewheel="return ChgImgSize(this)" />
嗯!謝謝 JouMing !這是一個不錯的參考喔!
本篇文章回覆於 20:18
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
如要回應,請先.

我要回帖

更多关于 vs2010 webservice 的文章

 

随机推荐