tags:

views:

1052

answers:

5

Can any one just tell me how to create a object of Datacontract in WCF.I have a application in which i need a constructor but at client side when i create a object ,it doesnt shows a constructor.I know one solution like add a partial class containg constructors.the confusion is here "where to add a partial class"

Please someone help me ........I am new to C# and .net.

A: 

In My WCF

ITestPointSrv.cs

[ServiceContract]    
public interface ITestPointSrv    
{    
    [OperationContract]
    DataTable GetMonthEvents(Int32 p_nYear, Int32 p_nMonth);
}

TestPointSrv.svc.cs

public DataTable GetMonthEvents(Int32 p_nYear, Int32 p_nMonth)    
{    
    return (new OccasionType()).GetMonthEvents(p_nYear, p_nMonth);    
}

In My WCF

Scheduler.cs

TestPointSrvClient srv = new TestPointSrvClient();    
DataTable dtEvents = srv.GetMonthEvents(this.dtpMonth.Year,
    this.dtpMonth.Month);
Adnan Badar
A: 

In the generated code, what constructor does it have? If a (non-abstract, non-static) class doesn't have any explicit constructor, it gets a default constructor (public, parameterless). So what code are you seeing (for this object) in the generated code?

Marc Gravell
A: 

The WSDL ( web service description language ) of your web service does not tell the constructor info of DataContract. Serialization only applies to [DataMember] fields within your DataContract classes. Therefore, the generated client does not include any methods or constructors of your DataContract, it only has a parameterless default constructor of course.

If you really want to add constructors to DataContract classes, you could open Reference.cs file within your web reference fold in your solution, and find the class and modify it. Note, if you update your web reference, the customized code will be overwritten.

Another solution like your saying, created a partial class for the generated DataContract class. You will know how to do this if you inspect the reference.cs file. ( you need click show all file icon in VS in order to see the reference.cs)

codemeit
+1  A: 

In order to do what you want, you must share the data contract type between the client and the service. There is an option for that in the "Add Service Reference" dialog.

John Saunders
+1  A: 

There are two ways to accomplish giving a DataContract class a constructor. Probably the most consistent way is to move the DataContract into a separate class library which you reference in your service and client. As stated by John Saunders there is a way to tell Visual Studio to use an existing class when generating the proxy code for the referenced service. After this you can just add the constructor to that class normally.

If you want the constructor to only appear on the client side or for some other reason are unable to use a shared class library, you can create a partial class. There is really no defined 'location' for such a class. All that is required for providing the constructor through a partial class is to create a new class file which defines the same class as your proxy.

So assuming you have a proxy class ReferencedServiceProxy.ContractClass you need to create a new code file which defines the partial class

namespace ReferencedServiceProxy
{
   partial class ContractClass
   {
       // Constructor. Naturally the constructor cannot overwrite one
       // defined in the proxy class already. Not sure if those define
       // a default constructor.
       public ContractClass()
       {
           // Implementation
       }
   }
}

As long as the file is compiled normally, the compiler does all the rest.

However, without knowing the need for a constructor, it's probably worth pointing out that a constructor is not called when deserializing the object on client-side as the object is not 'constructed' as such.

http://mehranikoo.net/CS/archive/2007/11/09/DataContractConstructorsInWCF.aspx has something related to what happens during deserialization and how to affect it. Just something which I learned the hard way.. with silverlight which doesn't even support the OnDeserialization attribute.

Mikko Rantanen