ispiro
ispiro

Reputation: 27633

How do I pass a custom class to a WCF service?

I have to pass several arguments to a WCF service so I created a class with the appropriate fields and thought I'd pass an instance of that class to the service. But the service sees that class as different than the one it has.

So I thought of creating that class in the service, but I found that in the calling program

Class1 c = new Class1 ();//of the class in the service
FieldInfo[] f = c.GetType().GetFields();

Returns nothing.

So I tried making an interface, but the service still wants its class from the service.

So how can I pass a custom class instance and still be able to use that class in the caller like any other class?

EDIT:

This is what I'm doing: I created a website (just the simple template) in VWD (-Visual Studio for web). And added a WCF service and a file with

[DataContract]
[Serializable()]
public class Class1
{
    [DataMember]
    public string s = "";
}

EDIT: OK. For some reason the fields are converted to properties.

Thank you all!

Upvotes: 1

Views: 11190

Answers (3)

danish
danish

Reputation: 5600

You are looking for DataContract here. The class which you want to use in your OperationContract, must be marked as DataContract for it can be serialized.

Edit: If you have added the reference to the service in your web application, you should be able to find the class in Reference.cs file.

Upvotes: 2

Artur Udod
Artur Udod

Reputation: 4743

I am not sure that i understood your question. Looks like you want to pass a custom class FROM wcf service (please, take at look at http://msdn.microsoft.com/en-us/library/ms733127.aspx). I don't get, how do want to pass something TO WCF service, unless it consumes another service.

Edited: You cannot use classes of the client as parameters to method of the service.

Upvotes: 2

Maximc
Maximc

Reputation: 1762

Here is an example of a Datacontract:

    [DataContract]
public class Person
{
    [DataMember]
    public int id { get; set; }

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

Then you can initiate it from your WCF client or pass it as an argument in a method or let it return from a method(function).

Upvotes: 3

Related Questions