Fill the nullable .net service method in AX2012 - axapta

I want to set argument in a .net service methot. This method parameter type Nullable'1.
I create a method which name is findLastDate, return type of this method is TransDate. But when run the process take an error ;
System.NotSupportedException: DateTimeConverter cannot convert from Microsoft.Dynamics.Ax.Xpp.AxShared.utcdatetime.
Then i change findLastDate method's returntype to utcDateTime but unfortunately.
How can i fill this service method?

It looks like your issue isn't related to null but due to marshaling AX and CLR types.
See - https://learn.microsoft.com/en-us/dynamicsax-2012/developer/how-to-marshal-between-x-and-clr-primitive-types#code-sample-for-systemdatetime for how to marshal between the two.
I linked to the System.DateTime example in the article.

Related

How to 'package' request parms into one object

If you have a controller method which has a whole bunch of parameters such as:
getSomething (String a, int b, Timestamp c, etc...)
what is the best way to replace those with an object, so you do not have to then instantiate a new object and set all those from the parameters, like so:
SomeObject o = new SomeObject();
o.setA(a);
o.setB(b);
o.setC(c);
etc..
// use o on other method calls to prepare a response
I have tried #ModelAttribute like so:
getSomething (#modelAttribute SomeObject o)
but it does not seem to work well unless you only use simple types for properties such as strings and ints. A Timestamp property for example causes it to fail with a "Bad Request"
#RequestBody could work, but with some pretty significant changes required to your method call which ends up that you can no longer simply call that method by the browser anymore to test it, which makes development much more annoying.
Is there no way to tell Spring to accept a bunch of parameters in a regular http GET request and use them to instantiate an object for you with the ability to work with all property data types?
You probably need to write a type converter for the Timestamp class, and register it with Spring MVC ConversionService.
See http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
Why can't you use a Date or DateTime (Joda Time) class?
Make sure you use the #Timestamp annotation if using JPA

Cannot deserialize an object using a converter?

Given a JSON string of the form {"$type":"MyType, MyAssembly","seed":0"}, why can't JsonConvert.DeserializeObject utilize the JsonConverter associated with "MyType"?
I've tried decorating the MyType class with a [JsonConverter(typeof(MyType))] attribute. Doesn't work. The custom JsonConverter's ReadJson method is never called.
I've tried adding the custom converter to the serializer's settings Converters collection and made sure the CanConvert method returns true for 'MyType' and the CanRead method returns true. Doesn't work. Neither the converter's CanConvert nor its ReadJson method is ever called.
The DeserializeObject method needs to be able to deserialize a JSON string containing an object whose type is unknown at compile time, but whose type is embedded in the JSON object via the special "$type" member. So don't suggest using DeserializeObject<T> or point out that it works for members, whose type is identified in the contract before hand.
FYI, this problem generalizes to cases where the deserialization needs to identify the object type solely from the embedded "$type" member, so for example, it also fails to resolve a converter if the JSON object is in an untyped JSON array, not just at the top-level.
Basically, an object cannot survive a round trip through the serialization/deserialization process, because although the WriteJson method will be called for the Converter when SerializeObject is called, when you subsequently pass the JSON string to DeserializeObject, it fails to call the converter's ReadJson method, and instead constructs an new instance and uses the basic member population routines.

About JAXB Databinding in JAX-WS

I have developed webservices using CXF framework. We are exposing all our Database operation(Service Layer) methods as a web service methods. We are using Hibernate for persistence.
For example consider I have one Java POJO class(for hibernate mapping)
named CountryEntity.java with the following
properties(ID,Name,ModifiedDate).
Inside my service layer i have two methods called
#WebMethod
public CountryEntity createCountryEntity(CountryEntity countryEntity);
#WebMethod
public CountryEntity getCountryEntityById(long id);
Based on JAX-WS documentation i understand that it is using JAXB to do
the databinding. When user calling my webservice for creating the new
Country record i dont want the user to send the Id value and Modified
Date value . So what i done is i annotated the propeties(id,modified
date) inside the CountryEntity.java with #XmlTransistent annotation.
So that operation is working fine.
But when i call getCountryEntityById web service method its returning
CountryEntity object but that object dont have the values for ID and
Modified Date property. I understand because of XMLTransistent
annotation the values not getting Marshalling.
Can i change my method createCountryEntity taking parameters like
(String countryName) , so that i no need to include XMLTransistent
annotation in my entity classes.? or Anyother solution for this?
Please help me . Thanks in advance
Since no answer i go with parameters instead of sending an object as parameter. Thank you.

Optional parameters in ASP.NET web service

I have a ASP.NET web service. This web service works fine. However, the WSDL lists some parameters as optional (minoccurs = 0) and others as non-optional. Some of the optional parameters are actually not optional, others which are marked as non-optional are actually optional. I would like to fix this, but I can't find the location where this information is coming from.
It seems to me that all primitive types (int, boolean etc.) are non-optional and all other parameters are marked as optional. However, I can't find a location where I can change this. I would like to specify default values for the primitive values if they are missing in the request and specify which non-primitive parameter is actually optional. Where do I do this?
I am assuming that when you say ASP.net web services, you are creating web services with ASMX extension. I think that what happens in this case is that all nullable types become optional and non-nullable become non-optional.
You could perhaps manually edit the generated WSDL file. But then you would have to redo that work if the wsdl was regenerated.
I would suggest that you switch to WCF with basisHttpBinding (except for the name of you service your clients should not notice the difference).
Using WCF you can simply mark the parameter in the data contract as required or not:
[DataMember(IsRequired="false")]
The primitives are not reference types, but rather they are value types. You can make a value type "nullable" a couple ways.
The short-hand is
int? i;
or long-hand here
Nullable<int> i;

Flex & WCF - Enum error

I have a WCF service operation that accepts a data contract parameter of custom type MyQuery -- function Search(q as MyQuery). The MyQuery object contains 2 properties:
MyQuery.SearchPhrase (string)
MyQuery.SearchType (custom enum SearchTypeEnum)
I also have a Flex client application that consumes this service. But when Flex invokes the Search() operation, I get the following error about the enumeration property:
"Cannot find definition for type
'http://mydomain/2009/04/SearchTypeEnum::SearchTypeEnum"
The error is thrown from Flex while it is building the request to the service.
So my question is..... is there any way to work around this issue in Flex? Or is my only alternative to redesign the service without enums?
This is how the enum data contract is defined at the service tier:
<DataContract(Namespace:="http://mydomain/2009/04/SearchTypeEnum")> _
Public Enum SearchTypeEnum
<EnumMember()> [Boolean] = 0
<EnumMember()> [NaturalLanguage] = 1
End Enum
As far as I know this is the correct definition of an enum data contract. I also tried using the ServiceKnownType and KnownType attributes on the service and on the MyQuery class but to no avail.
I would recommend you try that with a trivial .NET client - maybe even a console application. The idea would be to see if you can get any application to work with the service.
I could not find a way to get this to work. Instead I replaced enums with string constants. Not as elegant, but it works.

Resources