<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>Manuel Abadia's ASP.NET stuff - WCF</title>
    <link>http://www.manuelabadia.com/blog/</link>
    <description />
    <language>en-us</language>
    <copyright>Manuel Abadia</copyright>
    <lastBuildDate>Mon, 28 Apr 2008 21:25:28 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.9.6264.0</generator>
    <managingEditor>blogcomments@manuelabadia.com</managingEditor>
    <webMaster>blogcomments@manuelabadia.com</webMaster>
    <item>
      <trackback:ping>http://www.manuelabadia.com/blog/Trackback.aspx?guid=1265968d-573a-42f2-8382-9a35e2226272</trackback:ping>
      <pingback:server>http://www.manuelabadia.com/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.manuelabadia.com/blog/PermaLink,guid,1265968d-573a-42f2-8382-9a35e2226272.aspx</pingback:target>
      <dc:creator>Your DisplayName here!</dc:creator>
      <wfw:comment>http://www.manuelabadia.com/blog/CommentView,guid,1265968d-573a-42f2-8382-9a35e2226272.aspx</wfw:comment>
      <wfw:commentRss>http://www.manuelabadia.com/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=1265968d-573a-42f2-8382-9a35e2226272</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Probably most of you have heard about AdSense, the ad serving program run by Google.
It is very simple to have a website to display ads using AdSense, as you only need
to include an external script with a few parameters (that mainly configure the size
and colors of the ads). For each click in a banner of the website, the associated
AdSense account gets a bit of money.
</p>
        <p>
However, this is not the only way to get AdSense in your pages. Google has the "<a href="http://code.google.com/apis/adsense/">AdSense
API</a>", which consists in several web services that allow sharing the revenue of
the banners between the website owner and the content publisher. That way, website
with user generated content can pay their users if their content is useful.
</p>
        <p>
A customer asked me to modify their website so they can share some of the benefits
of their banners with the creators of the articles in their site using the AdSense
API. I’ll detail some of my experiences with the integration of the AdSense API in
the webpage.
</p>
        <p>
As I had to deal with web services, and I was comfortable with the web service support
present in the .NET Framework 2.0, the easiest thing to do for me was to follow that
route. However, I always try to learn new stuff when I can, so it was an excellent
chance for me to learn WCF. Creating a proxy for a web service in WCF is as easy as
in .NET Framework 2.0. However, the problems started really soon. For some unknown
reason, the Google guys require some SOAP headers in the web service calls that are
not exposed in the AdSense’s WSDL. Reading some of the comments in the AdSense Group
revealed that the SOAP headers were exposed previously but they were removed without
giving any reason. I found this to be really annoying and. It can be fixed adding
the headers to the OutgoingMessageHeaders in the current OperationScope. Only for
this detail the Adsense API can be labeled as unfriendly for the developers.
</p>
        <p>
Once you set the headers manually, it is easy to call to the different methods to
create AdSense accounts, associate them with a publisher and generate the code to
display ads. However, I found very confusing some parts of the documentation, specially
all the ids available (you end up using clientId, publisherId,  synServiceId,
developerId, and in some methods having to pass the same id twice).
</p>
        <p>
Inspecting the generated code for the WCF proxy, I found one thing I didn’t like about
WCF. The DataContractSerializer does not handle bare arrays, so the generated code
was using the XmlSerializer for a lot of things that need directly or indirectly to
handle bare arrays in the AdSense API. The official statement of Microsoft about this
seems to be that bare arrays do not support the distinction between null arrays and
empty arrays, so the best is to use a wrapped collection.
</p>
        <p>
The AdSense API has a technical requirements page that informs you how to proceed
in some interactions with the users and how to react to error conditions. The AdSense
API methods expose the fault details as specified in the AdSenseApiException. When
an error happens when your WCF proxy is calling to the AdSense API, a FaultException
is generated. I was expecting the proxy to receive a FaultException&lt; AdSenseApiException&gt;
instead so I could easily access to the exception details but that wasn’t the case
(maybe that only works in WCF to WCF scenarios?). I tried to create a generic error
handler in order to convert the SOAP faults to the FaultException&lt;AdSenseApiException&gt;
type, but the information I found about WCF error handling in the web was only talking
about server side error handling using the IErrorHandler interface. It was clear that
I needed more in depth knowledge of WCF in order to do that. After some research I
bought Inside Windows Communication Foundation:
</p>
        <iframe style="WIDTH: 120px; HEIGHT: 240px" marginwidth="0" marginheight="0" src="http://rcm.amazon.com/e/cm?t=manuelabadias-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=0735623066&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr" frameborder="0" scrolling="no">
        </iframe>
        <p>
