10

Mar

Header custom in WCF

Ciao a tutti

è molto facile in WCF passare delle informazioni di contesto dal client al server tramite degli header custom

vediamo come:

[code language="csharp"]

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

namespace ClientSoapHeaderTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var h = new ServiceHost(typeof(Service1));
            h.Open();

            var c = new ChannelFactory("client").CreateChannel();
            c.DoWork();

            Console.ReadLine();
        }
    }

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void DoWork();
    }
    public class Service1 : IService1
    {
        public void DoWork()
        {
            var hs = OperationContext.Current.IncomingMessageHeaders;

            var header1 = hs.GetHeader("header1", "");
            var header2 = hs.GetHeader("header2", "http://myheaders.application.com");

            Console.WriteLine("header1 = {0}", header1);
            Console.WriteLine("header2 = {0}", header2);
        }
    }
}

 



 
   
 

 
   
     
        http://localhost/Service1" binding="wsHttpBinding" contract="ClientSoapHeaderTest.IService1"/>
     

   

   
      http://localhost/Service1" binding="wsHttpBinding" contract="ClientSoapHeaderTest.IService1">
       
          valore header 1
          valore" _mce_href="http://myheaders.application.com">valore">http://myheaders.application.com">valore header 2 con namespace
       

     

   

 


 

[/code]

by Antonio Esposito on 3/10/2011