WCF入門到精通系列教程第1篇:Hello WCF

本文爲第一篇:Hello WCF。
      WCF全稱爲Windows Communication Foundation,是Microsoft平臺上的SOA架構,用於構建分佈式和可交互操作的應用程序。它統一ASMX, .NET Remoting, 與Enterprise Services的開發模型,爲各種應用提供單一的編程模型,基於配置驅動的協議選擇,消息格式,進程分配等。
開發環境:Visual Studio 2010 + NET Framework 4.0。
本章我們通過一個簡單的DEMO來創建一個WCF程序。
1、打開VS2010,選擇C#語言下的創建WCF程序,選中WCF Service Library,修改解決方案名稱爲HelloWCF與項目名稱爲HelloServiceLibrary,點擊確定。
2、刪除HelloServiceLibrary項目中生成的IService1.cs與Services1.cs文件。
3、新建IHelloWCF接口文件,代碼如下:
 //OperationContract爲服務契約[ServiceContract] public interface IHelloWCF{ //OperationContract爲方法契約[OperationContract] string GetMessage(string msg);}
4、新建HelloWCF文件,代碼如下:
 public class HelloWCF : IHelloWCF{ public string GetMessage(string msg){ return string.Format("The server received message is : {0}", msg);}}
5、修改HelloServiceLibrary中的App.config文件:
修改服務名稱爲:<service name="HelloServiceLibrary.HelloWCF">
修改端契約爲:<endpoint address="" binding="wsHttpBinding" contract="HelloServiceLibrary.IHelloWCF">
修改服務地址爲:<add baseAddress="http://localhost:8732/Design_Time_Addresses/HelloServiceLibrary/HelloWCF/" />
配置如下:
 Code [http://www.xueit.com]<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <!-- When deploying the service library project, the content of the config file must be added to the host's app.config file. System.Configuration does not support config files for libraries. --> <system.serviceModel> <services> <service name="HelloServiceLibrary.HelloWCF"> <endpoint address="" binding="wsHttpBinding" contract="HelloServiceLibrary.IHelloWCF"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8732/Design_Time_Addresses/HelloServiceLibrary/HelloWCF/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="True"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
6、新建控制檯應用程序Client,添加Service Reference,修改名稱空間爲HelloServiceLibrary。
7、在Program類中的Main函數中添加代碼。
 Code [http://www.xueit.com] static void Main(string[] args) { Console.WriteLine("------------------HelloWCFClient Begin------------------"); HelloServiceLibrary.HelloWCFClient client = new HelloServiceLibrary.HelloWCFClient(); Console.WriteLine("The client sent message is :Hello WCF"); Console.WriteLine(client.GetMessage("Hello WCF")); client.Close(); Console.WriteLine("------------------HelloWCFClient End------------------"); Console.ReadLine(); }8、F5運行調試程序,在控制檯上我們將看到客戶端調用WCF服務端返回的結果。
------------------HelloWCFClient Begin------------------The client sent message is :Hello WCFThe server received message is : Hello WCF------------------HelloWCFClient End------------------
    至此,一個簡單的WCF應用程序創建完成了,下章將詳細介紹WCF的契約設計

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章