WCF面向服务应用程序系列之一:Hello WCF
????? WCF全稱為Windows Communication Foundation,是Microsoft平臺(tái)上的SOA架構(gòu),用于構(gòu)建分布式和可交互操作的應(yīng)用程序。它統(tǒng)一ASMX, .NET Remoting, 與Enterprise Services的開發(fā)模型,為各種應(yīng)用提供單一的編程模型,基于配置驅(qū)動(dòng)的協(xié)議選擇,消息格式,進(jìn)程分配等。
開發(fā)環(huán)境:Visual Studio 2010 + NET Framework 4.0。
本章我們通過(guò)一個(gè)簡(jiǎn)單的DEMO來(lái)創(chuàng)建一個(gè)WCF程序。
1、打開VS2010,選擇C#語(yǔ)言下的創(chuàng)建WCF程序,選中WCF Service Library,修改解決方案名稱為HelloWCF與項(xiàng)目名稱為HelloServiceLibrary,點(diǎn)擊確定。
2、刪除HelloServiceLibrary項(xiàng)目中生成的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文件:
修改服務(wù)名稱為:<service name="HelloServiceLibrary.HelloWCF">
修改端契約為:<endpoint address="" binding="wsHttpBinding" contract="HelloServiceLibrary.IHelloWCF">
修改服務(wù)地址為:<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、新建控制臺(tái)應(yīng)用程序Client,添加Service Reference,修改名稱空間為HelloServiceLibrary。
7、在Program類中的Main函數(shù)中添加代碼。
代碼 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運(yùn)行調(diào)試程序,在控制臺(tái)上我們將看到客戶端調(diào)用WCF服務(wù)端返回的結(jié)果。
------------------HelloWCFClient Begin------------------The client sent message is :Hello WCF
The server received message is : Hello WCF
------------------HelloWCFClient End------------------
?
??? 至此,一個(gè)簡(jiǎn)單的WCF應(yīng)用程序創(chuàng)建完成了,下章將詳細(xì)介紹WCF的契約設(shè)計(jì)。
??? 點(diǎn)擊下載DEMO。
?
作者:心海巨瀾出處:http://xinhaijulan.cnblogs.com
版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
?
轉(zhuǎn)載于:https://www.cnblogs.com/xinhaijulan/archive/2010/10/07/1844903.html
總結(jié)
以上是生活随笔為你收集整理的WCF面向服务应用程序系列之一:Hello WCF的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ASP.NET页面揭秘之页面生命周期
- 下一篇: 在FCKeditor 2.6中添加插入视