Tuesday, July 30, 2013

MVC 4 - WCF - Add Service Reference Generates Blank Proxy

After a long time I created a WCF Service with ws2007HttpBinding which has a complex data model (with inheritance involved). I created the service as usual with the well known configuration and hoping it to work. I just run the application and it ran without any error. Bingo! wow great, I created a service in single go!

Now its time to create a client application. The client I was trying to create is MVC 4 web application (which I am most familiar with). The app was already present and I just need to add a service reference to consume my wcf service. As usual I just right clicked the project, clicked on AddServiceReference, entered the service url, given a friendly name to the service and clicked Ok. Wow! I'm ready with a client for my service. Now its time to call the service method by creating an instance of the client proxy. Being a lazy guy waiting for the visual studio intellisense to show up the given friendly proxy name by typing its initial few letters. Unfortunately, intellisense was not showing anything. OMG! need to wake up now.

Started to trace to identify what went wrong:
            First step I did is checking the proxy code (because I can't relay on intellisense for 100% accuracy. There are times, when everything is fine but intellisense itself is having problems). Well, I did that by clicking on the marked places below:

I noticed that the proxy classes are not generated and the Reference.cs file is blank:

Now my mind also became blank! I was following all the holy steps but still not able to wok as expected. Went back to my WCF application and started to verify following things:
  • Whether all my required data structures are decorated with DataContract (classes) & DataMember (properties) or not.
  • Whether the base class is decorated with DataContract or not. 
  • Whether the KnownTypes are set or not.
Everything was looking fine there but still proxy is not getting created. Frustrating!!!

Decided to check the service with wcftestclient (vs command prompt -> wcftestclient -> Right click & click on Add Service -> Enter service url -> ok) tool. 


Wow, great! it works here. Then, what might be the problem with my MVC 4 application. Just got another thought. Created a new console application and added the service reference in the same was as I did in MVC 4 app. Amazing, worked with single go.

Now the thing is pretty clear that there is something wrong in MVC 4 app. Its time find the solution (well just a google search will do it).

Solution:
Go to, configure service reference.


Select the option, Reuse types in specified referenced assemblies -> Check all except Newtonsoft.Json -> ok

Thats it. The proxy will be created now. We can do this while adding the service reference itself, by going to the Advanced window.

Note: Microsoft has fixed this issue in this update: http://support.microsoft.com/kb/2750149

The Cause (by microsoft): 
This issue occurs because the DataContractSerializer class has encountered a type 
(Newtonsoft.Json.Linq.JToken) that it does not support. In this case, it throws an exception, 
and then stops generating the service reference.

Conclusion: Since I am working in WCF after a long time I was continuously thinking that there is something wrong in my service and spent a lot of time to figure it out. Also, I missed one basic step in my tracing with which I could have saved my time (i.e. checking the Error List). Hope this article helps to someone else in future.

Out-of-interest, the error list was showing this error & warning.

Error:Custom tool error: Failed to generate code for the service reference 'ServiceReference1'.
Please check other error and warning messages for details.

Warning:
Custom tool warning: Cannot import wsdl:portType
Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. 
Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.

44 comments:

  1. If this works i am thanking you in advance :)

    ReplyDelete
  2. Thanks for the post. You have saved me a great deal of time!

    ReplyDelete
    Replies
    1. Thanks for the comments. I'm glad that it helped.

      Delete
  3. Thanks Naveen for posting . i have wasted 2 days on this Issue..
    Now its working Cooll...!

    ReplyDelete
  4. Thanks for the post. It is very helpful.

    ReplyDelete
  5. You have saved my life. Thanks a ton.

    ReplyDelete
  6. Thanks man!!! this is the life saver,I was stuck in this .

    ReplyDelete
  7. Hi Naveen , im new to mvc4 and wcf , now i have to start one project in mvc4 and wcf .i need ur help.

    ReplyDelete
  8. Hi Naveen, I have done the same steps which you had mentioned but still I was not able to add servicereference in mvc4 controller

    ReplyDelete
    Replies
    1. Are you able to generate the proxy using test client or any console application?

      Delete
    2. The same problem of Newtonsoft.Json has System.Web.Providers

      Delete
  9. Thanks buddy,

    You saved me thanks again

    ReplyDelete
  10. hi naveen
    i have followed your steps and its not generating the service reference file for me .help me on this

    ReplyDelete
    Replies
    1. Are you able to generate the proxy using test client or any console application?

      Delete
  11. thank you! days of trying to get past this one have been solved by this post!

    ReplyDelete
  12. Thank You very Much I Got Same Problem And I Solve It.

    ReplyDelete
  13. https://msdn.microsoft.com/en-us/library/bb628649.aspx

    ReplyDelete
  14. Use above link provide those who are using VS2013

    ReplyDelete
    Replies
    1. Thanks for the link. Will update the article once I get time.

      Delete
    2. Hi, The link provided by Ashutosh seemed to work for me. But what i don't understand is why the normal way of adding a service reference is not working compared to the old way of adding the service. From my knowledge, the new way (Service Reference -> Add Service Reference ->OK) should be easier and better than the old way (Service Reference -> Add Service Reference -> Advanced -> Add Web Reference -> Add Reference) . . . Does anyone know why this is happening?

      Thank you Naveen for the article, it helped me get to the solution :)

      Delete
  15. interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts,Thanks a million once again, Regards servicenow training in hyderabadservicenow training in hyderabad

    ReplyDelete
  16. Thanks so much, I searched for hours before finding the all-important info "This issue occurs because the DataContractSerializer class has encountered a type
    (Newtonsoft.Json.Linq.JToken) that it does not support. In this case, it throws an exception,
    and then stops generating the service reference."

    ReplyDelete
  17. It costs under ten dollars for each month, running from four to seven dollars for every month.
    does avast vpn keep logs

    ReplyDelete
  18. Now that you understand the basics of setting up a wireless router you can set up any wireless networking device you like. Whether your setting up a reach this article http://whatsmyrouterip.com/

    ReplyDelete
  19. Are you looking for Web Application Development in India? Kanhasoft is providing Web Application Development services globally to boost your business. Get custom web application solution with professionals.

    ReplyDelete
  20. VPN that does not cost you anything in advance. Most free VPN administrations offer just PPTP which is viewed as out of date.https://www.techpally.com/vpn-for-digital-privacy/

    ReplyDelete
  21. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    web application development

    ReplyDelete
  22. A lot of people having an incorrect image about the cash advance loans or sometimes refer it as bad credit payday loans. Klik hier

    ReplyDelete
  23. Your article is extremely helpful exceptionally fascinating subject i am looking that sort of post thank for imparting to us keep it up. bezoek website

    ReplyDelete
  24. They empower cooperative work including circulated communities of entertainers who sometimes, if at any point, meet physically. prywatnoscwsieci

    ReplyDelete
  25. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up deze website

    ReplyDelete
  26. Always so interesting to visit your site.What a great info, thank you for sharing. this will help me so much in my learning internetet securite

    ReplyDelete
  27. Awesome things you've generally imparted to us. Simply continue written work this sort of posts.The time which was squandered in going for educational cost now it can be utilized for studies.Thanks https://internetetsecurite.fr

    ReplyDelete
  28. شركة السالم لخدمات التنظيف ومكافحة الحشرات ونقل العفش مع الفك والتركيب بالطائف يتم العمل لدينا من خلال فريق وعماله فنيه مدربه في غاية الاتقان ومن خلال احدث المعدات والاداوات مع شركة السالم فانت دائما في راحة تامه وامن مستمر
    شركة تنظيف بالطائف
    شركة تنظيف مجالس بالطائف
    شركة تنظيف خزانات بالطائف
    شركة مكافحة حشرات بالطائف
    شركة رش مبيدات بالطائف
    شركة عزل اسطح بالطائف
    شركة تسليك مجاري بالطائف
    شركة نقل اثاث بالطائف


    ReplyDelete
  29. Very Helpful and informative blog! Keep sharing such blogsSoftware Development Company in India

    ReplyDelete
  30. شركة السالم لخدمات التنظيف ومكافحة الحشرات ونقل العفش مع الفك والتركيب بالطائف يتم العمل لدينا من خلال فريق وعماله فنيه مدربه في غاية الاتقان ومن خلال احدث المعدات والاداوات مع شركة السالم فانت دائما في راحة تامه وامن مستمر
    شركة تنظيف بالطائف
    شركة تنظيف مجالس بالطائف
    شركة تنظيف خزانات بالطائف
    شركة مكافحة حشرات بالطائف
    شركة رش مبيدات بالطائف
    شركة عزل اسطح بالطائف
    شركة تسليك مجاري بالطائف
    شركة نقل اثاث بالطائف


    ReplyDelete
  31. شركة السالم لخدمات التنظيف ومكافحة الحشرات ونقل العفش مع الفك والتركيب بالطائف يتم العمل لدينا من خلال فريق وعماله فنيه مدربه في غاية الاتقان ومن خلال احدث المعدات والاداوات مع شركة السالم فانت دائما في راحة تامه وامن مستمر
    شركة تنظيف بالطائف
    شركة تنظيف مجالس بالطائف
    شركة تنظيف خزانات بالطائف
    شركة مكافحة حشرات بالطائف
    شركة رش مبيدات بالطائف
    شركة عزل اسطح بالطائف
    شركة تسليك مجاري بالطائف
    شركة نقل اثاث بالطائف


    ReplyDelete
  32. Very prudent job. I like the the way you threw the whole information. It's a great work you have prepared this awesome read for your readers. I also intend to share a site relevant on payday loan 소액결제현금화 that provide loan very easy and instant approval.

    ReplyDelete
  33. Aside from this, no other website will be facilitated in your space which forestalls any mutual vindictive product from influencing your website and framework execution. vps

    ReplyDelete