Consuming WCF Service with DataContract - wcf

I do have a simple WCF service in which If I put the method with simple Data Type then I can access that service in the MVC project which is in same Solution. But if I change the Data Type of the Service method even to array or list of string or any other simple Data Type, I cannot access the service. Do I need to make any config changes.
[DataContract]
public class Property
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string PropertyPost { get; set; }
[DataMember]
public string PropertyType { get; set; }
[DataMember]
public string DealType { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string ProjectName { get; set; }
}
I actually want to return the List from the WCF service for which I have created the Datacontract, but it is not working even with simple List Type.
Do we need to specify anything in Service like WebInvoke?
Can any one help?

Related

How to implement GET endpoint in ASP.NET Core which returns object with File?

I have the following class:
public class Person
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public byte[] PersonPhoto{ get; set; }
}
Usually I implement get objects endpoint, which returns not directly file but path to that file, and second endpoint to retrieve file separately.
Is that any solution which will let me implement it from one endpoint?
Is it a good practice?

how to create a multiple datacontract class using wcf services

I have create a simple user login WCF Services.I have using large number of datamember properties in one datacontract class.so i have split the datamember properties in different class. for example (Userdetails class with using userid,usertype,username,password,address,city,state,phoneno,mobileno,country,pincode,etc...)so i have split on the two Datacontract classes into userdetails as Userinfo and contactdetails class.The userinfo class only define the userdetails only and contactdetails class defined contact details as. how to use on the multiple datacontact class in WCF services can you please explain them.
I'm not sure what exactly you want. but i hope you could use something like this:
[DataContract]
public class User
{
[DataMember]
public userdetails Userinfo { set; get; }
[DataMember]
public contactdetails contactdetails { set; get; }
}
[DataContract]
public class userdetails
{
[DataMember]
public long userid { set; get; }
[DataMember]
public string usertype { set; get; }
}
[DataContract]
public class contactdetails
{
[DataMember]
public long mobileNumber { set; get; }
[DataMember]
public string phoneNumber { set; get; }
}

Interface segregation in WCF for JSON

I am a newbie in WCF. I was wondering if we can retrive properties from base interface in the REST output.
Please consider following structure. Product includes IVenueView not Venue. Is it possible to only have Venue.Id in Product JSON response?
[DataContract]
public class Product {
[DataMember]
public Guid? Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public IVenueView Venue { get; set; }
}
public interface IVenueView {
[DataMember]
Guid? Id { get; set; }
}
[DataContract]
public class Venue : IVenueView
{
[DataMember]
public Guid? Id { get; set; }
[DataMember]
public string Name { get; set; }
}
Data contracts are all about data - interfaces define behaviors, so they don't really mix up well. The data contract that you have likely will not work (because the serializer doesn't "know" about the Venue type (it only knows about IVenueView), it won't be able to serialize / deserialize instances of Product.
No it is not possible because serialization and deserialization works with the implementation (actual data) not with interfaces. Moreover for pure serialization you will have to use something like:
[DataContract]
[KnownType(typeof(Venue))]
public class Product
{
[DataMember]
public Guid? Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public IVenueView Venue { get; set; }
}

Validation of elements in a collection of a WCF data contract

We have a WCF service that uses Microsoft.Practices.EnterpriseLibrary.Validation and receives an object like so (simplified):
[DataMember]
[NotNullValidator]
public string Name { get; set; }
[DataMember]
public IList<Appointment> Appointments { get; set; }
The Appointment DataContract could look like:
[DataMember]
[NotNullValidator]
public string Description { get; set; }
Now the problem is that the validation of the Name property seems to work, but the Description isn't validated. So you can't pass a request with an empty Name, but you can pass a request with a Name and a list of Appointments with empty Descriptions.
Is it normal that WCF doesn't validate the elements of a collection in a DataContract?
Well, we solved it by adding SelfValidation:
[HasSelfValidation]
public class Client
{
[DataMember]
[NotNullValidator]
public string Name { get; set; }
[DataMember]
public IList<Appointment> Appointments { get; set; }
[SelfValidation]
{
foreach (var appointment in Appointments)
{
results.AddAllResults(Validation.Validate(appointment));
}
}
}

RIA DomainService not generating client code using DataContracts

I'm working on a Silverlight 4.0 application and am using RIA services. I have created a class on the server-side which has DataContract and DataMember attributes applied to it.
A DomainService exposes this class as a query result and as such, generates code for it on the client. But somehow it doesn't generate code for all properties of the class. Primitive properties of type guid, string, int, bool etc are generated just fine, but if I have a property of my own complex type, that property isn't created on the client.
Here's the class in question:
[DataContract]
[KnownType(typeof(SummaryGroup))]
public class SummaryDataSet
{
public SummaryDataSet()
{
}
[KeyAttribute]
[DataMember]
public Guid Guid { get; set; }
[DataMember]
public SummaryGroup SummaryGroup { get; set; }
}
The Guid property is created on the client just fine. The SummaryGroup property isn't created on the client. Here's the code for the SummaryGroup:
[DataContract]
public class SummaryGroup
{
public SummaryGroup()
{
}
[KeyAttribute]
[DataMember]
public Guid Guid { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int Sequence { get; set; }
}
Both classes are in the same namespace.
Question: why isn't the SummaryGroup property of SummaryDataSet created on the client and what should I do to fix this?
WCF RIA cannot handle complex types, but you could try this:
[DataContract]
[KnownType(typeof(SummaryDataSet))]
public class SummaryDataSet
{
public SummaryDataSet()
{ }
[KeyAttribute]
[DataMember]
public Guid Guid { get; set; }
[DataMember]
[Association("SummarySet_SummaryGrp_FK", "Guid", "Guid")]
public SummaryGroup SummaryGroup { get; set; }
}
This gives RIA the information to connect from the SummaryDataSet to the SummaryGroup.
This assumes that you can request both the SummaryDataSet and SummaryGroup from the serverside service.