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接口文件,代碼如下:
[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/" />
?
配置如下 <?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函數中添加代碼。
代碼 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 WCF
The server received message is : Hello WCF
------------------HelloWCFClient End------------------
?
??? 至此,一個簡單的WCF應用程序創建完成了,下章將詳細介紹WCF的契約設計。
??? 點擊下載DEMO。
?
作者:心海巨瀾出處:http://xinhaijulan.cnblogs.com
版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
?
轉載于:https://www.cnblogs.com/xinhaijulan/archive/2010/10/07/1844903.html
總結
以上是生活随笔為你收集整理的WCF面向服务应用程序系列之一:Hello WCF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET页面揭秘之页面生命周期
- 下一篇: 在FCKeditor 2.6中添加插入视