I would like to prevent my clients from instantiating entities through the constructor.
As there’s quite a bit of pluming involved to create those entities (initialise states, collections etc…), and WCF doesn’t work that way, I would like instead to force them to call a method on the server side that will create the entity and send it over the wire, something like
Client side:
var client = EntityServiceClient("myEndpoint");
var newEntity = client.CreateEntity();
Server side:
public Entity CreateEntity()
{
return new Entity();
}
I have something working, but I would like to somehow throw an exception when the default constructor of the entity is used, or make it private. So the following should not work
Client side:
var client = EntityServiceClient("myEndpoint");
var newEntity = new Entity();
Is it something possible?
It’s not really how WCF is designed to work, the serialised objects as far as the client are concerned only contain data not behaviour so you should send back DTO’s not entities.
However if you insist on going with this approach which will most likely cause you trouble in the future, you can move your objects & service interface into a separate class library and distribute it instead of having clients generate the code from meta data when adding a service reference.