日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WCF面向服务应用程序系列之一:Hello WCF

發布時間:2025/3/14 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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/" />

?

配置如下 <?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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。