The book was exactly what I was looking for. Good and detailed internal information
about how WCF worked.
</p>
        <p>
Thanks to the book I was able to make my proxy generate the FaultException&lt;AdSenseApiException&gt;
transparently, but it wasn’t easy. Well, it is not difficult but you have to create
a lot of classes to do something simple.
</p>
        <p>
A channel exposes a method called GetProperty&lt;T&gt;. When an exception is generated
in the channel, the GetProperty&lt;T&gt; method is called with a parameter of type
FaultConverter. The method should return an an instance of a FaultConverter, which
creates the appropriate exception/fault type. To solve my problem I created an AdSenseFaultConverter:
</p>
        <p>
          <img src="http://www.manuelabadia.com/blog/content/binary/adsenseapi1.gif" border="0" />
        </p>
        <p>
However, in order to expose the AdSenseFaultConverter I had to create a ChannelFactory
(AdSenseChannelFactory), a channel (AdSenseChannelBase and AdSenseRequestChannel),
a Binding (AdSenseHttpBinding), and a BindingElement (AdSenseBindingElement). A lot
of classes with little added value just to override the GetProperty&lt;T&gt; method
in the channel. Also, to be able to use the configuration instead of setting up the
communication programmatically, I had to create a configuration BindingElement(AdSenseHttpBindingElement)
and a CollectionBindingElement (AdSenseHttpBindingCollectionElement). As I said before,
it is a lot of code.
</p>
        <p>
One thing that I haven’t commented yet is that your AdSense API implementation needs
to be reviewed in order to be able to serve live ads, and you need to follow some
policy requirements that you need to carefully study as they want to control even
the help pages you put in your web site. One of the requirements to be eligible to
participate in the AdSense API program is to have more than 100000 daily page views.
This is a showstopper for most of the web sites making the AdSense API useless for
99.9% of the people.
</p>
        <p>
Another negative point of the AdSense API is that the WSDL has been broken for more
than a month. I planned to write this article a couple of months ago so I don’t know
if the WSDL of the AdSense API is still invalid or not, but having it broken for more
than a month can give you an idea of the overall satisfaction I have with the AdSense
API.
</p>
        <p>
To finish talking about the AdSense API, I have to mention the Sandbox. The development
web services don’t behave exactly as the live web services (something that I hate),
so some methods need to have additional headers when testing the API to work properly.
Why? Well, basically because they do not have a decent sandbox. They just have the
development version of the web services and you have to live with that. No GUI for
managing the developers accounts, payments or something similar to the AdSense account
webpage. If you compare this to other sandboxes like the paypal one where you can
even receive the IPN confirmation messages you can clearly see the difference.
</p>
        <p>
And the last rant of the post is about Google policies regarding to AdWords and AdSense.
If you are an European citizen and you want to advertise using AdWords, Google forces
you to pay in Euros. However, when you have AdSense in your page, Google will pay
you in dollars. Judging by the prices of the ads in AdWords and the money you receive
with AdSense, Google is taking advantage of the European citizens by charging a 60%
more for an ad in AdWords and paying a 60% less for it (the exchange rate is approximately
1 euro = 1.60 dollars). Is the European commission too busy fining Microsoft for including
free products with Windows to investigate things like this?
</p>
        <p>
