DataMember and DataContract - datamember

I am working on a project and I need to include [DataMember] and [DataContract] in my code but when I do that these attributs could not be found.
Is there a "Using" or a reference that I should include for these to work?
Thanks for you help!

Related

IsWrapped property of MessageContractAttribute is missing in Mono

I have a problem with generated proxy class in wcf. I noticed the MessageContract is wrapped in my soap message, but i need to remove it. Unfortunatly, I can not resolve IsWrapped property of MessageContract attribute.
(I would like to add service reference to my portable library, using MVVMCross)
MSDN: http://msdn.microsoft.com/en-us/library/ms750528.aspx
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContract(IsWrapped = false)] //Cannot resolve symbol 'IsWrapped' (default value is true and i cannot change it)
Thanks in advance!
I found that it is not possible from within a PCL assembly, because the WCF implementation is not cross platform and available in PCL libraries. I have used the IsWrapped property to be implemented in the MonoTouch projects. I have no experience with MonoDroid and WCF.

XML generated by WCF client is missing some elements

I am writing a WCF client to communicate with a JAX-WS web service. Basically communication with the service works. But when investigating the XML generated by the WCF client there are some elements missing. All the properties are correctly generated and I have set them in my code. I am new to WCF and web services in general so I have problems to analize what could be wrong here. What could cause the missing elements in the XML? The only thing I noticed all these missing properties have in common is that they are enumeration types. But other than that I found nothing. For example there is a enumeration for country codes. An entitity has 3 properties of that enumeration type. Only one of the 3 related elements is generated in the XML.
Thanks for your help.
As the OP discovered, when a WCF client (including proxy classes generated by the XSD.exe utility) imports an enumerator from the WSDL or XSD, the proxy also has a corresponding bool property. For an enumeration element named 'Foobar' there is also 'FoobarSpecified' which must be set to true or WCF will not serialize the data element.
This is still true 6 years after the question was asked, even when using the new WCF Client in .NET Core and Visual Studio 2017, and it isn't especially obvious, so I thought I'd promote the OP's comment response to wiki answer.
This is most likely occuring because WCF contracts treat enums differently. For data contracts you mark the class with a DataContract attribute and the members with a DataMember attribute. What's not as well known is that enums have their own attribute called EnumMember which WCF uses to serialize them properly.
There is an MSDN article called Enumeration Types in Data Contracts which goes over the usage in greater detail. The example code from the article shows how a contract with an enum should look:
[DataContract]
public class Car
{
[DataMember]
public string model;
[DataMember]
public CarConditionEnum condition;
}
[DataContract(Name = "CarCondition")]
public enum CarConditionEnum
{
[EnumMember]
New,
[EnumMember]
Used,
[EnumMember]
Rental,
Broken,
Stolen
}
Note that in their example (which I've included above) you can set just a subset of the enum values as part of the data contract if that's a requirement.
In addition to this any property that is not tagged with the DataMember attribute will not serialize over the wire. This should be the checklist to ensure that serialization works for WCF:
Check that classes are marked with the DataContract attribute.
Check that properties are marked with the DataMember attribute.
Check that the individual enum values are marked with the EnumMember attribute.

Adding [DataContract] to class with [Serializable] causes problems

I am using the DataContractSerializer to persist objects. Most classes have the [DataContract] attribute but one (not sure why) has [Serializable]. The properties of this class have the [DataMember] attribute. So far, it has worked ok.
Recently, I tried to add a new property but get an error when deserialising. The error message is something like: 'property Notes was expected but found property ModuleNr'.
I tried adding the [DataContract] attribute but got a different error like: 'Deserialised object with id 15 not found'. It seems that the class is not being deserialised at all.
Can anyone explain what I am doing wrong and what I can do to deserialise the existing objects.
Thanks.
Is it the case that the object that was persisted was using the older class structure, and the property you added is a required property, causing the deserialization to fail?
Also, if the Serializable property is there and you are not sure why its there, it might be a good idea to verify if some other code is not using this class to serialize - if it is, the new property you are adding might break it :)
Hope this helps!

Why can I not use the KnownType attribute in my WCF class?

I am using WCF to retrieve a collection of objects. The objects are all of type ProcedureText but may be of child classes SuspensionText or ResumptionText, both of which inherit from ProcedureText.
public class ProcedureText { }
public class SuspensionText : ProcedureText { }
public class ResumptionText : ProcedureText { }
My OperationContract specifies a method returning an array of ProcedureText objects:
[OperationContract]
[WebGet(UriTemplate = "procedureTexts")]
ProcedureText[] GetProcedureTexts();
This works if I cast all my objects to ProcedureText but I want to keep the distinction of using the sub-types. I had hoped to use the KnownType attribute to do this and had expected to be able to do it by adding it to my ProcedureText class:
[System.Runtime.Serialization.KnownType(typeof(SuspensionTextDto))]
[System.Runtime.Serialization.KnownType(typeof(ResumptionTextDto))]
public class ProcedureText { }
This doesn't work because the compiler cannot resolve System.Runtime.Serialization.KnownType. I know from the document that the attribute is part of .NET Framework 4, but I am using .NET Framework 4 and that is the Target Frameweork for my project.
Why do I need to set to be able to use the attribute?
The relevant DLL containing that type is not added by default. You need to add a reference to:
System.Runtime.Serialization
The usage is described in the documentation:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.knowntypeattribute.aspx
Basically it is so that the serializer recognises the type. Your service contract returns an array of the base class, however the types in that array could be more derived. If the serializer is not told this, the serialization will fail I think.
Without explicitly adding the reference to the project, I was able to use " I was able to use "using System.Runtime.Serialization" in my code but when I used attributes like "KnownType" or "DataMember" the compiler gave an error.
I was able to overcome this issue by explicitly adding the reference. Go to "Add References" in your project and search for "System.Runtime.Serialization" under Assemblies and add the dll to the project.
Works in .net 4.5 and 4.5.1 so I assume this will work in 4.0 as well.

DataContract attribute -WCF

Is DataContract attribute is deprecated in ASP.NET 4.0 WCF ? I can see only DataContractFormat attribute.
I can't apply DataContractFormat attribute over struct.
example
[DataContractFormat]
public struct Contact
{
public string firstName;
public string lastName;
}
It throws an error saying that DataContractFormat artribute can only be used on class, interface and methods.
No, the .NET 4 still contains the DataContractAttribute:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute%28v=VS.100%29.aspx
and it should be able to be applied to class or struct.
You have to add a reference to the System.Runtime.Serialization assembly (right click References, add ref...)
DataContract is not deprecated - where the heck did you get that idea from?
DataCOntractFORMAT is something totally different. I suggest you please read the documentation ;) Helps a lot. Will also explain what DataContractFormat is for.
http://msdn.microsoft.com/en-us/library/system.servicemodel.datacontractformatattribute.aspx
As you can see in the example this attribute goes on the class/interface that defines the SERVICE CONTRACT. It controls how for that service data serializaton is (guess what) formatted.