S
Steve Barker
WCF Question
Hi guys,
I’m pretty new to WCF, and have a basic question about how it works. I’m
trying to use WCF to write an “SOA-systemâ€, and am having a few problems.
The following analogy explains what I’m trying to do. I have a very simple
class:
[DataContract]
public class MyClass
{
private string data = "default data";
[DataMember]
public string Data
{
get
{
return data;
}
set
{
data = value;
}
}
}
This is exposed via a WCF Service:
[ServiceContract]
public interface IMyClassService
{
[OperationContract]
void Add(MyClass item);
}
public class MyClassService : IMyClassService
{
public void Add(MyClass item)
{
//Do something with the data...
Debug.WriteLine(item.Data);
}
}
As you can see, this is extremely simple. I’ve put the service in an ASP.NET
web application, created the correct web-config entries, included an svc file
with the correct tag, and the service does work. I’ve consumed the service
via a console application (by creating a service reference). I have the
following (very simple) code in the console application:
public static class Program
{
public static void Main(string[] args)
{
MyClassServiceClient client = new MyClassServiceClient();
MyClass myClass = new MyClass();
client.Add(myClass);
}
}
Obviously, the MyClass reference in the console application is a proxy class
generated automatically by WCF. It only knows about the properties marked
with [DataMember] attributes. This means that any “private†code within
MyClass is not executed by the console application. However, when the proxy
is passed to the service, I would expect an instance of the “real†or full
MyClass class to be instantiated on the WCF server, and I would expect the
private code to run on construction. It doesn’t! This can be proved by
looking at the value of the Data property on the server. It is still set as
null, despite giving it a value in the field declaration.
Hence, if MyClass has a constructor with code, this would not fire on the
server.
So, my questions is this: Is there a way to ensure that MyClass is
constructed fully on the server? It would be great if constructor code could
be fired. Can this behaviour be obtained by tweaking the attributes in the
service classes?
Thanks in advance,
Steve.
Hi guys,
I’m pretty new to WCF, and have a basic question about how it works. I’m
trying to use WCF to write an “SOA-systemâ€, and am having a few problems.
The following analogy explains what I’m trying to do. I have a very simple
class:
[DataContract]
public class MyClass
{
private string data = "default data";
[DataMember]
public string Data
{
get
{
return data;
}
set
{
data = value;
}
}
}
This is exposed via a WCF Service:
[ServiceContract]
public interface IMyClassService
{
[OperationContract]
void Add(MyClass item);
}
public class MyClassService : IMyClassService
{
public void Add(MyClass item)
{
//Do something with the data...
Debug.WriteLine(item.Data);
}
}
As you can see, this is extremely simple. I’ve put the service in an ASP.NET
web application, created the correct web-config entries, included an svc file
with the correct tag, and the service does work. I’ve consumed the service
via a console application (by creating a service reference). I have the
following (very simple) code in the console application:
public static class Program
{
public static void Main(string[] args)
{
MyClassServiceClient client = new MyClassServiceClient();
MyClass myClass = new MyClass();
client.Add(myClass);
}
}
Obviously, the MyClass reference in the console application is a proxy class
generated automatically by WCF. It only knows about the properties marked
with [DataMember] attributes. This means that any “private†code within
MyClass is not executed by the console application. However, when the proxy
is passed to the service, I would expect an instance of the “real†or full
MyClass class to be instantiated on the WCF server, and I would expect the
private code to run on construction. It doesn’t! This can be proved by
looking at the value of the Data property on the server. It is still set as
null, despite giving it a value in the field declaration.
Hence, if MyClass has a constructor with code, this would not fire on the
server.
So, my questions is this: Is there a way to ensure that MyClass is
constructed fully on the server? It would be great if constructor code could
be fired. Can this behaviour be obtained by tweaking the attributes in the
service classes?
Thanks in advance,
Steve.