using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System.Reflection;

namespace WcfDemo1
{
    /// <summary>  
    /// 实现服务协定接口的服务类  
    /// </summary>  
    class Program
    {
        /// <summary>
        /// 服务器端
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // 服务器基址  
            Uri baseAddress = new Uri("http://localhost:1378/services");
            // 声明服务器主机  
            using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
            {
                // 添加绑定和终结点  
                WSHttpBinding binding = new WSHttpBinding();
                binding.Name = "MyBinding";
                binding.Security.Mode = SecurityMode.None;
                host.AddServiceEndpoint(typeof(IService), binding, "mysv1");
                // 添加服务描述  
                host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
                try
                {
                    // 打开服务  
                    host.Open();
                    Console.WriteLine("服务已启动。");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }

    }

    [ServiceContract(Namespace = "MyNamespace")]
    public interface IService
    {
        [OperationContract]
        Student GetStudentInfo();
    }

    public class MyService : IService
    {
        public Student GetStudentInfo()
        {
            Student stu = new Student();
            AddrInfo info = new AddrInfo();
            info.Province = "广东省";
            info.City = "深圳市";
            info.DetailAddr = "南山区高新中";
            stu.Name = "小陈";
            stu.Phone = "1388888888";
            stu.Address = info;
            Dictionary<string, float> m_scores = new Dictionary<string, float>();
            m_scores.Add("语文", 97f);
            m_scores.Add("英语", 64.5f);
            m_scores.Add("数学", 38f);
            m_scores.Add("历史", 77.6f);
            m_scores.Add("地理", 82.3f);
            stu.Scores = m_scores;
            return stu;
        }
    }

    [DataContract]
    [KnownType("GetKnowTypes")]
    public class Student
    {
        [DataMember]
        public string Name;
        [DataMember]
        public string Phone;
        [DataMember]
        public AddrInfo Address;
        [DataMember]
        public object Scores;

        static Type[] GetKnowTypes()
        {
            return new Type[] { typeof(Dictionary<string, float>) };
        }
    }

    [DataContract]
    public class AddrInfo
    {
        [DataMember]
        public string Province;
        [DataMember]
        public string City;
        [DataMember]
        public string DetailAddr;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace Client
{
    class Program
    {
        /// <summary>
        /// 客户端 添加服务引用
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //准确获取运行时间  
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            WS.ServiceClient cli = new WS.ServiceClient();
            WS.Student stu = cli.GetStudentInfo();
            sw.Stop();
            string msg = "学生姓名:{0}\n联系电话:{1}\n" +
                        "地址信息:-----------\n" +
                        "省份:{2}\n" +
                        "市区:{3}\n" +
                        "详细地址:{4}\n" +
                        "耗时:{5}";
            Console.WriteLine(msg, stu.Name, stu.Phone, stu.Address.Province, stu.Address.City, stu.Address.DetailAddr, sw.ElapsedMilliseconds);

            Console.WriteLine("---------------------------------------");
            Console.WriteLine("学生成绩单:");
            Dictionary<string, float> scores = stu.Scores as Dictionary<string, float>;
            foreach (var item in scores)
            {
                Console.Write("{0}:{1}\n", item.Key, item.Value);
            }  
            Console.ReadKey();

        }
    }
}


本文转载:CSDN博客