WCF数据协定

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();

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值