2012-04-30 13 views
7

क्या WebChannelFactory पर हेडर सेट करना संभव है? अगर मैं एक WebClient वस्तु उपयोग कर रहे थे, मैं कुछ इस तरह कर सकता है:WebChannelFactory और शीर्षलेख?

WebClient client = new WebClient(); 
client.Headers.Add("referer", "http://stackoverflow.com"); 
client.Headers.Add("user-agent", "Mozilla/5.0"); 

लेकिन मैं एक WebChannelFactory पर हेडर संशोधित करने के लिए एक तरह से यह पता लगाने के लिए सक्षम नहीं किया गया है।

उत्तर

10

WebChannelFactory कक्षा स्वयं किसी भी HTTP शीर्षलेख नहीं लेती है, लेकिन आप उन्हें वर्तमान WebOperationContext में जोड़ सकते हैं क्योंकि आप इसे देखने के लिए एक नया दायरा बनाते हैं - नीचे देखें।

WebChannelFactory<ICalculator> factory = new WebChannelFactory<ICalculator>(new Uri(baseAddress)); 
ICalculator proxy = factory.CreateChannel(); 
using (new OperationContextScope((IContextChannel)proxy)) 
{ 
    WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com"); 
    WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0"); 
    Console.WriteLine("Add: {0}", proxy.Add(33, 55)); 
    Console.WriteLine(); 
} 

using (new OperationContextScope((IContextChannel)proxy)) 
{ 
    WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com"); 
    WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0"); 
    Console.WriteLine("Subtract: {0}", proxy.Subtract(44, 33)); 
    Console.WriteLine(); 
} 

यह काम करता है, लेकिन यह काफी वर्बोज़ है - आप अनिवार्य रूप से प्रत्येक कॉल के लिए एक नया गुंजाइश बनाने की जरूरत आप इसे करने के निवर्तमान हेडर जोड़ना चाहते हैं।

एक और विकल्प क्लाइंट क्लास में क्लाइंट को लपेटना है ताकि आपके लिए स्कोपिंग और हेडर जोड़ सकें। ClientBase<T> से व्युत्पन्न कक्षा का उपयोग करना ऐसा करने का एक आसान तरीका है। नीचे दिए गए कोड WebChannelFactory द्वारा बनाए गए प्रॉक्सी से अनुरोधों में HTTP हेडर जोड़ने के लिए दोनों विकल्पों (क्लाइंट बेस-व्युत्पन्न क्लास का उपयोग करके सीधे दायरे का उपयोग करके) दोनों प्रश्नों के साथ पूर्ण नमूना है।

public class StackOverflow_10388746 
{ 
    [ServiceContract] 
    public interface ICalculator 
    { 
     [WebGet] 
     int Add(int x, int y); 
     [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
     int Subtract(int x, int y); 
    } 
    public class Service : ICalculator 
    { 
     public int Add(int x, int y) 
     { 
      PrintHeaders("Add"); 
      return x + y; 
     } 
     public int Subtract(int x, int y) 
     { 
      PrintHeaders("Subtract"); 
      return x - y; 
     } 
     void PrintHeaders(string operation) 
     { 
      Console.WriteLine("Incoming HTTP headers for operation '{0}'", operation); 
      foreach (var header in WebOperationContext.Current.IncomingRequest.Headers.AllKeys) 
      { 
       Console.WriteLine(" {0}: {1}", header, WebOperationContext.Current.IncomingRequest.Headers[header]); 
      } 
     } 
    } 
    public class MyWebClient : ClientBase<ICalculator>, ICalculator 
    { 
     Dictionary<string, string> outgoingHeaders = new Dictionary<string, string>(); 

     public MyWebClient(Uri baseAddress) 
      : base(new WebHttpBinding(), new EndpointAddress(baseAddress)) 
     { 
      this.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
     } 

     #region ICalculator Members 

     public int Add(int x, int y) 
     { 
      using (new OperationContextScope(this.InnerChannel)) 
      { 
       foreach (var headerName in this.outgoingHeaders.Keys) 
       { 
        WebOperationContext.Current.OutgoingRequest.Headers.Add(headerName, this.outgoingHeaders[headerName]); 
       } 

       this.outgoingHeaders.Clear(); 
       return this.Channel.Add(x, y); 
      } 
     } 

     public int Subtract(int x, int y) 
     { 
      using (new OperationContextScope(this.InnerChannel)) 
      { 
       foreach (var headerName in this.outgoingHeaders.Keys) 
       { 
        WebOperationContext.Current.OutgoingRequest.Headers.Add(headerName, this.outgoingHeaders[headerName]); 
       } 

       this.outgoingHeaders.Clear(); 
       return this.Channel.Subtract(x, y); 
      } 
     } 

     #endregion 

     public void AddOutgoingHeader(string name, string value) 
     { 
      this.outgoingHeaders.Add(name, value); 
     } 
    } 

    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     WebChannelFactory<ICalculator> factory = new WebChannelFactory<ICalculator>(new Uri(baseAddress)); 
     ICalculator proxy = factory.CreateChannel(); 
     using (new OperationContextScope((IContextChannel)proxy)) 
     { 
      WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com"); 
      WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0"); 
      Console.WriteLine("Add: {0}", proxy.Add(33, 55)); 
      Console.WriteLine(); 
     } 

     using (new OperationContextScope((IContextChannel)proxy)) 
     { 
      WebOperationContext.Current.OutgoingRequest.Headers.Add("referer", "http://stackoverflow.com"); 
      WebOperationContext.Current.OutgoingRequest.Headers.Add("user-agent", "Mozilla/5.0"); 
      Console.WriteLine("Subtract: {0}", proxy.Subtract(44, 33)); 
      Console.WriteLine(); 
     } 

     MyWebClient client = new MyWebClient(new Uri(baseAddress)); 
     client.AddOutgoingHeader("referer", "http://stackoverflow.com"); 
     client.AddOutgoingHeader("user-agent", "Mozilla/5.0"); 
     Console.WriteLine("Add (via client): {0}", client.Add(44, 77)); 
     Console.WriteLine(); 

     client.AddOutgoingHeader("referer", "http://stackoverflow.com/another"); 
     client.AddOutgoingHeader("user-agent", "Mozilla/5.0-b"); 
     Console.WriteLine("Add (via client): {0}", client.Subtract(44, 77)); 
     Console.WriteLine(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

महान उत्तर! आपका बहुत बहुत धन्यवाद! – Kyle

संबंधित मुद्दे