Constructor in wcf

Can someone tell me how to create a Datacontract object in WCF. I have an application in which I need a constructor, but on the client side, when I create an object, it does not show the constructor. I know one solution, like add containg constructors for a partial class. The confusion here is "where to add a partial class"

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

+3
source share
5 answers

There are two ways to make the DataContract class a constructor. Probably the most consistent way is to transfer the DataContract to a separate class library that you specify in your service and client. As John Saunders stated, there is a way to tell Visual Studio to use an existing class when creating proxy code for the specified service. After that, you can simply add a constructor to this class.

If you want the constructor to be displayed only on the client side or for some other reason could not use the shared class library, you can create a partial class. For such a class, there really is no specific "location". All that is required to provide the constructor through a partial class is to create a new class file that defines the same class as your proxy.

, , - ReferencedServiceProxy.ContractClass, ,

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
       }
   }
}

, .

, , , , , "" .

http://mehranikoo.net/CS/archive/2007/11/09/DataContractConstructorsInWCF.aspx -, , . , , Silverlight, OnDeserialization.

+6

, , . " " .

+2

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);    
}

WCF

Scheduler.cs

TestPointSrvClient srv = new TestPointSrvClient();    
DataTable dtEvents = srv.GetMonthEvents(this.dtpMonth.Year,
    this.dtpMonth.Month);
0

? ( , ) , (, ). , ( ) ?

0

WSDL ( -) - DataContract. [DataMember] DataContract. , DataContract, , , .

If you really want to add constructors to the DataContract classes, you can open the Reference.cs file in your web link fold in your solution and find the class and change it. Please note: if you update your web link, the customized code will be overwritten.

Another solution, similar to your statement, created an incomplete class for the generated DataContract class. You will learn how to do this if you check the reference.cs file. (you need to click the icon of all files in VS to see reference.cs)

0
source

Source: https://habr.com/ru/post/1706448/