coder
coder

Reputation: 4283

access wcf interface method from client

This is one of the classes in Interface file.

[DataContract]
public class ClassX
{
    public ClassX()
    {
        ClassXParameters = new List<ClassXParameter>();
    }

    public void Add(string name, string value)
    {
        ClassXParameters.Add(new ClassXParameter() { Name = name, Value = value });
    }

    [DataMember]
    public List<ClassXParameter> ClassXParameters { get; set; }
}


[DataContract]
public class ClassXParameter
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Value { get; set; }
}

on the client I'm trying to do something like this

ClassX classx = new ClassX();
classx.Add("testname", "testvalue");

But this .Add method is not even visible.

currently I'm doing

ClassX classx = new ClassX();

List<ClassXParameter> params = new List<ClassXParameter()>;
params.add(new ClassXParameter() {Name="testname", Value="testvalue"});
classx.ClassXParameters = params;

Is there anyway I can do what I'm trying to do?

Note: I am not sure why some of the text above are in bold.

Upvotes: 2

Views: 160

Answers (2)

Anders Abel
Anders Abel

Reputation: 69260

If you autogenerate the client code from scratch, it will generate a new class, which contains those members and properties that are marked with DataContract.

If you have methods that you want available on the client, you can accomplish this by putting the DataContract types in an own assembly, which you reference from both the server and the client. When you generate the service reference you have to choose the option to reuse existing classes instead of generating new ones.

Often it is suitable to put data validation rules in the data contract classes property setters. Reusing the data contract assembly in the client will cause the data validation to occur directly on the client, without the need for a roundtrip. It also causes the error in a place where it is much easier to spot than if it is reported as deserialization error.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

Data Contracts are for data only. Any methods will not be visible on the client.

The bold was because of the "-----".

Upvotes: 0

Related Questions