2

On my client side, I want the default C# constructors from my WCF service C# classes. There reason we want to do this is to have the default values from the server side classes on the client side. From the searches I've done, there are 2 ways to go about this:

  1. When I need a default instance of the object I make a server call that returns me the default instance
  2. I create a client side C# class which inherits from the server side C# class and copy paste the constructor and use this constructor

Is either one of these the "better" option? or is this heavily dependent on how often I would need a new instance of the object?

EDIT: We tried using [System.Runtime.Serialization.OnDeserialized] seen here but it's not working. We are also not using the DataContract attribute in our class, we use ServiceKnownType instead

philr
  • 1,860
  • 1
  • 21
  • 31
  • 1
    With this approach any client of your WCF will depend on the "proxy" classes provided by you. They cannot use WCF independently. Another point of view - having some logic in the constructor not a good idea - maybe you can redesign your classes? – Fabio Nov 28 '16 at 22:12
  • @Fabio we have default values for our classes that we want to keep. We tried using `[System.Runtime.Serialization.OnDeserialized]` [here](https://stackoverflow.com/questions/8566204/datacontract-default-datamember-value) but it's not working. We are not using `DataContract` attribute in our class, we use `ServiceKnownType` instead – philr Nov 29 '16 at 15:25

1 Answers1

0

I think you can try using OnDeserializing attribute

[System.Runtime.Serialization.OnDeserializing]
private void OnDeserializing(StreamingContext ctx)
{
   // Here set your default values
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • I tried to use this too but it didn't work.. I think it's because we're using `[ServiceKnownType]` – philr Nov 29 '16 at 16:07