how to remove a property from wcf response xml - wcf

I don't want to show the property in my xml response when it does not contain data.
[DataMember(EmitDefaultValue = false)]
List<Details>
I will be having data to this property in some scenarios and will not have data in this property in some scenarios. I do not want this property in my xml response when I do not have data in it.

I am able to implement it by initializing my property in the constructor as null and initializing it again in my API when the data is present for that property.

Related

In net 5, when binding from body, how to specify that all properties must be present?

Attributes like BindRequired make an action parameter to be required, even the usual model validation attributes can be applied. How can you make all properties from a class be required to be present in the request body?
If you have access to change the class: decorate all the properties with[Required] or [BindRequired].
If you don't, you should make a DTO (Data Transfer Object) for that class and apply [Required] or [BindRequired] attributes.

To Access an Object from string Hierarchical Object

I have a string object like this. I have tried deserializing using newtonsoft json converter but it results in the null object. I got to know the reason behind which is that the object I tried to convert to is inside the "data". Hence, it returns Null always for each property after conversion.
I want to know how I can access the "data" directly from this object?
{"data":{"providerRef":null,"orderId":"4579144","orderStatus":"x:app_pending","applicantInterfaceURL":"http://google.com","successful":true,"error":null,"reportAddress":null,"correlationId":"55f7022c-28f9-490a-8dd1-b30a40e3467a"},"status":0,"error":{"actionArguments":null,"errorCode":null,"errors":null,"message":null}}
Now, I have implemented it like
The class VolunteerBackgroundCheckResponse have these properties:
Can anyone help me getting through the data object only from the json string?
This is solved. I have used the JObject for the purpose and received all the properties beneath the main object. I accessed "data" and deserialized it.

Why do i need to set IsRequired if it comes from class?

I'm having trouble to understand the situation which Is :
The Host has a service which has operationMethods
for example Calc (Myclass a)
MyClass contains 2 properties ( prop1 , prop2)
Now , the clients also whould know about MyClass via proxy and should know also contract by reference.
So when the client creates new instance of MyClass (to send it)- how can one prop can be missing ???
when he instancing new MyClass - both prop are initialized (null or what ever). so why do i need the isRequired ?
Theres no guarantee as to what language or how they are formulating their soap request, so the way they are generating it may not use a generated class that contains the property. Is required requires that is present in the message
Interaction with IsRequired
As discussed in Data Contract Versioning, the DataMemberAttribute attribute has an IsRequired property (the default is false). The property indicates whether a given data member must be present in the serialized data when it is being deserialized. If IsRequired is set to true, (which indicates that a value must be present) and EmitDefaultValue is set to false (indicating that the value must not be present if it is set to its default value), default values for this data member cannot be serialized because the results would be contradictory. If such a data member is set to its default value (usually null or zero) and a serialization is attempted, a SerializationException is thrown.

WCF result deserializing to default values for value types in a list of key/value pairs

I have a WCF service and the result is a custom TimeSeries class defined as:
[DataContract]
public class TimeSeries
{
[DataMember]
public string Name { get; set; }
[DataMember]
public List<KeyValuePair<DateTime, double>> Data { get; set; }
}
My service method creates an array of these objects to return. Debugging the service method, I can see that an array containing one of these objects is created correctly (it has a name and 37 vk pairs of data). Using Fiddler, I can see that the object is being serialized and sent (the data is still correct in the HTTP response). However the problem comes when on the client I check the result object and it is incorrect. Specifically, I get a TimeSeries object with the correct name, and the correct number of of kv pairs, but they contain the default values for each DateTime and double (ie 01/01/0001 12:00AM & 0.0).
My client is Silverlight v4 and I am using an automagically generated service reference. The problem appears to be related to deserialization. Anyone have any thoughts as to why it is doing this, what I am missing, or how I can fix it?
As it is stated in Serializing a list of Key/Value pairs to XML:
KeyValuePair is not serializable, because it has read-only properties
So you need your own class, just like the answer on that page says.
An alternative rather than using your own class is to use a Dictionary<DateTime,double> instead which seems to serialize and deserialize fine.

How to transfer objects through the header in WCF

I'm trying to transfer some user information in the header of the message through message inspectors.
I have created a behavior which adds the inspector to the service (both client and server).
But when I try to communicate with the service I get the following error:
XmlException:
Name cannot begin with the '<' character, hexadecimal value 0x3C.
I have also get exception telling me that DataContracts where unexpected.
Type
'System.DelegateSerializationHolder+DelegateEntry'
with data contract name
'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System'
is not expected. Consider using a
DataContractResolver or add any types
not known statically to the list of
known types - for example, by using
the KnownTypeAttribute attribute or by
adding them to the list of known types
passed to DataContractSerializer.
The thing is that my object contains other objects which are marked as DataContract and I'm not interested adding the KnownType attribute for those types.
Another problem might be that my object to serialize is very restricted in form of internal class and internal properties etc.
Can anyone guide me in the right direction. What I'm I doing wrong?
Some code:
public virtual object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var header = MessageHeader.CreateHeader("<name>", "<namespace>", object);
request.Headers.Add(header);
return Guid.NewGuid();
}
Don't put the angle brackets into the actual strings. Remember, the serialization format may not even be text based, all you're doing is specifying the name of the element and the namespace. So your code should look more like this:
var header = MessageHeader.CreateHeader("name", "urn:myNamespace", object);
request.Headers.Add(header);
To close this question, I never solved the exception. Instead I implementated ISerializable which worked great for me.