WCF笔记(3)发送和接收SOAP(消息)头

一、定义

消息头是附加信息,那有啥用呢?你可别说,有时候还真有不少用处。举个例子,WCF的身份验证是不是很麻烦?还要颁发什么证书的(当然不是荣誉证书),如果只是验证一个客户端的身份,如用户名什么的,那么,在调用服务方法时,动态加入一些消息头,到了服务器端就获取并验证消息头。这样一来,是不是也实现身份验证?

如何传递消息头?当然是客户端发送,服务器端接收的情况较多了


二、示例代码

1、服务器端(接收消息头)

 /// <summary>
    /// 1、定义一个服务协定
    /// </summary>
    [ServiceContract(Name="MyService")]
    public interface IService
    {        
        [OperationContract]
        string TestMethod();
        [OperationContract (Name="SecondMethod")]
        int Add(int a, int b);
        [OperationContract]
        string GetMessageHeader();
        [OperationContract]
        Employee GetEmployee();
    }

/// <summary>
    ///2、定义一个类并实现服务协定
    /// </summary>
    public class MyService : IService
    {
        public string TestMethod()
        {
            return "这是一个测试方法";
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
        /// <summary>
        /// 获取客户端发送的消息头
        /// </summary>
        /// <returns>返回消息头</returns>
        public string GetMessageHeader()
        {
            int index= OperationContext.Current.IncomingMessageHeaders.FindHeader("header", "http://my");
            if (index != -1)
            {
                return OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
            }
            return "";
        }

        public Employee GetEmployee()
        {
            return new Employee
            {
                Name = "张三",
                Age = 28,
                City = "北京"
            };
        }
    }

 //3、在Main入口点中定义服务器相关的参数,并启动服务。
        static void Main(string[] args)
        {
            Uri baseUri = new Uri("http://localhost:8000/Service");
            using(ServiceHost serverHost=new ServiceHost(typeof(MyService),baseUri))
            {
                //向服务器添终结点
                WSHttpBinding httpBinding = new WSHttpBinding();
                //这里不需要安全验证
                httpBinding.Security.Mode = SecurityMode.None;
                serverHost.AddServiceEndpoint(typeof(IService), httpBinding, "my");
                //为了能让VS生成客户端代码,即WSDL文档,故要添加以下行为  
                ServiceMetadataBehavior mdBehavior = new ServiceMetadataBehavior
                {
                   HttpGetEnabled=true
                };
                serverHost.Description.Behaviors.Add(mdBehavior);

                //如果服务顺利启动,则提示,处理Opened事件  
                serverHost.Opened += (sender, e) => Console.WriteLine("服务已启动");

                //启动服务
                try
                {
                    serverHost.Open();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                // 为了让程序不往下执行而结束,故加上这句  
                Console.ReadKey();
                // 关闭服务器  
                serverHost.Close();  
            }
        }        
    }


2、客户端(发送消息头)

  WS.MyServiceClient myClient = new WS.MyServiceClient();
            //在客户端发送消息头
            using (OperationContextScope scope = new OperationContextScope(myClient.InnerChannel))
            {
                MessageHeader myHeader = MessageHeader.CreateHeader("header", "http://my", "你好,这是消息头");
                OperationContext.Current.OutgoingMessageHeaders.Add(myHeader);
                //调用方法获取消息头
                string messHeader = myClient.GetMessageHeader();
                Console.WriteLine(messHeader);
                Console.WriteLine("服务方法已调用");
            }           


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值