How to inherit three data contracts in a single object? - wcf

How to invoke three data contract objects and create it in a single object?
I have a data contract class like this
[Serializable]
[DataContract]
public class Address
{
[DataMember]
public long AddressId { get; set; }
}
another data contract class like
[Serializable]
[DataContract]
public class Email
{
[DataMember]
public long EmailId { get; set; }
}
another data contract class like
[Serializable]
[DataContract]
public class Phone
{
[DataMember]
public long PhoneId { get; set; }
}
Now i want to use the AddressId, EmailId, PhoneId in the same method.
How it is possible??

Please, keep the [DataContract] attrubute only, you don't need decorating with Serializable as well.
Well, one have the following options with WCF Data Contracts:
Composite Data Contracts.
Member fields of any class marked as DataMember can be data contracts themselves, once they're decorated with DataContract attribute too. Aggregation of all nested data contracts illustrates the fact that data contracts are recursive in nature. WCF detects all the data contract enabled properties in the object graph and captures their state as well.
[DataContract]
class Address
{
[DataMember]
public long AddressId { get; set; }
}
// The same for the rest two, and then an aggregating type.
[DataContract]
class Contact
{
[DataMember]
public Address Address {get;set;} // here we go
[DataMember]
public Email Email {get;set;}
[DataMember]
public Phone Phone {get;set;}
}
Data Contract Hierarchy
Your data contract class may be a subclass of another data contract class, here you just have to explicitly opt-in for a given data contract, i.e. specify the DataContract on each type in the hierarchy tree.
[DataContract]
class ContactDetails
{
[DataMember]
public long AddressId { get; set; }
// you could move the phone and email details here too.
}
[DataContract]
class Contact : ContactDetails
{
[DataMember]
public string Name { get; set; }
}
You can't have three separate classes for each one and inherit from them at once in .Net. And my suggestion is the first case for you - that is data contract aggregation.
Bonus: Polymorphic Type Reference.
Applying the [KnownType(Type type)] attribute on a base type for passing polymorphic objects as operation contract arguments. This is definately not your case.

Contracts applied to classes to provide service metadata for your service (service class just can use decorated classes as parameter types in service methods). So - if you want to compose some type (class) from existing properties - this is not related to WCF contracts.

Related

Using the same class as both Code First Entity Model and WCF Data Contract

I am using the following DataContract for transferring over the WCF Service:
[DataContract]
public class User
{
[DataMember]
public int UserId { get; set; }
[StringLength(255, MinimumLength = 3)]
[DataMember]
public string UserName { get; set; }
}
When the Service Operation transfers this object, the connection to the client is forcibly closed.
But, when I remove the StringLength attribute from the above contract, it works. This StringLength attribute is present because this same class is acting as an entity class (POCO class) for the Entity Framework Code First Model.
How can I resolve this other than defining the string length restriction in the Fluent API?
please follow these links maybe fix your problem:
wcfdataannotations
stackoverflow.com/questions/20430185/data-annotation-attributes-are-not-firing-in-wcf

How to ignore a DataMember from the superclass in a subclass

The title is pretty self-explanatory.
I have a base WCF DataContract, let's call it the PersonContract, which covers all fields of the Person entity in my database.
I have a number of client applications that call the same service through endpoints of different interfaces implemented by that service. This is because (amongst other differences) I want every of those applications to be able to access and edit only a specific subset of the Person entity.
Now if I want to define a contract with all the properties of PersonContract except one, can I subclass PersonContract and ignore a single property in the subclass?
Or is my only option building contracts from the smallest subset (but I doubt I can fully avoid repeating code then)?
Out of curiosity I did a couple tests and it doesn't look like it'll work.
Here are the data contracts I used:
[DataContract]
public class Person
{
[DataMember]
public virtual string FirstName { get; set; }
[DataMember]
public virtual string MidName { get; set; }
[DataMember]
public virtual string LastName { get; set; }
}
[DataContract]
public class Person2 : Person
{
[IgnoreDataMember]
public override string MidName { get; set; }
}
And my service contract:
public interface IService1
{
[OperationContract]
Person GetPerson();
[OperationContract]
Person2 GetPerson2();
}
Both operations return the same result.
Another way that you might be able to produce the results you're looking for could be to define your minimal contract (the one with missing the excluded properties) and inherit from it adding the field needed by the other operation.
The equivalent data contracts would look something like:
[DataContract]
public class Person2 : Person
{
[DataMember]
public virtual string MidName { get; set; }
}
[DataContract]
public class Person
{
[DataMember]
public virtual string FirstName { get; set; }
[DataMember]
public virtual string LastName { get; set; }
}
And I've verified that the results are as I would expect.

Send a list with appointments through WCF

I would like to send a list of Appointments through WCF. My Interface looks like this:
[ServiceContract]
public interface IServices
{
[OperationContract]
string addAppointments(List<Appointment> appointmentList);
}
If I call my WCF Service I'm always getting the following error:
Type 'Microsoft.Exchange.WebServices.Data.Appointment' cannot be
serialized. Consider marking it with the DataContractAttribute
attribute, and marking all of its members you want serialized with the
DataMemberAttribute attribute. See the Microsoft .NET Framework
documentation for other supported types.
My Service currently looks like this:
class Service : IServices
{
public string addAppointments(List<Appointment> appointmentList)
{
foreach (Appointment app in appointmentList)
{
Console.WriteLine(app.Organizer.Name);
}
return "true";
}
}
It's not your service that's at fault, it's the class your passing, Appointment.
Start by adding [DataContract] to your class. then [DataMember] to each of the properties you'd like to pass.
For example, if you started with:
public class Appointment{
public DateTime Date { get; set; }
public string Name { get; set; }
}
You can make it serializable by WCF's DataContractSerializer by adding those attributes:
[DataContract]
public class Appointment{
[DataMember]
public DateTime Date { get; set; }
[DataMember]
public string Name { get; set; }
}

Interfaces in contract on client

I have such a contract:
[DataContract]
[KnownType(typeof(Person))]
public class Gadget
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public IPerson Person { get; set; }
}
It represents a gadget that belongs to a person. I just came up with this simple example, it's not important whether it makes sense or not.
So, instead of returning the Person class, I return the IPerson interface. Now the client can no longer generate a strong typed object, but will generate this:
public object Person { get; set; }
Now my question is: is it possible to let the client also generate the IPerson interface? It should have enough information, because it can only instantiate Person (only known type).
Interfaces will not be transfered by adding a service reference. These interfaces only exist in .NET, but your service is suppossed to be interoperable.
As far as your WSDL is concerned there is likely to be no way to tell Person and IPerson apart.
If you really want to use that interface you will need to move it across manually. This means editing the generated client code by hand.

Can I use DataContract and Serializable together?

I am working on WCF service. My all class are already serialize using [Serializable] attribute but due to "k__BackingField" Property Naming problem I used DataContract and DataMember attribute.
so Can i use both attribute together like following:
[Serializable]
[DataContract]
public class User
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int UserID { get; set; }
}
is this correct?
I also got similar solution here.
C# automatic property deserialization of JSON
Serializable and DataContract (not versus?)
I found an article on MSDN according to this we can use both attribute DataContract and Serializable together.
With [Serializable], all fields become part of the data contract (unless they are marked with [NonSerialized]). With [DataContract], only members marked with [DataMember] are included. Note that if a type has both [DataContract] and [Serializable] attributes on it, it will use the [DataContract] mapping
http://msdn.microsoft.com/en-us/magazine/cc163569.aspx
if the problem is in naming why don't you use
[XmlElement(ElementName = "Name")]
public string Name { get; set; }