心海巨澜

天行键,君子以自强不息;地势坤,君子以厚德载物!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

      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
版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted on 2010-10-07 08:12  心海巨澜  阅读(6653)  评论(7编辑  收藏  举报