Hide public Property when exposed through web service - c#

I would like to prevent a property from being exposed via my WCF web service. I tried adding the XmlIgnore attribute bug that didn't work. We are using .NET 3.5. WCF.
This doesn't work:
public class MyClass
{
public string S1 { get; set; }
[XmlIgnore]
public string S2NotExposed { get; set; }
}

Mark your class with the [DataContract] attribute from the DataContractAttribute Class , then mark only the properties you want to expose with the [DataMember] attribute from the DataMemberAttribute Class.

Related

How to fix XML serialization issues in ASMX web service

We have an asmx web service A which is consuming a WCF service B with below DataContract structure.
[DataContract]
public class Animal
{
[DataMember]
string AnimalName { get; set; }
[DataMember]
Head[] head { get; set; }
}
[DataContract]
public class Person
{
[DataMember]
string PersonName { get; set; }
[DataMember]
Head[] head { get; set; }
}
[DataContract]
public class Head
{
[DataMember]
string Title { get; set; }
}
We are receiving below error while running an asmx web service A.
Types 'Animal.Head' and 'Person.Head' both use the XML type name, 'Head', from namespace 'http://tempuri.org/'. Use XML attributes to specify a unique XML name and/or namespace for the type.
We also tried putting a different XMLElement namespace in DataContract but didn't help.
[XmlElement(Namespace = "urn:/A/Animal/")]
[DataMember]
Head[] head { get; set; }
Any help on this is much appreciated.
XmlElement is used for XmlSerializer, wcf by default uses DataContractSerializer to
serialize and deserialize model.
If you want to use XmlSerializer , you could refer to
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-the-xmlserializer-class
DataContract also has a Namespace property which could specify namespace for your model.

DataContractSerializer using default properties

Recently when I read the default behavior of DataContractSerializer, I get the rules from MSDN, however I do not understand the first rule which I extracted as below:
The DataContractSerializer infers a data contract from types without attributes using the default properties of the newly created types.
How do I interpret this statement, if some one has clear idea, could you help, I know that "without attributes", the attribute means DataContract attribute, however what does that "default properties" refer to. Is there something called "default properties" in a custom type?
If you a have type referenced within another class that has [DataContract] attribute, then DataContractSerializer will serialize the referenced type even if it is not attributed with [DataContract]. Serialization will happen on all public properties, unless the property is attributed with [IgnoreDataMember].
For example:
[DataContract]
public class ClassA
{
public ClassB MyData { get; set; }
public string SomeString { get; set; }
public int SomeNumber { get; set; }
}
public class ClassB
{
public string SomeOtherInfo { get; set; }
public int SomeOtherNumber { get; set; }
}
In the above code, ClassB will be serialized based on its default properties, which in this case are all the public properties: "SomeOtherInfo" and "SomeOtherNumber".

Using Datacontract: WCF

How should i be declaring the datacontracts
My Operation contract has a Method:
Apple GetApples()
My data Contract Apple looks Like
[DataContract]
public class Apple
{
[DataMember]
public int Id { get; set; }
[DataMember]
public FruitType type { get; set; }
}
As there is another member of type FruitType.
[DataContract]
public class FruitType
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string type { get; set; }
}
OR
as a simple class
public class FruitType
{
public int Id { get; set; }
public string type { get; set; }
}
What is the difference between these two? other than that the simple type is not a datacontract and will depende on how i want to use it.?
how should i declare it??
Those attributes give you the control over how your properties will be represented in different formats. For example for XML you can specify the XML Namespace and XML node names.
Even if you are happy with default property names and default namespace, when you try to serialize data to XML, your XML nodes will have weird names such as typek_BackingField.
In other words, if you use WCF you should use DataContract and DataMember attributes, even if you think it works fine the formatted data may not look what you expect. As a result it removes compatibility with other (non-WCF) systems. Or even when you don't share your types (contracts) with other WCF systems.

Can you use interfaces as DataMembers in WCF?

Can you do this?
[DataContract]
public class RegisterEndpointRequest : NotificationRegistrationServiceRequest
{
[DataMember]
public IEndpoint Endpoint { get; set; }
}
Notice the member Endpoint is an interface (IEndpoint), not a class. Will WCF allow this?
I think you can (but I haven't tested it), but you will then need to declare all implementations of that interface with [KnownType]:
[DataContract]
[KnownType(typeof(EndpointImplA))]
[KnownType(typeof(EndpointImplB))]
public class RegisterEndpointRequest : NotificationRegistrationServiceRequest
{
[DataMember]
public IEndpoint Endpoint { get; set; }
}
Each implementing class must have a [DataContract] attribute.
The DataContractAttribute in WCF is not intended for use on an interface: See the documentation here
The answer in this question may give you a better idea why.
Yes, of course you can have interface as DataMember inside DataContract..
Then you have to have specify all the interface implementations in the KnownType attributes...
[DataContract]
[KnownType(typeof(ActivityDC))]
[KnownType(typeof(StepDC))]
[KnownType(typeof(WaveDC))]
public class CampaignDC : AuditedEntityBaseDC
{
[DataMember]
public IList<IActivityDC> Activities { get; set; }

Set Json Attribute on existing class

I have an existing class in an external assembly which I can't change.
I would like to serialize an object from this class with Newtonsoft JSON.Net, but not all the properties.
Normally I can do this with the JsonIgnoreAttribute attribute like this:
public class TestJsonClass
{
public string PropA { get; set; }
[JsonIgnoreAttribute]
public string PropB { get; set; }
}
But since I can't change the class, is there a way to ignore a property without attributes?
Try inherit class and override property with appropriate annotations or copy property values in a completely new class.

Categories