Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wcf

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!

like image 689
Monsterlokomotivet Avatar asked Nov 01 '22 13:11

Monsterlokomotivet


1 Answers

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.

like image 135
Dead.Rabit Avatar answered Nov 15 '22 04:11

Dead.Rabit