日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF 调用webservice

發布時間:2024/4/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WCF 调用webservice 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.cnblogs.com/yiyisawa/archive/2008/12/16/1356191.html

wcf client與webservice通信(-)只修改配置文件而改變服務端

問題: 假設有一個大型系統新版本使用wcf 作為服務端,生成wcf client 調用可以調用正常。 那如果當wcf 服務端出現問題或其他的原因我想再用回以前老版本的webservice或是jms server ,但客戶端調用還是通過wcf client 調用。只通過更改配置來實現。

?一、web service項目,添加一個普通service class .代碼如下:

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->Code
[WebService(Namespace
="http://Microsoft.ServiceModel.Samples")]
????
public?class?CalculatorService?:?System.Web.Services.WebService
????{????????
????????[WebMethod]
????????
public?double?Add(double?n1,?double?n2)
????????{
????????????
return?n1?+?n2;
????????}
????????[WebMethod]
????????
public?double?Subtract(double?n1,?double?n2)
????????{
????????????
return?n1?-?n2;
????????}
????????[WebMethod]
????????
public?double?Multiply(double?n1,?double?n2)
????????{
????????????
return?n1?*?n2;
????????}
????????[WebMethod]
????????
public?double?Divide(double?n1,?double?n2)
????????{
????????????
return?n1?/?n2;
????????}
????}?

webservice配置文件無需更改。運行。記錄服務地址。

?

二、打開路徑C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin:找到svcutil.exe文件。開始菜單-->run --> input cmd --->cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin -->回車;

輸入svcutil http://localhost:8080/service/service.asmx,將會在C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin生成一個webservice的代理類。注意:此代理類是wcf client形式的。(在后面只需將這個代理類小作改動,便可用于wcf sevice.)

生成的代理類:

?

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->//------------------------------------------------------------------------------
//?<auto-generated>
//?????This?code?was?generated?by?a?tool.
//?????Runtime?Version:2.0.50727.3053
//
//?????Changes?to?this?file?may?cause?incorrect?behavior?and?will?be?lost?if
//?????the?code?is?regenerated.
//?</auto-generated>
//------------------------------------------------------------------------------



