WCF - WCF基本概念和简单WCF服务端创建 (一)

基本概念

Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型。
WCF的基本概念:
  地址:定义服务的地址
  绑定:定义服务的通讯方式(传输协议、编码方案)
  契约:定义服务的具体实现(服务契约、数据契约、消息契约和错误契约)
  终结点:由地址、绑定和契约共同构成一个终结点,服务器通过终结点向客户端公开服务,客户端通过终结点调用服务(A、B、C)。

详细说明

下面通过一个简单的demo来说明

1、创建一个空白解决方案,命名为WcfDemo;

2、在解决方案里添加两个类库,分别命名为IBLL和BLL;

3、在IBLL类库下添加一个接口,命名为IWcfServices,并添加其核心引用 System.ServiceModel;

我们在这里创建一个简单的加法函数接口并指定为操作契约类型的接口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{
    /// <summary>
    /// wcf是典型的面向接口编程,注意需要添加核心引用 System.ServiceModel
    /// </summary>
    [ServiceContract] //提供一个服务契约
    public interface IWcfServices
    {
        [OperationContract]//指定这里的Add方法是一个操作契约
        int Add(int a, int b);
    }
}

4、在BLL类库下添加一个类,命名为WcfService,添加引用IBLL,并添加其核心引用 System.ServiceModel;

using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class WcfService : IWcfServices
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

5、在解决方案下添加一个控制台应用程序,并命名为Host,添加引用BLL和IBLL,并添加其核心引用 System.ServiceModel;

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(BLL.WcfService)))
            {
                host.Open();
                Console.WriteLine("服务已启动...");
                Console.ReadKey(true);
                host.Close();
            }
        }
    }
}

6、修改Host下的app.config配置文件;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <!--根据服务契约修改Name-->
      <service name="BLL.WcfService" behaviorConfiguration="behaviorConfiguration">
        <host>
          <baseAddresses>
            <!--根据实际需求修改服务地址和端口号-->
            <add baseAddress="http://localhost:8800/"/>
          </baseAddresses>
        </host>
        <!--根据实际需要修改服务契约contract-->
        <endpoint address="" binding="basicHttpBinding" contract="IBLL.IWcfServices"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfiguration">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

生成解决方案,并以管理员身份运行Host.exe,当你看到如下图所示时,说明你已经完成了一个简单的wcf服务端的编写了。

OK,下面我们简单的测试一下。

找到并打开VS自带的IDE WcfTestClient.exe,添加服务后,可进行相关的测试。

至此,一个简单WCF服务端一将创建完成了。

总结

1、WCF是一个面向接口编程,需要先创建相关接口,通过接口来指定服务契约,并指定接口里方法的契约类型;

2、WCF需要引用其核心类库System.ServiceModel.dll;

  作者:Jeremy.Wu
  出处:https://www.cnblogs.com/jeremywucnblog/
  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。         

猜你喜欢

转载自www.cnblogs.com/jeremywucnblog/p/12813060.html
WCF