How can I consume odata using json with c#? - c#

I'm consuming an odata v2 api with c# / winRT. It works fine, but it's using xml as encoding format for both read and write requests. How can I make it use json instead?
I've already tried attaching an event handler to myDataSource.SendingRequest, and adding an "Accept" header, but the client-side library explicitly intercepts this and throws an exception. Also, I could not find any "Accept" property on the client side.
Thanks,
Adrian

Assuming you're using the WCF Data Services client library (as per Phani's blog post), then this is currently not possible. The client library doesn't support JSON (yet).
If you really need to consume JSON you would need to use the ODataLib directly, which is not as "easy to use" as the WCF Data Services client, but it gives you more flexibility as well. ODataLib is the reader and writer for OData wire format. See this blog post for a sample. The ODataLib for Metro itself - Microsoft.Data.OData.Metro should already be part of the VS 2012 RC release.

You need to add an accept header to the request: 'accept: application/json'

Related

Using protofub for ASP.NET Web API requests

I am working on a bunch of Web APIs which must have a latency of a single digit millisecond! To produce the response I am using protobuf which is great. My question is that can protobuf be used to de-serialize the request as well?
For example for such an API:
public async Task<List<Artist>> Search(SearchArtistRequest request)
I will write a SDK (a bunch of .DLL) which will make the call to this API and third parties will only use the .DLL. So if I could send my request message in protobuf format and de-serialize 'request' using protobuf then I might gain some performance improvements (given that by default Json.NET is used and its performance is aweful). Is there a way to do so?
You can create your own serializer, test it and replace existing one with yours in WebAPI.
There are a couple of resources about custom serialization in WebAPI.
Look here ("Testing Object Serialization"), Media Formatters and Replace...serializer.

how to run CURL commands using c#? [duplicate]

This question already has answers here:
C# - How to make a HTTP call
(3 answers)
Closed 7 years ago.
i want to get the data from elastic search using NEST but we are unable to write more consistent queries using nest.
so i want to write a url and get the json data from the CURL command and use it building my UI.
SO how can i do this using c#?
While it is a duplicate I wouldn't call out to another program just to hit a url.
Just use WebClient or WebRequest (difference)
curl is a linux tool to transfer data from & to servers through URLs.
elastic search is a full-text search engine with a RESTful web interface.
nest is a library that abstracts the REST api for you, if you say the abstractions do not fit your needs, you don't need to fall back to another command line program to query a REST server... if you're in a programming language that can do this easily and a lot more!
You can query REST server using C# just fine. This is the API spec:
https://github.com/elastic/elasticsearch/tree/master/rest-api-spec
You can write your own REST webclient:
Create a URL request and then submit the request using HTTP GET or
POST protocol . When the response data is returned, you must serialize
the data against a set of data contracts. As REST Services add new
functionality, these data contracts may need to be updated. A benefit of
working with JSON serialization rather than XML serialization is that
changes in the schema rarely cause issues for existing applications.
For instance, if a new property is added to the REST Services, an application that uses the old data contracts can still process a response without errors; however, the new property will not be available. To get the new property, you must update the data contracts. In order to make use of JSON serialization in .NET, use the DataContractJsonSerializer by referencing the following libraries in your project:
System.Runtime.Serialization
System.ServiceModel.Web
Source:
https://msdn.microsoft.com/en-GB/library/jj819168.aspx

Modifying SOAP requests with GetWebRequest

I am trying to dynamically modify XML data in SOAP requests to ASMX services.
I overrided GetWebRequest() method in SoapHttpClientProtocol class in order to read and modify XML data that the RequestStream contains.
The problem is, the request seems to be empty, there is no data in it whatsoever. Is this because the SOAP data hasn't yet been generated and serialized or am I doing something wrong?
What you need is a SoapExtension. You could hook into the SoapMessageStage.AfterSerialize stage in ProcessMessage to modify your soap message. I've done this in the past to add WSSE headers in situations where I couldn't add a dependency on Microsoft's WSE library (since it isn't available for Mono).
Complete tutorial here: http://msdn.microsoft.com/en-us/magazine/cc164007.aspx
GetWebRequest is too early for your purpose, GetWebResponse is too late.

Protocol Buffers over HTTP GET using WCF

I have a WCF service hosted that is returning a byte[] of protobuf-net serialized data. Originally this was hosted over the default webHttpBinding settings (SOAP) and everything was working properly. I recently found out that I need to call HTTP GETs and POSTs directly from our client so I thought it'd be easier to switch to use a RESTful service. I switched to use the WebGet attribute and the WCF REST Template.
I attempted to create a simple web site to test the client and I'm having trouble deserializing the data. This is an example of how I'm calling the service:
using (WebClient client = new WebClient())
{
result = client.DownloadString(url);
}
// Deserialize
BinaryVehicles binVehs;
using (var ms = new MemoryStream(StrToByteArray(result)))
{
binVehs = Serializer.Deserialize<BinaryVehicles>(ms);
}
An example of what is returned in "result":
< base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ChsKCzEyMy00NTYtNzg5EgU0NDAwMBoFQmxhY2sKHAoLOTYzLTg1Mi03NDESBTIzMDAwGgZTaWx2ZXI=< /base64Binary>
I also attempted to deserialize the data between the < base64Binary > tags with no results. Does anyone have any idea on how I should go about sending the binary protobuf-net data from an WebGet method and how I should deserialize the data? Thanks.
protobuf-net primarily handles just the serialization aspects (the protobuf spec by Google doesn't define any further than this). So it really comes down to: how are you serializing it?
I must admit that the WCF GET approach is not something I've looked at hugely, so there is no special handling there. One simple approach may be to look at just returning a string, and handling the base-64 encoding yourself, but to be honest if you are doing HTTP GET, then WCF itself seems overkill.
I blogged here about using ASP.NET MVC at the server for protobuf via HTTP GET. The linked sample code also includes a wire-compatible ASP.NET implementation.
If there is something appropriate we can do to make WCF GET easier, I'm all ears...

Raw SOAP data with WebServices in C#

Where can I find the RAW/object data of a SOAP request in C# when using WebServices.
Can't find it anywhere. Shouldent it be available in the HttpContext.Current.Request object ?
Shouldent it be available in the HttpContext.Current.Request object ?
No, it shouldn't.
What are you trying to accomplish? If you just want to see that data so you can log it, or as an aid to debugging, then see the example in the SoapExtension class. It's a working sample of an extension that can log input and output as XML. I've used a modified version of it myself.
If you're just looking to debug your web service, then you can install Fiddler, and that allows you to inspect the data sent to and from your web service.
It sounds like you're going to have to go lower level on your implementation if you want to see the raw XML. Check out the generic handler (ASHX extension). This will allow you to deal with the request/response streams directly. It's very low level, but gives you full control over the service lifecycle.
I found
Request.Params[null]
refers to the RAW data posted to the page in C# ASP.NET.

Categories

Resources