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 




         [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 " ?> 




       <endpoint address= " "

                 binding= "webHttpBinding "

                 bindingConfiguration= "ArcGISBinding "

                 behaviorConfiguration= "ArcGIS "

                 contract= "TestRestWCFClient.IArcGISApi "

                 name= "ArcGISREST " /> 






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








         <behavior name= "ArcGIS "> 








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:

No comments: