Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing and retrieving a class with methods through WCF service

Tags:

.net

wcf

c#-4.0

I want to pass/retrieve an instance of the following class through a wcf service. The object should carry the methods that are defined by its class also. is it possible? suppose the following class:

[DataContract]
public class MyClass
{
    [DataMember]
    public string Name;

    public MyClass()
    {

    }

    public MyClass(string name)
    {
        this.Name = name;
    }

    public void SetName(string name)
    {
        this.Name = name;
    }

    public string GetName()
    {
        return this.Name;
    }
}

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    MyClass GetMyClassInstance();        
}

public class MyService:IMyService
{
    public MyClass GetMyClassInstance()
    {
        return new MyClass("hello");
    }
}

Now, when I add a reference to MyService in my client application project, the data contract MyClass will be generated, along with the service client, say MyServiceClient, so, and i do the following:

MyServiceClient client=new MyServiceClient();
MyClass myClass1= client.GetMyClassInstance();

But my real question is, after getting the result from the service, whether this is possible(?) :

myClass1.SetName("oops!!!");

while transmitting data contracts, will the methods in them also be transmitted? My Business objects contain methods too, and they need to be passed through WCF. Is there a way? Is passing BOs such as this through WCF a good practice? Thanks in advance!

like image 560
Anantha Avatar asked Dec 29 '25 22:12

Anantha


1 Answers

WCF exchanges XML (or JSON) documents. The method values marked with 'DataMember' will be in the document. No code within a method is serialized.

like image 124
lcryder Avatar answered Jan 01 '26 15:01

lcryder