我們首先先新建一個WCF服務項目(代碼沒有改變,都是默認生成),然后把它部署到IIS上面,為我們下面客戶端調用做准備(當然IIS宿主只是其中一種,還有Windows服務、Winform程序、控制台程序中進行寄宿);
![]() |
![]() |
方式一:利用工具svcutil.exe命令生成代理類
svcutil.exe {終結點}/out:{輸出文件.cs} /config:{配置文件.config}
如: svcutil.exe http://localhost:8089/Service1.svc?wsdl /out:Client.cs /config:app.config
1:首先開打Visual Studio 命令提示
2:輸入生成客戶端及配置文件的命令(注意命令的空格及路徑);
3:生成成功后會在相應的路徑找到文件,並把它復制到我們項目內;
4:引入System.RuntimeSerivalization及System.ServiceModel
5:客戶端調用代碼如下:
Service1Client ClientHost = new Service1Client(); MessageBox.Show(ClientHost.GetData(5));
方式二:直接在項目引用服務
1:這種方式比較簡單,直接在項目增加引用服務便可以;
2:同樣也要引入System.RuntimeSerivalization及System.ServiceModel
3:客戶端調用代碼如下:
ServiceReference1.Service1Client ServerHost = new ServiceReference1.Service1Client(); MessageBox.Show(ServerHost.GetData(5));
*用此種方式會在配置文件里自動生成一些相關配置如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IService1" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:14187/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /> </client> </system.serviceModel> </configuration>
方式三:使用ChannelFactory調用
1:首先若是我們要采用配置時 在app.config里增加配置如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="ClientTestServer" address="http://localhost:8089/Service1.svc" binding="basicHttpBinding" contract="WindowsForIIS3.IService1"></endpoint> </client> </system.serviceModel> </configuration>
2:這邊要注意是我們服務契約接口復制到項目中;當然把服務契約單獨建立一個類庫然后再引用做法會更好:
3:客戶端調用代碼如下:
ChannelFactory<IService1> factory = new ChannelFactory<IService1>("ClientTestServer"); IService1 proxy = factory.CreateChannel(); MessageBox.Show(proxy.GetData(5));
*上面采用的是ChannelFactory調用配置文件的方式,也可以采用ChannelFactory編碼方式;代碼如下:
using (ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>(new BasicHttpBinding(), "http://localhost:8089/Service1.svc")) { IService1 proxy = channelFactory.CreateChannel(); MessageBox.Show(proxy.GetData(5)); }
方式四:使用ClientBase方式調用
1:app.config里增加配置如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="ClientTestServer" address="http://localhost:8089/Service1.svc" binding="basicHttpBinding" contract="WindowsForIIS3.IService1"></endpoint> </client> </system.serviceModel> </configuration>
2:先建一個代理類分別繼承:ClientBase<服務契約接口>,服務契約接口 代碼如下:
using System.Runtime.Remoting; using System.ServiceModel; namespace WindowsForIIS3 { public class ServerProxy:ClientBase<IService1>,IService1 { public string GetData(int ValueCount) { return base.Channel.GetData(ValueCount); } } }
3:客戶端調用代碼如下:
ServerProxy proxy = new ServerProxy();
MessageBox.Show(proxy.GetData(5));
*通過ClientBase對象進行服務調用,其內部也是調用ChannelFactory創建的服務代理
以上這些方法來進行WCF客戶端的調用;在測試時遇到一個錯誤順便記錄如下(解決方式修改配置文件:binding="basicHttpBinding"):
![]() |
![]() |
若是文章對您有幫助記得幫忙推薦一下!
最近有個妹子弄的一個關於擴大眼界跟內含的訂閱號,每天都會更新一些深度內容,在這里如果你感興趣也可以關注一下(嘿對美女跟知識感興趣),當然可以關注后輸入:github 會有我的微信號,如果有問題你也可以在那找到我;當然不感興趣無視此信息;