Solved

WCF Rest JSON Serialization

Posted on 2016-07-26
12
78 Views
Last Modified: 2016-08-01
I am planning to use WCF rest JSON.  In past I have seen serialization issues it.  I read people using newtonsoft.json as default serializer

I see some explanation below of using it. I need some example where we I can use with newtonsoft.json . also want to handle throw back http errors to the client

http://stackoverflow.com/questions/3118504/how-to-set-json-net-as-the-default-serializer-for-wcf-rest-service.


One more question Other have to host on windows service so going with WCF . I dont tjhink Web api is solution here??

Thanks
0
Comment
Question by:Member_2_7967608
  • 6
  • 6
12 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 41729681
WCF vs WebAPI really depends on what you want.

If you want to do a "simple" REST service then go with WebAPI, hand down. When I say "simple" is architectural simplicity, business wise there's no limitation.

If you want to have a more advanced implementation, supporting SOAP and REST endpoints, or realy put your hands on how the communication is done at a lower level, then WCF is the way to go.

From what I see on your tags, you want to implement a REST service so, in that case, go for WebAPI, it's all you need in terms of functionality and already serializes your responses to JSON without any problems.

Cheers,
Alex
0
 

Author Comment

by:Member_2_7967608
ID: 41730136
Hi Alex

Thanks for the comments. My consumer is AngularJS.

I want to host in IIS Web api or WCF Rest. Here are some more details

1. I have to host 2 methods. One for GET and other For Update.
2.  The GET method invoke 3 ext services to get the data and finally gets data.
3. One issue I had earlier is with the DataContract Serializer of WCF. I had to use Json.NET Serializer to handle null values and ignore.

So Web api would be better choice, right?
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 41730216
Yep, definitely.
In your case I go WebAPI without a doubt.

What kind of issue did you have regarding the null values?
Null on the inputs or on the output?
0
 

Author Comment

by:Member_2_7967608
ID: 41733063
I want to ignore Properties with Null to serialize to Json. I think the default Serializer does not let us do that.   Also If some properties we dont want to serialize and ignore we cant implement it.
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 500 total points
ID: 41733493
WEBAPI allows you to control the serialization through what they call formatters, and in your case is pretty simple. Should be something like:
 config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore

Open in new window

0
 

Author Comment

by:Member_2_7967608
ID: 41733698
Thanks so much.

One last question,  in  Web Api I need to use some proxy URLs from the APP.config.

System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]

<appSettings>
    <add key="URL1" value="http://test2" />
    <add key="URL2" value="http://test2" />
  </appSettings>

1. Should I create a Static class and use those URLs.
   Since the Webapi is stateless. So for every call it will make configuration access.
   I am checking for any performance issues on service hits.

2. Or I can pass it in the Angularjs Request body these 2 URLs everytime.

3. Is there any other mechanism?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 41733799
Hi mate,
what do you mean by proxy URLs?

What are you trying to achieve? Reverse proxy for the AngularJS ajax calls?
0
 

Author Comment

by:Member_2_7967608
ID: 41733841
The Webapi will be calling to 2 external soap web services.  They have 2 address endpoints.

 So I need to have the soap endpoints  of the services because they change based on dev or test environment.

<I was trying to simplify this as URL in my message>
0
 

Author Comment

by:Member_2_7967608
ID: 41733914
Let me know you need further clarification.
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 41733972
Oh, ok, got it now.

Yes, the best is to put that kind of config in the web.config file as appSettings as you did in your sample.
Then you can just add one config per environment and use XML transformations to add the correct values depending on the environment you're building to.

Another approach is to put that config in the server (host) configuration, as a server variable of something like that.
This is what I do when I want to promote packages throughout environments instead of packaging each all the environment configs in plain text inside (some I might not want the devs to know).
With this approach, instead of picking the config from the web-config you just pick it from the configuration on the server.
This is a lot less usual to find but definitely the way to go if you're serious about continuous delivery stuff :)

I guess I went a bit off track on you here, sorry :), but I think this second option is good information for you to keep in the back of your head for possible future usage.

Cheers mate!
Alex
0
 

Author Comment

by:Member_2_7967608
ID: 41734886
Thank you Alex.

I have option to store  on web.config.

I guess my question was based on performance. Are the config values stored in IIS worker process for this app memory or they are initiated with Every request.
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 500 total points
ID: 41735012
Well, it's fair to be said that reading values from web.config shouldn't be a performance issue unless you're really hammering that service like mad.
Anyway, if you read those values on the global.asax and put them in a static config object, then you're set. Basically, they will be loaded into memory throughout the lifecycle of the application pool.

So, use the application_start method on the global.asax to load your settings and put them in a static configuration variable of class.

That's it :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to compare two table schema using c#? 6 32
Build JSON from table records 17 41
Check my code out(2) 1 35
VB.NET HttpWebRequest 12 34
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now