Saturday, February 19, 2011

Consume ArcGIS Server REST API using WCF.

Have been trying this all day (It does not work), so I write my findings down so it can be continued another day. Found a great blog post here How to consume REST services with WCF that I tried to adapt create a client proxy against ArcGIS Server REST API. The first part creating a Operation contract an adding an endpoint to was easy to do:

 [ServiceContract ]

     public  interface  IArcGISApi 

     {

         //http://help.arcgis.com/en/arcgisserver/10.0/apis/rest/index.html 

         //http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/1/query?geometry=-125.4,35.2,-118.7,43.8&geometryType=esriGeometryEnvelope  

         [OperationContract ]

         [WebGet (

             BodyStyle = WebMessageBodyStyle .Bare,

             ResponseFormat = WebMessageFormat .Json,

             UriTemplate = "?f=json&Where={query}&returnGeometry=true&returnIdsOnly=false&outFields=*" )]

             //UriTemplate = "?method=flickr.interestingness.getList&api_key={apiKey}&extras={extras}")] 

             QueryResponse  Query(string  query);

     }

 



The app.config is configured like this:


 <?xml version= "1.0 " encoding= "utf-8 " ?> 

 <configuration> 

   <system.serviceModel> 

     <client> 

       <endpoint address= "http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Household_Income/MapServer/4/query "

                 binding= "webHttpBinding "

                 bindingConfiguration= "ArcGISBinding "

                 behaviorConfiguration= "ArcGIS "

                 contract= "TestRestWCFClient.IArcGISApi "

                 name= "ArcGISREST " /> 

     </client> 

 

 

     <bindings> 

       <webHttpBinding> 

         <binding name= "ArcGISBinding " maxReceivedMessageSize= "10000000 "/> 

       </webHttpBinding> 

     </bindings> 

 

 

 

     <behaviors> 

       <endpointBehaviors> 

         <behavior name= "ArcGIS "> 

           <webHttp/> 

         </behavior> 

       </endpointBehaviors> 

     </behaviors> 

   </system.serviceModel> 

   </configuration>

 


And the code for running sending a query looks like this:

 ChannelFactory <IArcGISApi > factory = new  ChannelFactory <IArcGISApi >("ArcGISREST" );

             var  proxy = factory.CreateChannel();

             var  response = proxy.Query(" );

             ((IDisposable )proxy).Dispose();

 


What's not working? Well seems like the response is "text/plain" (checked with fiddler) instead of "application/json". I haven't found a way to force it to be sent as "application/json". The error I get is: "The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml'; 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."


Update: To make it work read: http://mathiaswestin.blogspot.com/2011/02/solved-incoming-message-has-unexpected.html

No comments: