日期:2014-05-16 浏览次数:22289 次
1 新建空解决方案WCFServiceExp。
在解决方案下面新建Windows应用程序WCFClient和WCF服务应用程序WCFService。
程序完工后的结构如下图所示。

2 实现WCFService
新建WCF服务应用程序WCFService
在解决方案上右击--选择“添加”--选择“新建项目”--在已安装的模板中选择“WCF”--选择“WCF服务应用程序”
 
采用模板新建的WCFService结构如下图所示。
 
将WCFService下面的IService1.cs重命名为IExampleService.cs,将Service1.svc重命名为ExampleService.svc。
 
修改IExampleService.cs中提供的示例代码,得到下面的代码。
using System.ServiceModel;
namespace WCFService
{
    [ServiceContract]
    public interface IExampleService
    {
        [OperationContract]
        string GetMessage(string yourName);
    }
}
修改ExampleService.svc中提供的示例代码,得到下面的代码。
namespace WCFService
{
    public class ExampleService : IExampleService
    {
        public string GetMessage(string yourName)
        {
            if (string.IsNullOrEmpty(yourName))
            {
                return "Hello,World!";
            }
            else
            {
                return "Hello," + yourName + "!";
            }
        }
    }
}
此实例中提供的服务功能极其简单,仅向调用者返回一句欢迎信息。
修改Web.config文件,得到以下内容。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFService.ExampleService" behaviorConfiguration="ExampleServiceBeheavior">
        <endpoint address="" binding="wsHttpBinding"