WCF服务Demo

 本人刚开始学习WCF ,发现此Demo很通俗易懂,故记录下来和象我一样的新手一起学习!

该例子分为五个部分:

Host:服务器端,只是提供服务,用控制台程序实现的;

IService:服务契约,要随WCF客户端分发的一个类库,是WCF服务向外公布的服务接口的集合;

Service:服务实现,是服务的主体,驻留WCF服务器端,并通过接口向WCF客户端提供服务的类库;

Provider:服务代理,WCF客户端通过调用它来间接调用到WCF服务;

Client:客户端,WCF服务的使用者;

服务器端Host代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Reflection;
using System.Configuration;
using System.ServiceModel.Configuration;
namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var host = new ServiceHost(typeof(Service.Caculate)))
            {
                host.Open();
                Console.WriteLine("Wcf Service is running !");
            }
            Console.Read();
        }
    }
}

服务器端的App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.serviceModel>
  <services>
   <service name="Service.Caculate" behaviorConfiguration="IndexerServiceBehavior">
    <!--address="mex" 相对与baseAddress的网络地址-->
    <endpoint address="mex" binding="mexHttpBinding" contract="IService.ICaculate" />
    <host>
     <baseAddresses>
      <add baseAddress="http://localhost:8732/Wcf/Service/Caculate" />
     </baseAddresses>
    </host>
   </service>
  </services>
  <behaviors>
   <serviceBehaviors>
    <behavior name="IndexerServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
 </system.serviceModel>
</configuration>

服务契约IService,是一个类库项目,随客户端分发 :

(注:客户端可能分为多个应用程序,调用WCF服务的每个项目都要引用Provider.DLL和IService.DLL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
namespace IService
{
    [ServiceContract]
    public interface ICaculate
    {
        [OperationContract]
        decimal Add(decimal x, decimal y);
    }
}

服务实现部分,是一个类库项目,驻留服务器端不随客户端分发,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IService;
using System.ServiceModel;
namespace Service
{
    /// <summary>
    ///Author   : Aricous
    ///Datetime : 20110301
    ///Function : Description
    /// </summary>
    public class Caculate : ICaculate
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public decimal Add(decimal x, decimal y)
        {
            return (x + y);
        }
    }
}

供客户端调用的代理,这里称为服务提供者,是一个类库,随客户端分发:

(注:客户端可能分为多个应用程序,调用WCF服务的每个项目都要引用Provider.DLL和IService.DLL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
namespace Provider
{
    /// <summary>
    ///Author   : Aricous
    ///Datetime : 20110301
    ///Function : Description
    /// </summary>
    public class Wcf
    {
        private static readonly Wcf instance = new Wcf();
        private Wcf() {}

        private static object mutex = new object();

        private static IService.ICaculate _CaculateService;
        /// <summary>
        /// 
        /// </summary>
        public static IService.ICaculate CaculateService
        {
            get
            {
                if (_CaculateService == null)
                {
                    lock (mutex)
                    {
                        _CaculateService = new ChannelFactory<IService.ICaculate>("CaculateService").CreateChannel();
                    }
                }
                return _CaculateService;
            }
        }
    }
}

客户端调用WCF服务的例子:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.ServiceModel;
namespace Client
{
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            decimal x = Convert.ToDecimal(textBox1.Text.Trim());
            decimal y = Convert.ToDecimal(textBox2.Text.Trim());
            decimal z = Provider.Wcf.CaculateService.Add(x, y);
            textBox3.Text = z.ToString();
        }
    }
}

 

客户端配置文件App.Config内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.serviceModel>
  <client>
   <!--服务器端使用了相对网络地址,所以这里要在baseAddress(http://localhost:8732/Wcf/Service/Caculate)后加上相对地址:/mex-->
   <endpoint address="http://localhost:8732/Wcf/Service/Caculate/mex"
       binding="mexHttpBinding"
       contract="IService.ICaculate"
       name ="CaculateService" >
   </endpoint>
  </client>
 </system.serviceModel>
</configuration>

原文地址:https://www.cnblogs.com/colder/p/1994155.html