<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Adding the User-Agent when calling a Web Service with WCF</title>
	<atom:link href="http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/</link>
	<description>The ongoing software adventures of Julian Biddle</description>
	<lastBuildDate>Tue, 02 Oct 2012 19:24:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Christian Roy</title>
		<link>http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/#comment-2096</link>
		<dc:creator><![CDATA[Christian Roy]]></dc:creator>
		<pubDate>Tue, 08 Nov 2011 18:56:58 +0000</pubDate>
		<guid isPermaLink="false">https://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/#comment-2096</guid>
		<description><![CDATA[Thanl to you and Charlie Sibbach!! You saved me a lot of time]]></description>
		<content:encoded><![CDATA[<p>Thanl to you and Charlie Sibbach!! You saved me a lot of time</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charlie Sibbach</title>
		<link>http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/#comment-2068</link>
		<dc:creator><![CDATA[Charlie Sibbach]]></dc:creator>
		<pubDate>Thu, 23 Jun 2011 17:38:36 +0000</pubDate>
		<guid isPermaLink="false">https://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/#comment-2068</guid>
		<description><![CDATA[Thanks a lot for this. Our DDoS mitigation service requires a user agent header or it blocks the request; it totally broke our external API without these upgrades. I ported it to C#, using these two classes:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

    /// 
    /// This inspector adds a user agent to the outgoing web request. It is based on code at: 
    /// http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/
    /// 
    class HttpUserAgentMessageInspector : IClientMessageInspector
    {
        private const string UserAgentHTTPHeader = &quot;user-agent&quot;;
        private readonly string _userAgent;

        public HttpUserAgentMessageInspector(string userAgent)
        {
            _userAgent = userAgent;
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            HttpRequestMessageProperty httpRequestMessage;
            Object httpRequestMessageObject;
            if(request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
            {
                httpRequestMessage = (HttpRequestMessageProperty) httpRequestMessageObject;
                if(String.IsNullOrEmpty(httpRequestMessage.Headers[UserAgentHTTPHeader]))
                {
                    httpRequestMessage.Headers[UserAgentHTTPHeader] = _userAgent;
                }
                
                
            } else
            {
                httpRequestMessage = new HttpRequestMessageProperty();
                httpRequestMessage.Headers.Add(UserAgentHTTPHeader, _userAgent);
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
            }
            return null;
        }

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            return;
        }
}


using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

    /// 
    /// This behavior adds a user agent to the outgoing web request. It is based on code at: 
    /// http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/
    /// 
    class HttpUserAgentEndpointBehavior : IEndpointBehavior
    {
        private readonly string _userAgent;

        public HttpUserAgentEndpointBehavior(string userAgent)
        {
            _userAgent = userAgent;
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            return;
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            return;
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            var inspector = new HttpUserAgentMessageInspector(_userAgent);
            clientRuntime.MessageInspectors.Add(inspector);
        }
}]]></description>
		<content:encoded><![CDATA[<p>Thanks a lot for this. Our DDoS mitigation service requires a user agent header or it blocks the request; it totally broke our external API without these upgrades. I ported it to C#, using these two classes:</p>
<p>using System;<br />
using System.ServiceModel;<br />
using System.ServiceModel.Channels;<br />
using System.ServiceModel.Dispatcher;</p>
<p>    ///<br />
    /// This inspector adds a user agent to the outgoing web request. It is based on code at:<br />
    /// <a href="http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/" rel="nofollow">http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/</a><br />
    ///<br />
    class HttpUserAgentMessageInspector : IClientMessageInspector<br />
    {<br />
        private const string UserAgentHTTPHeader = &#8220;user-agent&#8221;;<br />
        private readonly string _userAgent;</p>
<p>        public HttpUserAgentMessageInspector(string userAgent)<br />
        {<br />
            _userAgent = userAgent;<br />
        }</p>
<p>        public object BeforeSendRequest(ref Message request, IClientChannel channel)<br />
        {<br />
            HttpRequestMessageProperty httpRequestMessage;<br />
            Object httpRequestMessageObject;<br />
            if(request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))<br />
            {<br />
                httpRequestMessage = (HttpRequestMessageProperty) httpRequestMessageObject;<br />
                if(String.IsNullOrEmpty(httpRequestMessage.Headers[UserAgentHTTPHeader]))<br />
                {<br />
                    httpRequestMessage.Headers[UserAgentHTTPHeader] = _userAgent;<br />
                }</p>
<p>            } else<br />
            {<br />
                httpRequestMessage = new HttpRequestMessageProperty();<br />
                httpRequestMessage.Headers.Add(UserAgentHTTPHeader, _userAgent);<br />
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);<br />
            }<br />
            return null;<br />
        }</p>
<p>        public void AfterReceiveReply(ref Message reply, object correlationState)<br />
        {<br />
            return;<br />
        }<br />
}</p>
<p>using System.ServiceModel.Channels;<br />
using System.ServiceModel.Description;<br />
using System.ServiceModel.Dispatcher;</p>
<p>    ///<br />
    /// This behavior adds a user agent to the outgoing web request. It is based on code at:<br />
    /// <a href="http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/" rel="nofollow">http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/</a><br />
    ///<br />
    class HttpUserAgentEndpointBehavior : IEndpointBehavior<br />
    {<br />
        private readonly string _userAgent;</p>
<p>        public HttpUserAgentEndpointBehavior(string userAgent)<br />
        {<br />
            _userAgent = userAgent;<br />
        }</p>
<p>        public void Validate(ServiceEndpoint endpoint)<br />
        {<br />
            return;<br />
        }</p>
<p>        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)<br />
        {<br />
            return;<br />
        }</p>
<p>        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)<br />
        {<br />
            return;<br />
        }</p>
<p>        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)<br />
        {<br />
            var inspector = new HttpUserAgentMessageInspector(_userAgent);<br />
            clientRuntime.MessageInspectors.Add(inspector);<br />
        }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keiran McDonald</title>
		<link>http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/#comment-1930</link>
		<dc:creator><![CDATA[Keiran McDonald]]></dc:creator>
		<pubDate>Thu, 19 Aug 2010 13:28:38 +0000</pubDate>
		<guid isPermaLink="false">https://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/#comment-1930</guid>
		<description><![CDATA[hehe tagged with whining :)]]></description>
		<content:encoded><![CDATA[<p>hehe tagged with whining <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