To sum up, I’m completely disappointed with Google and their AdSense API. If some
people ever had the illusion that Google was a non evil company that did the things
the right way, they need to wake up.<br /></p>
        <img width="0" height="0" src="http://www.manuelabadia.com/blog/aggbug.ashx?id=1265968d-573a-42f2-8382-9a35e2226272" />
      </body>
      <title>Google and AdSense</title>
      <guid isPermaLink="false">http://www.manuelabadia.com/blog/PermaLink,guid,1265968d-573a-42f2-8382-9a35e2226272.aspx</guid>
      <link>http://www.manuelabadia.com/blog/PermaLink,guid,1265968d-573a-42f2-8382-9a35e2226272.aspx</link>
      <pubDate>Mon, 28 Apr 2008 21:25:28 GMT</pubDate>
      <description>&lt;p&gt;
Probably most of you have heard about AdSense, the ad serving program run by Google.
It is very simple to have a website to display ads using AdSense, as you only need
to include an external script with a few parameters (that mainly configure the size
and colors of the ads). For each click in a banner of the website, the associated
AdSense account gets a bit of money.
&lt;/p&gt;
&lt;p&gt;
However, this is not the only way to get AdSense in your pages. Google has the "&lt;a href="http://code.google.com/apis/adsense/"&gt;AdSense
API&lt;/a&gt;", which consists in several web services that allow sharing the revenue of
the banners between the website owner and the content publisher. That way, website
with user generated content can pay their users if their content is useful.
&lt;/p&gt;
&lt;p&gt;
A customer asked me to modify their website so they can share some of the benefits
of their banners with the creators of the articles in their site using the AdSense
API. I’ll detail some of my experiences with the integration of the AdSense API in
the webpage.
&lt;/p&gt;
&lt;p&gt;
As I had to deal with web services, and I was comfortable with the web service support
present in the .NET Framework 2.0, the easiest thing to do for me was to follow that
route. However, I always try to learn new stuff when I can, so it was an excellent
chance for me to learn WCF. Creating a proxy for a web service in WCF is as easy as
in .NET Framework 2.0. However, the problems started really soon. For some unknown
reason, the Google guys require some SOAP headers in the web service calls that are
not exposed in the AdSense’s WSDL. Reading some of the comments in the AdSense Group
revealed that the SOAP headers were exposed previously but they were removed without
giving any reason. I found this to be really annoying and. It can be fixed adding
the headers to the OutgoingMessageHeaders in the current OperationScope. Only for
this detail the Adsense API can be labeled as unfriendly for the developers.
&lt;/p&gt;
&lt;p&gt;
Once you set the headers manually, it is easy to call to the different methods to
create AdSense accounts, associate them with a publisher and generate the code to
display ads. However, I found very confusing some parts of the documentation, specially
all the ids available (you end up using clientId, publisherId,&amp;nbsp; synServiceId,
developerId, and in some methods having to pass the same id twice).
&lt;/p&gt;
&lt;p&gt;
Inspecting the generated code for the WCF proxy, I found one thing I didn’t like about
WCF. The DataContractSerializer does not handle bare arrays, so the generated code
was using the XmlSerializer for a lot of things that need directly or indirectly to
handle bare arrays in the AdSense API. The official statement of Microsoft about this
seems to be that bare arrays do not support the distinction between null arrays and
empty arrays, so the best is to use a wrapped collection.
&lt;/p&gt;
&lt;p&gt;
The AdSense API has a technical requirements page that informs you how to proceed
in some interactions with the users and how to react to error conditions. The AdSense
API methods expose the fault details as specified in the AdSenseApiException. When
an error happens when your WCF proxy is calling to the AdSense API, a FaultException
is generated. I was expecting the proxy to receive a FaultException&amp;lt; AdSenseApiException&amp;gt;
instead so I could easily access to the exception details but that wasn’t the case
(maybe that only works in WCF to WCF scenarios?). I tried to create a generic error
handler in order to convert the SOAP faults to the FaultException&amp;lt;AdSenseApiException&amp;gt;
type, but the information I found about WCF error handling in the web was only talking
about server side error handling using the IErrorHandler interface. It was clear that
I needed more in depth knowledge of WCF in order to do that. After some research I
bought Inside Windows Communication Foundation:
&lt;/p&gt;
&lt;iframe style="WIDTH: 120px; HEIGHT: 240px" marginwidth=0 marginheight=0 src="http://rcm.amazon.com/e/cm?t=manuelabadias-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0735623066&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr" frameborder=0 scrolling=no&gt;
&lt;/iframe&gt;
&lt;p&gt;
The book was exactly what I was looking for. Good and detailed internal information
about how WCF worked.
&lt;/p&gt;
&lt;p&gt;
Thanks to the book I was able to make my proxy generate the FaultException&amp;lt;AdSenseApiException&amp;gt;
transparently, but it wasn’t easy. Well, it is not difficult but you have to create
a lot of classes to do something simple.
&lt;/p&gt;
&lt;p&gt;
A channel exposes a method called GetProperty&amp;lt;T&amp;gt;. When an exception is generated
in the channel, the GetProperty&amp;lt;T&amp;gt; method is called with a parameter of type
FaultConverter. The method should return an an instance of a FaultConverter, which
creates the appropriate exception/fault type. To solve my problem I created an AdSenseFaultConverter:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.manuelabadia.com/blog/content/binary/adsenseapi1.gif" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
However, in order to expose the AdSenseFaultConverter I had to create a ChannelFactory
(AdSenseChannelFactory), a channel (AdSenseChannelBase and AdSenseRequestChannel),
a Binding (AdSenseHttpBinding), and a BindingElement (AdSenseBindingElement). A lot
of classes with little added value just to override the GetProperty&amp;lt;T&amp;gt; method
in the channel. Also, to be able to use the configuration instead of setting up the
communication programmatically, I had to create a configuration BindingElement(AdSenseHttpBindingElement)
and a CollectionBindingElement (AdSenseHttpBindingCollectionElement). As I said before,
it is a lot of code.
&lt;/p&gt;
&lt;p&gt;
One thing that I haven’t commented yet is that your AdSense API implementation needs
to be reviewed in order to be able to serve live ads, and you need to follow some
policy requirements that you need to carefully study as they want to control even
the help pages you put in your web site. One of the requirements to be eligible to
participate in the AdSense API program is to have more than 100000 daily page views.
This is a showstopper for most of the web sites making the AdSense API useless for
99.9% of the people.
&lt;/p&gt;
&lt;p&gt;
Another negative point of the AdSense API is that the WSDL has been broken for more
than a month. I planned to write this article a couple of months ago so I don’t know
if the WSDL of the AdSense API is still invalid or not, but having it broken for more
than a month can give you an idea of the overall satisfaction I have with the AdSense
API.
&lt;/p&gt;
&lt;p&gt;
To finish talking about the AdSense API, I have to mention the Sandbox. The development
web services don’t behave exactly as the live web services (something that I hate),
so some methods need to have additional headers when testing the API to work properly.
Why? Well, basically because they do not have a decent sandbox. They just have the
development version of the web services and you have to live with that. No GUI for
managing the developers accounts, payments or something similar to the AdSense account
webpage. If you compare this to other sandboxes like the paypal one where you can
even receive the IPN confirmation messages you can clearly see the difference.
&lt;/p&gt;
&lt;p&gt;
And the last rant of the post is about Google policies regarding to AdWords and AdSense.
If you are an European citizen and you want to advertise using AdWords, Google forces
you to pay in Euros. However, when you have AdSense in your page, Google will pay
you in dollars. Judging by the prices of the ads in AdWords and the money you receive
with AdSense, Google is taking advantage of the European citizens by charging a 60%
more for an ad in AdWords and paying a 60% less for it (the exchange rate is approximately
1 euro = 1.60 dollars). Is the European commission too busy fining Microsoft for including
free products with Windows to investigate things like this?
&lt;/p&gt;
&lt;p&gt;
To sum up, I’m completely disappointed with Google and their AdSense API. If some
people ever had the illusion that Google was a non evil company that did the things
the right way, they need to wake up.&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.manuelabadia.com/blog/aggbug.ashx?id=1265968d-573a-42f2-8382-9a35e2226272" /&gt;</description>
      <comments>http://www.manuelabadia.com/blog/CommentView,guid,1265968d-573a-42f2-8382-9a35e2226272.aspx</comments>
      <category>General;Microsoft .NET Framework;AdSense;WCF</category>
    </item>
  </channel>
</rss>