WCF初接觸實作之服務(wù)發(fā)布和使用
之前兩篇隨筆的示例中客戶端直接引用契約類庫,現(xiàn)實中可能因為開發(fā)團(tuán)隊或語言等原因,客戶端不能直接引用契約類庫,這就需要服務(wù)端公布自己的契約、客戶端發(fā)現(xiàn)契約。
服務(wù)端:
服務(wù)端通過配置服務(wù)行為,以元數(shù)據(jù)的形式公布服務(wù)??梢允褂门渲梦募部梢允褂么a。
1、使用配置文件:
將之前的WCFDemo.Host.WithConfig項目的配置文件用WCF服務(wù)配置編輯器打開,新建服務(wù)行為配置:
這里就用默認(rèn)的Name,實際項目中起個好聽的名字吧
添加服務(wù)元數(shù)據(jù):
設(shè)置元數(shù)據(jù)的HttpGetEnabled和HttpGetUrl:
選擇服務(wù),設(shè)置其BehaviorConfiguration為剛添加的服務(wù)行為:
保存后的配置文件:
- <?xml version="1.0"encoding="utf-8"?>
- <configuration>
- <system.serviceModel>
- <behaviors>
- <serviceBehaviors>
- <behavior name="NewBehavior0">
- <serviceMetadata httpGetEnabled="true"httpGetUrl="http://localhost:5678/DemoService/metadata"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <services>
- <service behaviorConfiguration="NewBehavior0"name="WCFDemo.Services.DemoService">
- <endpoint address="http://localhost:5678/DemoService"binding="basicHttpBinding"
- bindingConfiguration=""contract="WCFDemo.Contracts.IDemoService"/>
- bindingConfiguration=""contract="WCFDemo.Contracts.IDemoService"/>
- bindingConfiguration=""contract="WCFDemo.Contracts.IDemoService"/>
- </service>
- </services>
- </system.serviceModel>
- </configuration>
使用配置文件的方式,程序代碼不需要任何修改。
2、代碼方式:
在WCFDemo.Host.WithoutConfig項目的啟動服務(wù)代碼處添加服務(wù)行為的處理代碼:
- host = newServiceHost(typeof(DemoService));
- host.AddServiceEndpoint(typeof(IDemoService), newBasicHttpBinding(), "http://localhost:5678/DemoService");
- ServiceMetadataBehavior b = newServiceMetadataBehavior();
- b.HttpGetEnabled = true;
- b.HttpGetUrl = newUri("http://localhost:5678/DemoService/metadata");
- host.Description.Behaviors.Add(b);
- host.Opened += delegate{ label1.Text = "服務(wù)啟動";};
- host.Open();
比原來多了4行添加服務(wù)行為的代碼。
現(xiàn)在,運行兩個宿主程序中的任意一個,點擊啟動按鈕后,服務(wù)就啟動并發(fā)布了,客戶端可以發(fā)現(xiàn)契約并使用。
客戶端:
客戶端如何發(fā)現(xiàn)并使用服務(wù),有2種方式:使用命令行svcutil生成文件、在IDE中添加服務(wù)引用。
1、使用svcutil
運行宿主并啟動服務(wù);運行Visual Studio 命令提示,鍵入svcutil http://localhost:5678/DemoService/metadata,將生成一個DemoService.cs文件和一個output.config文件(可以通過/out:指定輸出目錄);
在解決方案中添加一個Windows窗體應(yīng)用程序WCFDemo.Client,為其添加引用System.ServiceModel和System.Runtime.Serialization;
將剛才生成的兩個文件添加到項目,并將output.config改名為App.config;
在窗體上放置一個Button和DataGridView,為Button的Click編寫代碼如下:
- DemoServiceClient c = newDemoServiceClient();
- dataGridView1.DataSource = c.GetMonitorData();
當(dāng)然可以不用配置文件,newDemoServiceClient()中設(shè)置參數(shù)binding和remoteAddress。
svcutil常用的選項有/out:、/config:、/noconfig:等,詳細(xì)用法這里就不介紹了。查看配置文件會發(fā)現(xiàn)里面內(nèi)容很多,因為它自動為關(guān)鍵的綁定節(jié)點設(shè)置了默認(rèn)值,這部分內(nèi)容可以刪除,所以很多時候不使用svcutil生成的配置文件。
2、添加服務(wù)引用
右擊WCFDemo.Client,在添加服務(wù)引用對話框中輸入地址http://localhost:5678/DemoService/metadata,點擊“前往”按鈕:
給命名空間起個好名(示例中就用默認(rèn)名)后確定。
我們會發(fā)現(xiàn),除了添加了服務(wù)引用,還修改了配置文件,如果原來沒有配置文件,添加服務(wù)引用后會自動添加配置文件。
在窗體上再放置一個Button,為其Click編寫代碼如下:
- ServiceReference1.DemoServiceClient c = newServiceReference1.DemoServiceClient();
- dataGridView1.DataSource = c.GetMonitorData();
和前一個一樣,可以不用配置文件。
服務(wù)器有兩種方案發(fā)布自己的元數(shù)據(jù):基于HTTP-GET協(xié)議、使用專門的終結(jié)點。以上介紹的是前一種,下面介紹一下第二種。
1、使用配置文件
將之前的WCFDemo.Host.WithConfig項目的配置文件用WCF服務(wù)配置編輯器打開,新建服務(wù)終結(jié)點,并設(shè)置ABC:
現(xiàn)在WCFDemo.Host.WithConfig已提供兩種發(fā)布服務(wù)的方式,啟動服務(wù)后,客戶端通過之前的地址http://localhost:5678/DemoService/metadata和剛才輸入的地址http://localhost:5678/DemoService/MEX,都可以找到服務(wù)。
2、使用代碼方式
在WCFDemo.Host.WithoutConfig項目的啟動服務(wù)代碼處增加一行添加終結(jié)點代碼:
- host = newServiceHost(typeof(DemoService));
- host.AddServiceEndpoint(typeof(IDemoService), newBasicHttpBinding(), "http://localhost:5678/DemoService");
- ServiceMetadataBehavior b = newServiceMetadataBehavior();
- b.HttpGetEnabled = true;
- b.HttpGetUrl = newUri("http://localhost:5678/DemoService/metadata");
- host.Description.Behaviors.Add(b);
- host.AddServiceEndpoint(typeof(IMetadataExchange), newCustomBinding(newHttpTransportBindingElement()), "http://localhost:5678/DemoService/MEX");
- host.Opened += delegate{ label1.Text = "服務(wù)啟動";};
- host.Open();
效果同上。
原文鏈接:http://www.cnblogs.com/Higel/archive/2011/12/28/2303031.html
【編輯推薦】