????[System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel",?"3.0.0.0")]
????[System.ServiceModel.ServiceContractAttribute(Namespace?
=?"http://Microsoft.ServiceModel.Samples",?ConfigurationName?=?"CalculatorServiceSoap")]
????
public?interface?CalculatorServiceSoap
????{

????????[System.ServiceModel.OperationContractAttribute(Action?
=?"http://Microsoft.ServiceModel.Samples/Add",?ReplyAction?=?"*")]
????????
double?Add(double?n1,?double?n2);

????????[System.ServiceModel.OperationContractAttribute(Action?
=?"http://Microsoft.ServiceModel.Samples/Subtract",?ReplyAction?=?"*")]
????????
double?Subtract(double?n1,?double?n2);

????????[System.ServiceModel.OperationContractAttribute(Action?
=?"http://Microsoft.ServiceModel.Samples/Multiply",?ReplyAction?=?"*")]
????????
double?Multiply(double?n1,?double?n2);

????????[System.ServiceModel.OperationContractAttribute(Action?
=?"http://Microsoft.ServiceModel.Samples/Divide",?ReplyAction?=?"*")]
????????
double?Divide(double?n1,?double?n2);
????}

????[System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel",?"3.0.0.0")]
????
public?interface?CalculatorServiceSoapChannel?:?CalculatorServiceSoap,?System.ServiceModel.IClientChannel
????{
????}

????[System.Diagnostics.DebuggerStepThroughAttribute()]
????[System.CodeDom.Compiler.GeneratedCodeAttribute(
"System.ServiceModel",?"3.0.0.0")]
????
public?partial?class?CalculatorServiceSoapClient?:?System.ServiceModel.ClientBase<CalculatorServiceSoap>,?CalculatorServiceSoap
????{

????????
public?CalculatorServiceSoapClient()
????????{
????????}

????????
public?CalculatorServiceSoapClient(string?endpointConfigurationName)
????????????:
????????????????
base(endpointConfigurationName)
????????{
????????}

????????
public?CalculatorServiceSoapClient(string?endpointConfigurationName,?string?remoteAddress)
????????????:
????????????????
base(endpointConfigurationName,?remoteAddress)
????????{
????????}

????????
public?CalculatorServiceSoapClient(string?endpointConfigurationName,?System.ServiceModel.EndpointAddress?remoteAddress)
????????????:
????????????????
base(endpointConfigurationName,?remoteAddress)
????????{
????????}

????????
public?CalculatorServiceSoapClient(System.ServiceModel.Channels.Binding?binding,?System.ServiceModel.EndpointAddress?remoteAddress)
????????????:
????????????????
base(binding,?remoteAddress)
????????{
????????}

????????
public?double?Add(double?n1,?double?n2)
????????{
????????????
return?base.Channel.Add(n1,?n2);
????????}

????????
public?double?Subtract(double?n1,?double?n2)
????????{
????????????
return?base.Channel.Subtract(n1,?n2);
????????}

????????
public?double?Multiply(double?n1,?double?n2)
????????{
????????????
return?base.Channel.Multiply(n1,?n2);
????????}

????????
public?double?Divide(double?n1,?double?n2)
????????{
????????????
return?base.Channel.Divide(n1,?n2);
????????}
????}

?

?

三、添加Console Application,將上面生成的代理類加入項目中,并在Main方法中調用。

?

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->?Service1Clients?client?=?new?Service1Clients();

???????????
//???Service1Client?client?=?new?Service1Client();
????????????
//?Call?the?Add?service?operation.
????????????double?value1?=?100.00D;
????????????
double?value2?=?15.99D;
????????????
????????????
????????????
????????????
????????????
double?result?=?client.Add(value1,?value2);
????????????Console.WriteLine(
"Add({0},{1})?=?{2}",?value1,?value2,?result);

????????????
//?Call?the?Subtract?service?operation.
????????????value1?=?145.00D;
????????????value2?
=?76.54D;
????????????result?
=?client.Subtract(value1,?value2);
????????????Console.WriteLine(
"Subtract({0},{1})?=?{2}",?value1,?value2,?result);

????????????
//?Call?the?Multiply?service?operation.
????????????value1?=?9.00D;
????????????value2?
=?81.25D;
????????????result?
=?client.Multiply(value1,?value2);
????????????Console.WriteLine(
"Multiply({0},{1})?=?{2}",?value1,?value2,?result);

????????????
//?Call?the?Divide?service?operation.
????????????value1?=?22.00D;
????????????value2?
=?7.00D;
????????????result?
=?client.Divide(value1,?value2);
????????????Console.WriteLine(
"Divide({0},{1})?=?{2}",?value1,?value2,?result);

????????????
//Closing?the?client?gracefully?closes?the?connection?and?cleans?up?resources
????????????client.Close();

????????????Console.WriteLine();
????????????Console.WriteLine(
"Press?<ENTER>?to?terminate?client.");
????????????Console.ReadLine();

添加配置文件:App.config.此配置文件在二步生成代理類的時候會有Out.config同時產生。config里面的內容拷過來即可。

?

Code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><system.serviceModel>
????
?????
<bindings>
??????
<basicHttpBinding>
????????
<binding?name="CalculatorServiceSoap"?closeTimeout="00:01:00"
????????????openTimeout
="00:01:00"?receiveTimeout="00:10:00"?sendTimeout="00:01:00"
????????????allowCookies
="false"?bypassProxyOnLocal="false"?hostNameComparisonMode="StrongWildcard"
????????????maxBufferSize
="65536"?maxBufferPoolSize="524288"?maxReceivedMessageSize="65536"
????????????messageEncoding
="Text"?textEncoding="utf-8"?transferMode="Buffered"
????????????useDefaultWebProxy
="true">
??????????
<readerQuotas?maxDepth="32"?maxStringContentLength="8192"?maxArrayLength="16384"
??????????????maxBytesPerRead
="4096"?maxNameTableCharCount="16384"?/>
??????????
<security?mode="None">
????????????
<transport?clientCredentialType="None"?proxyCredentialType="None"
????????????????realm
=""?/>
????????????
<message?clientCredentialType="UserName"?algorithmSuite="Default"?/>
??????????
</security>
????????
</binding>
??????
</basicHttpBinding>
????
????
</bindings>
????
<client>
??????
??????
<endpoint?address="http://localhost:8080/service/service.asmx"
?????????binding
="basicHttpBinding"
?????????contract
="IService1"?name="IService1"?/>
????
</client>
??
</system.serviceModel>

?

?將webservice運行起來,(也可host到iis 里去。)debug console application.即可看到結果。

回家吃飯了。

細節和要注意的地方在第二節中寫出來。

?

項目下載地址:http://files.cnblogs.com/yiyisawa/wcfclienttowebservice.rar

?

總結

以上是生活随笔為你收集整理的WCF 调用webservice的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。