Click here to Skip to main content
16,019,152 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
VS2012/.NET 4.0, WCF Service, Passing an Object to Service

I am trying to call an Add service operation from client to create a Region. Somehow, the client code is not accepting the Region object and when debugging through code I see the RegionID is displayed as zero although I assigned a value in client before the call to Add operation.

Client Code:
C#
RegionServiceClient objClient = new RegionServiceClient("BasicHttpBinding_IRegionService");

    RegionServiceProxy.RegionData objRegion = new RegionServiceProxy.RegionData();
    objRegion.RegionID = 5;
    objRegion.RegionDescription = "New Region"
    int newID = objClient.Add(objRegion);



Service Operation Code:
C#
public int Add(RegionData objRegion)
{
    // objRegion.RegionID shows up as 0
    // objRegion.RegionDescription shows up as "New Region"
}


Question:
- Why am I losing the value for RegionID?
- Am I missing something?

Here is my Data Contract:

C#
[DataContract]
    public class RegionData
    {
        [DataMember]
        public int RegionID = 0;
        [DataMember]
        public string RegionDescription = string.Empty;
    }



Please help!
Posted
Updated 10-Dec-20 1:29am
Comments
Suvabrata Roy 12-Dec-13 23:42pm    
I did not find anything wrong in your code.
to Find reason add another parameter ( DataMember ) with int type and Set some value to it let see what happen...
[no name] 13-Dec-13 13:02pm    
I found some articles that suggest that "the add service refernece generate some properties with the suffix .IsSpecified, when I set it to true, then the value is sent, otherwise it isnt.".

Here is the article:
http://social.msdn.microsoft.com/Forums/en-US/d5d446f3-8757-4a75-aac3-e55eaca3d28f/wcf-service-losing-values-of-properties?forum=wcf
and
http://msdn.microsoft.com/en-us/library/ms733127.aspx

I did find RegionIdSpecified property in my service reference, but this is tedious if I have to set this for all int and other datatype properties, other than string.

I have also tried adding [Serialization] attribute to Data Contract class along side [DataContract] attribute and see that RegionIDSpecified property was gone and I am able to pass the object. But, all other operations of the service got disturbed and I have to pass some objects to it. i.e. GetList() method now asks for GetListRequest object as parameter to that operation.

I haven't got any easier/quick solution yet.

Member 10434230 13-Dec-13 6:55am    
All seems to be fine with your code...
Can you follow the suggestion of "Suvabrata" and post the results
[no name] 13-Dec-13 14:18pm    
Same result...When I added new field (NewRegionID) with [DataMember], I see two properties added in intelliscense - NewRegionID and NewRegionIDSpecified. When I debug the code into service method, NewRegionID value is showing up as 0, instead of 5.

I created a sample app as per code and all works fine, check below details:

[ServiceContract]
public interface IService1
{
[OperationContract]
int Add(RegionData objRegion);
}

[DataContract]
public class RegionData
{
[DataMember]
public int RegionID = 0;
[DataMember]
public string RegionDescription = string.Empty;
}

public class Service1 : IService1
{
public int Add(RegionData objRegion)
{
// objRegion.RegionID shows up as 0
// objRegion.RegionDescription shows up as "New Region"

return objRegion.RegionID;
}
}


private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client cl = new ServiceReference1.Service1Client();
ServiceReference1.RegionData data = new ServiceReference1.RegionData();

data.RegionID = 20; // Set whatever int value you want
data.RegionDescription = "Testing descriptions";


int abc = cl.Add(data); //the output comes out to be above int value i.e. 20.
}




Review your code and find out what is going wrong.
 
Share this answer
 
Comments
[no name] 13-Dec-13 17:15pm    
Thanks for your response and help! Yes, I have checked my code again and again.

FYI, I have tried creating WCF service library component as well as creating service host file .svc within IIS, and in both cases if I don't provide IsRequired to true then it shows as zero value inside service operation, regardless of the value i pass.
Here is an update, based on following article:
http://stackoverflow.com/questions/2046206/how-to-prevent-specified-properties-being-generated-in-wcf-clients[^]

When I add following attribute instead of just [DataMember], I was able to see the value in my service operation - Add(objRegion) and data is being added into the database.

[DataMember(IsRequired=true)]

Still on research why we need IsRequired on all the fields of Data Contract to true, but if we don't add or set IsRequired to false, then you have Specified properties generated for all fields of Data Contract except string fields and the value shows up as zero in service method if you do not set Spefified properties to be true for those fields.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Jun-14 15:33pm    
How could you consider this as an answer? And even accept it formally? This is considered as abuse. Use comments and/or "Improve question" instead.
—SA
RegionServiceProxy.RegionData objRegion = new RegionServiceProxy.RegionData();
objRegion.RegionID = 5;
objRegion.RegionIDSpecified = true
objRegion.RegionDescription = "New Region"
int newID = objClient.Add(objRegion);
 
Share this answer
 
v2
Comments
Richard Deeming 10-Dec-20 8:54am    
An unformatted, unexplained code dump, which references a property that doesn't exist in the OP's code, is not a "solution" to this already-solved question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900