Monsterlokomotivet
Monsterlokomotivet

Reputation: 91

(WCF - C#) Return custom class containing collection of different custom class

I've been roaming around the internet for an answer to my question but have yet to find one.

Here's the scenario: I have a WCF library service that works against a database containing a few configuration of mine. These configurations are made up of a few custom classes;

[DataContract]
public class Config : Object
{
    [DataMember]
    public int AppId { get; set; }

    [DataMember]
    public int VersionId { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public DateTime DateCreated { get; set; }

    [DataMember]
    private List<ParameterRow> ParameterRows = null;


[DataContract]
public class ParameterRow : Object
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Id { get; set; }

    [DataMember]
    private List<ParameterItem> parameterItems = null;
}

[DataContract]
public class ParameterItem : Object
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Value { get; set; }

    [DataMember]
    public string DataType { get; set; }
}

There you can see how it's all related. I also included a method-head just to show you an example of the simple functions. I have my current code working fine with the current functions (including the DB). I also have a WCF library service that retrieves configurations from the database and gives out to the client (when they call the service). Below is the function-head the clients call to the service.

public Config GetConfig(int id);

However, I cannot figure out how to get a whole configuration to the client in one call and still be able to use the methods of the Config-class. I know how to use datamembers and get properties to the client, but not collections nor methods to use. I have access to the config-class on both sides, I've just run out of ideas at the moment.

Suggestions/tips anyone? Greatly appreciated!

EDIT: This is the GetConfig-method in the service:

public Config GetConfig(int id)
    {
        dbHandler = new DatabaseHandler(new StoredProceduresFake());
        resultConfig = dbHandler.GetLatestConfiguration(id);
        return resultConfig;
    }

And here is the clients side of the call(I just created a console-app to test the service):

resultConfig = client.GetConfig(1);

But in the client-function I now get errors on all the config-properties. In the client V-Studio want me to write Config.Name = Namek_BackingField Whereas the service only need to write ConfigName. And I can't access the list of parameterrows, thus rendering the whole config useless to me. My guess is that the rest of the data is in the Config.ExtensionData, but I don't know how to access that.

FINAL EDIT; This has been resolved. As mentioned below only properties were sent over the network from the WCF-service. To solve this I created another class which extracted data from the class "Config" received from the service, stored it as wanted and also gave me the possibility to get my own methods. It was a simple solution of parsing the data. (In a way heh). Thanks for all the help, greatly appreciated!

Upvotes: 5

Views: 3931

Answers (3)

Adarsh Kumar
Adarsh Kumar

Reputation: 1150

If I have understood your problem. You mean,
1. Get all the config in one call.
2. You are able to get the Config result object along with its properties but you are not able to call the methods of resulted Config object.
If so then I would suggest below things:

  • Create a service contract as

    public List<Config> GetConfig(int id); //If id = -1 then return all config at once otherwise single matched config

  • Use [DataContract] for classes and [DataMember] for properties instead of [Serializable]

  • Instead of generating the service contract(classes and interfaces) using some tool, manually copy them to the client application. It will ensure that the methods have been imported in data contract class.

  • Import all the server side databases, configuration files etc. iff these are required in data contract methods which you want to be able to invoke.

Upvotes: 0

Dead.Rabit
Dead.Rabit

Reputation: 1975

Only your properties will be sent across the wire when you subscribe to a WCF service. The way I work around this problem is to create a separate class library to contain data contracts, this class library can then be referenced by both the client and the service so they share implementations for the methods (which wouldn't normally be shared with a SOAP WSDL binding).

When you create a service reference to the WCF service in your client application you can choose to reuse types from the shared class library instead of building the client side interfaces using the WSDL so method implementations are maintained.

Something you'll run into quite quickly is it's sometimes helpful to have functions that are only relevant in either the server or the client (i.e: a Validate( .. ) function might only be relevant to the client wheras a GetData( .. ) function might only be neccesary on the server. In these cases you should use Extension methods or inherit from the data contract to redefine it.

Upvotes: 2

Ahmed ilyas
Ahmed ilyas

Reputation: 5822

This may help - take a look at the OnDeserialized attribute: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

It seems you want to invoke the method to set all the properties based upon data from the DB (i.e the class is used to represent config settings but from the DB or something like that?)

instead, your service layer should be the one doing this THEN returning that object back after all settings have been set in the class. The class itself should not really be the one doing the logic/magic but rather the WCF Service Method should be doing it and returning the object back

Upvotes: 0

Related Questions