I am having issues with serializing objects to JSON, and being new to WCF, I am having issues on where to start in terms of debugging. All I get if I hit the service by typing the url in the browser is page not available.
The scenario: I have class A which inherits a List of class B. If I comment out in Class A where it adds to it's collection, I can at least hit the service (just obviously no data will be present), but if it adds to its collection, I can no longer hit the service.
Interface:
<OperationContract()> _
<WebGet(UriTemplate:="getAdverseReactions", responseFormat:=WebMessageFormat.Json)> _
<ServiceKnownType(GetType(AdverseReactions))> _
<ServiceKnownType(GetType(AdverseReaction))> _
Function GetAdverseReactions() As AdverseReactions
Implementing interface:
Public Function GetAdverseReactions() As AdverseReactions Implements IPortalCoreService.GetAdverseReactions
Return CoreServiceController.GetAdverseReactions()//which returns class A
End Function
Class A:
<CollectionDataContract(Name:="AdverseReactionsCollection", ItemName:="AdverseReaction")> _
Public Class AdverseReactions
Inherits List(Of AdverseReaction)
Class B:
<DataContract(IsReference:=True)> _
Public Class AdverseReaction
I stepped through the code via attaching a process, and no exceptions are thrown and I can confirm that the objects are returned as they should be, I just obviously cannot serialize it. I have read about circular references, and a friend of mine suggested that these two classes might be serializing each other in an infinite manner.
My main question: Is there a place I can look to see why this is occurring, or at least some more information about it? This issue has been handling me, all I want is to serialize this and when I do I think I will take a weeks vacation :).
In Solution Explorer, right-click the WCF Client project and then click Set as Startup Project. Enable debugging in the app. config or web. config file.
You can access the window from Tools > Odin Inspector > Serialization Debugger and select your script type from the dropdown to start debugging. Or you can directly start debugging a component by clicking the Debug Serialization button in the component's cogwheel dropdown menu.
This code constructs an instance of the DataContractSerializer that can be used only to serialize or deserialize instances of the Person class. DataContractSerializer dcs = new DataContractSerializer(typeof(Person)); // This can now be used to serialize/deserialize Person but not PurchaseOrder.
The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding . Net objects. This process is called serialization.
Add the following block to your web.config <configuration>
block
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\log\WebTrace.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
and ensure the directory specified (c:\log) exists and is writable by the IIS service.
Once this is done, perform the action that causes the serialization issue then navigate to the directory and double-click the generated svclog file.
This will open the file in the Microsoft Service Trace Viewer. Once this is opened, you will see errors displayed in red down the left-hand side.
Clicking on one of these will show the details in the top right pane and you can click on each of the actions to determine what WCF is complaining about.
While this is slightly dated (2010), I think it will give you some ideas of paths to try.
Quickly finding WCF Serialization/Deserialization Issues
This previous StackOverflow question might help to:
How to trace WCF serialization issues / exceptions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With