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的契约设计。
作者:心海巨澜
出处:http://xinhaijulan.cnblogs.com
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述