I'm not 100% sure what the correct terminologies are but..
I have a class called InParams with two fields, a string and a long and their corresponding Property accessors to the fields. These are decorated with [DataContract] and [DataMember] respectively.
I have a WCF service method called void Test(InParams inParams)
The proxy generated fine with svcutil and I was able to set the long field, however when the service method is execute the long field is always 0, even though I explicitly set the long field. I looked at the soap envelope and don't see a tag for my long field.
When I change the long field to a string field it gets serialized. This is the same for ints as well.
Am I missing an attribute or something?
can you post a sample? double check to:
ensure class has [DataContract()] decoration
ensure PUBLIC properties have [DataMember()] decoration
Ensure your proxy class is up to date by removing/regenerating it. See if that makes a difference?
If you have a boolean YourPropertyNameSpacified property on the client in addition to YourPropertyName, you must set it to true on the client. This goes for all fields of value type, I believe. Also see WCF service proxy not setting "FieldSpecified" property.
In addition to doing what Tanner mentioned, longs and ints are clearly supported by the DataContractSerializer.
.NET Framework primitive types. The following types built into the .NET Framework can all be serialized and are considered to be primitive types: Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Boolean, Char, Decimal, Object, and String.Link
Related
I have an endpoint that receives an DTO into the body.
The DTO has a ULong attribute that the Jackson do not parse (deserialize) when I request the
endpoint with postman.
Also, I try to create my custom serializer?
class ULongSerializer: StdSerializer<ULong>{
override fun serializer(value: ULong, gen: JsonGenarator, provader: SerializerProvider){
gen.writeStartObject()
gen.writeNumber(value) //HERE IS THE PROBLEM
gen.writeEndObject
}
}
neither write number method do not support ULong, nor another methods like write field.
Can someone give me some tips to deal with this?
ULong is a class and you may write Jackson serializer/deserializer for it, but a field of this type is compiled to bytecode as a field of primivite long type. So values exceeding Long.MAX_VALUE would be serialized/deserialized incorrectly (as negative), and custom serializer/deserializer won't help you.
Take a look at kotlinx.serialization. It have OOTB experimental support for unsigned integer types. See https://github.com/Kotlin/kotlinx.serialization/blob/master/CHANGELOG.md#experimental-support-for-inline-classes-ir-only
I am attempting to migrate a legacy vb.net application to .net standard and turn it into a nuget package. A good amount of it has been straight forward. I am currently hung up on this error caused by functions like this.
Public Property ErrorMessages As Collection
Get
ErrorMessages = _errorMessages
End Get
Set(value As Collection)
_errorMessages = value
End Set
End Property
If i import System.Collections.ObjectModelCollection(Of T) it is asking me for a type and i am unsure how to proceed. It turns my code into
Collection(Of,) and expects a second argument. Has anyone faced this before? Do i use a different import statement or how is this dealt with in vb now?
You should almost certainly replace Collection with Dictionary(Of TKey, TValue), using the dictionary type from the System.Collections.Generic namespace.
Once again, this requires you to fill in the genetic type arguments TKey and TValue with the actual types. You need to figure out from context which type fits the collection. The value of TKey is probably String since that’s the only key type VB6’ collections properly support. And given the name (ErrorMessages), TValue is probably String as well.
Let's say I have an existing method in a WCF service:
[OperationContract]
CustomerInfo GetCustomerInfo(int customerId);
It turns out I need to change this to take a long as its parameter instead of an int. Would that break existing clients? Or is there some implicit conversion that happens?
In the code base I'm working in there have a method that has the signature
Public Sub SetDropDownValue(Of T As Structure)(ByVal target As ListControl, ByVal value As Nullable(Of T))
The method I am writing is passed a parameter of type object.
How can I cast the object into something that can be passed into the SetDropDownValue method?
No, you won't be able to cast a reference type as a value type (which is what the Structure constraint signified). The CLR does allow you to cast a value type as a reference type (this is known as boxing) but the nature of the difference between the implementation (and semantics) of these two different types makes the reverse impossible.
The only think you could do would be to create a value type that held a reference to your object as a field, but perhaps this problem may be a hint that you are going about the whole thing in the wrong way.
This should work if you know T:
something.SetDropDownValue(target, DirectCast(value, Nullable(Of T)))
See this article for details.
If you don't know the type T, you're in trouble and would have to start futzing around with reflection at runtime. This is complex, dangerous, and has awful performance.
I am adding a feature to an existing VB .Net application that involves retrieving data from a .Net web service. The web service returns an array of Locations. A Location is pretty simple, it has 3 properties – an integer and two strings.
So that the rest of my application does not have to be dependent on this web service, I would like to create my own Location type within my application. My thought is I could call a method that returns a generic list of my Location type which internally calls the web service and populates the list I return. That way, if the data source for Locations changes in the future to something other than the web service, I only have to fix the method instead of fixing all the callers.
So I created my own Location that has identical properties as the service Location. But I don’t seem to be able to cast the array of service locations into a generic list of my locations. I also tried casting a single service Location into one of my Locations and that didn’t work either.
So is casting an option or do I have to loop through each service Location and assign each property to a new one of my Locations? Or is there a completely different way to go about this?
By default you will not be able to cast one Location to another. They are completely unrelated types and thus cannot be the subject of casting. You can make it possible to cast though by defining a custom operator for the application version of CType.
' Location for application
Public Class Location
...
Public Shared Operator Widening CType(p1 as Namespace.Of.WebService.Location) As Location
Dim loc = ConvertWebServiceLocationToApplicationLocation
return loc
End Operator
End Class
This allows you to now do a CType operation between a WebService Location the Application Location.
Casting the array though, simply won't be possible. There is no way to define a conversion operator for arrays so they can't use the above trick. But you can write a quick and dirty function for this purpose
Public Shared Function ConvertArray(ByVal arr As Namespace.Of.WebServiec.Location()) As Location()
Dim newArray(arr.Length) As Location
For i as Integer = 0 To arr.Length - 1
newArray(i) = CType(arr(i), Location)
Next
return newArray
End Function
Casting will not work because these are not the same type. Even if two types look exactly the same, you cannot cast from one to the other, unless you define a CType operator which describes how to transform an object from one type into an object from another type.
Even then, you cannot cast a List(of Type1) into a List(Of Type2) directly.
You will have to loop through and create a new object of your class.