Generate Contracts for REST objects - c#

I'm new to REST and this sounds like it should be pretty simple. In a .NET app, I can create a reference to a WCF service and the contracts for all the available types will be generated for me.
Now I'm trying to consume a REST service in a Windows Phone 7 app. While I can make my call and get back the proper response, is there a simple way to create the classes that each object would be deserialized to?
I'm using RestSharp to manage my calls. In some examples I've seen, user's have created their own classes, and generated the xml manually. I would like to avoid this if at all possible.
many thanks!

Assuming your response is XML, you can save the xml into a file, then call xsd.exe on it to generate a schema. Call xsd.exe on the schema and it will generate a c# class file you can seriazlize and deserialize to from the xml. Here's the documeantion on how XSD.exe works:
http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx

You have to generate the classes that your response data will map to (or use a dynamic deserialization scheme if you're on .NET 4) since REST does not include a schema definition system the way SOAP does. In RestSharp, there's a T4 helper to make generating the C# classes easier. It gets you about 80% of the way there. If you need any help with it, post to the RestSharp Google Group.

Related

How to Generate C# Objects classes from Json schema for .Net 4.5+

First off, i found this:
Generate C# classes from JSON Schema
and although it seems to have the answer. It does not work in .net 4.5+.
I don't want to run compatibility in my application because its using a lot of new API functions.
My goal is to receive the Json, write to file the model.cs and the rest of my application can use it. But is there a library, resource or method to do this for newer applications?
https://marketplace.visualstudio.com/items?itemName=DangKhuong.JSONtoC
http://json2csharp.com/
https://github.com/ststeiger/json2csharp
or use: https://msdn.microsoft.com/en-us/library/system.web.helpers.json(v=vs.111).aspx
dynamic data = Json.Decode(json);

How to walk a C# object hierarchy and create JSON string?

I am creating a metadata site for our API. It is like swagger implementation. Currently I am facing difficulty with creating a sample JSON representation of our request response objects. These are complex objects that may even contain lists.
Right now I am at the point where via using reflection I am able to find all the request and their corresponding response objects.
Is there a library that can convert a reflection
assembly.GetType("FullyQuallifiedObjectName")
output to a JSON sample string? My research so far has not been fruitful.
Implementing your own inspector class may be too difficult and take much time. You may consider Microsoft's way to build help pages in WebApi. New Mvc4 and Mvc5 projects in VS2013 already contain complete help pages in WebApi templates.
(You do not need to implement custom XmlDocumentationProvider, just create a new WebApi project in VisualStudio 2013 and you will find completed xml doc provider)
If you use WebApi - it's exactly what you need. If not - modify and reuse already implemented xml doc provider.

Is it possible to generate C# class by deserialization some protocol?

I need to generate or define new class based on deserialization serialized class. So I want to transfer class definition from server to client to have access to it's properties later.
Is it possible and how?
Proper way to do it would be to either expose a schema definition for your service for clients to consume & generate strongly type class definitions from that or provide a DLL with your DTO contract definitions (class/interface definitions) to the client.
If you chose neither of those approaches (no schema & no dll with interfaces) but still
want to generate a class definition, you can in an improper way generate .cs class definitions, from a sample data of the service (call the services couple of times and intercept the responses or use some http client). However this approach does not guarantee that you will get an accurate or/and complete generation. Basically you can go from:
XML->XSD->C# cs class file (or even XML to C# cs file directly)or JSON->C# class file
And deserializing object to dynamic especially when you don't own both the server & client code is pretty much the worst thing you can do. And this way you didn't transfer you class definition to the client. Deserializng to dynamic objects is actually no desrialization at all as matter of fact, it gives you a dictionary of strings with syntactical sugar to access them as properties at runtime with not compile time support which can be equal to a disaster. In short don't do it unless you own all the code (not that it's a good idea then either but maybe you could get by somehow)
One portable way to transfer the property definitions and the data itself is to use the JSON serializer.
You can deserialize into a dynamic object using JSON.Net
Deserialize json object into dynamic object using Json.net

How can I parse REST service response?

I have set up my REST service and it works fine. This is the response that I get from the service is shown in the figure:
How can I parse this type of request in my WPF application?
The response you have there is seemingly an XML structure that contains publisher information. As for parsing it you have a number of options however all of these require/prefer you to have the schema for the resulting XML.
Use the Visual Studio XSD tool to create schema classes in your project. Once you have these you can then deserialise the XML into an object. You can then use the object within your WPF application.
Use XmlDocument to load the XML then use xPath queries to extract the data you need.
Use XDocument (linq to XML) to load the XML then use LINQ style queries to extract the data you need.
Personally, I would use option 1 - It does require a schema (the other options don't technically need one) but it does give you objects which IMHO are much easier to maintain and use than xpath/linq queries.
It is also worth mentioning that depending on how the service reference was added to the client (and how the service exposes itself) you may already have this XML class bound into the client service reference. As an example you service reference in the client may allow you to do this:
PublisherInfo pi = myServiceClient.GetPublisherInfo();
In which case all the conversion from XML to PublisherInfo is handled for you. I am guessing that the sample XML above was obtain by calling the service in a browser, therefore the conversion to PublisherInfo obviously doesn't happen as this would occur within the client code.
Well, there are a number of ways. You could use XmlReader, load it into an XmlDocument, and more.
The former of those options exposes a constructor that accepts an input stream and an XmlReaderSettings instance.
But how is this response returned? If you showed us some code, or provided a little more information then you might find we have different approaches that are more appropriate.
Why do you need to parse it, why don't you use the classes generated by Visual Studio when you add the reference of your service to the WPF project?
If you really want to parse it, use the Xml libraries mentioned in the other answers

Custom Business Object : AJAX Enabled WCF

Can I pass a custom object between AJAX enabled WCF and my asp.net page?
I searched the web but could not find any examples. Most shows simple types like string and integers.
I also do not know how to populate custom object's property through JavaScript on the client side.
We have a browser add on and we have to pass data to that addon from a web service, I researched and looks like AJAX enabled WCF is way to go
Using .net framework 3.5 and VS 2008
You can't pass the actual custom objects, but you can of course pass the serialized version of them through your service and to your page, javascript, etc. Basically, you have to map the fields of your complex custom .NET types to classes decorated with the DataContract attribute. These classes are the types that your service will return. The DataContract-decorated classes will contain fields with primitive types, like strings, integers, etc. The WCF service will serialize these into XML or JSON.
On the client side, jQuery will be your best friend. I personally prefer JSON because the properties of your objects are much easier to get at that way instead having to deal with parsing a bunch of XML. So, setup your service to output JSON.
Also, to make your service URLs easier to read, make sure to use a RESTful approach. It's as easy as decorating your service methods with the WebGet attribute and supplying a UriTemplate. Once you see some examples, it'll blow your mind. Note: if you ever encounter a WebInvoke with Method="GET", just use WebGet instead...it's more compact...no Method specification needed.
This particular article was EXTREMELY useful to me when I was developing my WCF service and the ASP.NET app that consumed it: http://www.c-sharpcorner.com/UploadFile/sridhar_subra/116/
Here's another person asking the same question as you: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/879d46af-9c78-4b5d-b746-82843d742a6f
Hope this helps! Long live WCF!
With .NET 3.5 your best bet is WebHttpBinding which accepts plain old XML (POX) and you need to send XML to the WCF service.
You can also use WCF REST using REST starter kit. For samples have a look here. This supports JSON as well.
If you were using .NET 4.0, JSON-enabled WCF HTTP was the way to go. WCF REST with 4.0 was an alternative although I really do not like it.

Categories

Resources