If a AJAX-Enabled WCF should return an Object with a Date Property which is Nothing a Request to this Service fails.
Test.svc
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports System.Runtime.Serialization
<ServiceContract(Namespace:="WebApp")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class Test
<OperationContract()>
<WebGet()>
Public Function TestOperation() As CompositeType
Dim obj As New CompositeType
obj.DateProp = Nothing
obj.StringProp = "Test"
Return obj
End Function
End Class
<DataContract()>
Public Class CompositeType
<DataMember()>
Public Property DateProp() As Date
<DataMember()>
Public Property StringProp() As String
End Class
scripttag on Testform.aspx
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('./Test.svc/TestOperation', function (data) {
alert(data.d.DateProp);
});
});
</script>
Web.config (serviceModel section)
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebApp.TestAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="WebApp.Test">
<endpoint address="" behaviorConfiguration="WebApp.TestAspNetAjaxBehavior"
binding="webHttpBinding" contract="WebApp.Test" />
</service>
</services>
</system.serviceModel>
Chrome displays on this call the following error message: “failed to load resource”.
Has anyone an explanation/idea for this behaviour and how are you supposed to prevent this problem?
Thanks for your advice 😉
If you want to be able to assign
Nothingto aDateTimefield you should make it nullable:I am not a VB.NET expert but as far as I know the
Datekeyword in VB.NET is an alias to the DateTime structure (which is not nullable), except that VB.NET must be doing some black voodoo magic to allow you to assign Nothing to it. Except that when the serializer attempts to serialize it into JSON the following exception is thrown: