I'm a little confused. I have an abstract class that serves as the base class for several types of employee derivatives. The base class includes several virtual methods which return name info in various formats. Here's a simple example...
[Serializable]
[DataContract]
public abstract class FooBase
{
[DataMember]
public string FirstName {get; set;}
[DataMember]
public string LastName {get; set;}
public virtual string GetLastNameFirstName()
{
return String.Format("{0}, {1}", LastName, FirstName);
}
}
I then have derived classes in the WCF service which include the base class as well as whatever else:
public class MyFoo : FooBase
{
public string DoSomethingElse()
{
return "I did something else.";
}
I created a simple test method in the WCF Service that returns a new, populated MyFoo object.
So then I have a web client that has a service reference which exposes the method - looks good so far... The problem is, I cannot get to the base class' method for GetLastNameFirstName() on the client side. Here's an example that doesn't work... the method is not available:
using (ServiceClient proxy = new ServiceClient())
{
MyFoo newFoo = proxy.GetNewFoo(); <=== Returns a new MyFoo object from the service.
Response.Write(newFoo.GetLastNameFirstName()); <=== I cannot get to this method, but I can get to the other public props no prob.
}
So... what am I missing? Does the derived class have to override the virtual methods in the base class so they are available to the client app? Seems like base class implementation doesn't buy you much in this scenario... How should I set this up?
I want to be able to get to base class methods without having to write redundant code in derivative classes...
from the client code you can only call method defined as Service contract. you need to define the method in the your service which will take input as 'MyFoo" and call GetLastNameFirstName method.
Somnath Mali
.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
I'm a little confused. I have an abstract class that serves as the base class for several types of employee derivatives. The base class includes several virtual methods which return name info in various formats. Here's a simple example...
[Serializable]
[DataContract]
public abstract class FooBase
{
[DataMember]
public string FirstName {get; set;}
[DataMember]
public string LastName {get; set;}
public virtual string GetLastNameFirstName()
{
return String.Format("{0}, {1}", LastName, FirstName);
}
}
If you want to access GetLastNameFirstName() , you need to create this method in your ServiceContract not DataContract and decorate it with [OperationContract] attribute
Yeah, I understand how to do that. My question is more around the embedded methods of base class. This base class is used to create derivative classes which are sent from the WCF service methods. I can't get to the embedded methods of the base class on the
client side.
Class example:
MyEmployee : EmployeeBase EmployeeBase includes properties for FirstName and LastName, and a virtual method for GetLastNameFirstName() which returns a formatted version of the two properties.Note: MyEmployee is my derived object, using the EmployeeBase class which contains
properties and virtual methods that have implementation.
WCF method example:
public MyEmployee GetEmployee(string foo) {
MyEmployee emp = BLL.GetEmployee(foo);
return emp;
} This gets the data and creates a new MyEmployee object which is returned to the calling client.
Consuming client example:
protected void Button_Click (sender, e) {
using (proxy...)
{
MyEmployee sEmp = proxy.GetEmployee(123); Get a MyEmployee object from the service. This "should" have everything in it.
Response.Write(sEmp.GetLastNameFirstName()); This virtual method which in the base class isn't available to the client...
}
}
Is this possible with standard WCF services? Or do we have to look at remoting or something?
As nijhawan.sau mentioned, if you want to access GetLastNameFirstName() method from WCF client, you need to move this method to your ServiceContract and decorate it with [OperationContract] attribute.
"Unless you mark your method with [OperationContract] attribute, it will not be available to client, because while creating proxy only those methods will be exposed which have that specific attribute and inside a service, others will be ignored".
I understand the Operation Contract, etc. After more research, I guess what I was looking to do would be more
remoting than standard WCF service functionality.
kewlpack
0 Points
7 Posts
Can't access abstract base class' virtual methods in client of WCF service
Jun 20, 2012 10:08 PM|LINK
I'm a little confused. I have an abstract class that serves as the base class for several types of employee derivatives. The base class includes several virtual methods which return name info in various formats. Here's a simple example...
[Serializable] [DataContract] public abstract class FooBase { [DataMember] public string FirstName {get; set;} [DataMember] public string LastName {get; set;} public virtual string GetLastNameFirstName() { return String.Format("{0}, {1}", LastName, FirstName); } }I then have derived classes in the WCF service which include the base class as well as whatever else:
public class MyFoo : FooBase { public string DoSomethingElse() { return "I did something else."; }I created a simple test method in the WCF Service that returns a new, populated MyFoo object.
So then I have a web client that has a service reference which exposes the method - looks good so far... The problem is, I cannot get to the base class' method for GetLastNameFirstName() on the client side. Here's an example that doesn't work... the method is not available:
using (ServiceClient proxy = new ServiceClient()) { MyFoo newFoo = proxy.GetNewFoo(); <=== Returns a new MyFoo object from the service. Response.Write(newFoo.GetLastNameFirstName()); <=== I cannot get to this method, but I can get to the other public props no prob. }So... what am I missing? Does the derived class have to override the virtual methods in the base class so they are available to the client app? Seems like base class implementation doesn't buy you much in this scenario... How should I set this up? I want to be able to get to base class methods without having to write redundant code in derivative classes...
Thanks in advance!
A
somnathmali
Contributor
2816 Points
450 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 21, 2012 05:07 AM|LINK
from the client code you can only call method defined as Service contract. you need to define the method in the your service which will take input as 'MyFoo" and call GetLastNameFirstName method.
.NET Developer , Pune INDIA.
Please Mark As Answer If my reply helped you.
shanmugamm
Participant
1620 Points
321 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 21, 2012 05:11 AM|LINK
U need include the KnownType Attribute in to your abstract class
[Serializable] [DataContract] [KnownType(typeof(MyFoo))] public abstract class FooBase { [DataMember] public string FirstName {get; set;} [DataMember] public string LastName {get; set;} public virtual string GetLastNameFirstName() { return String.Format("{0}, {1}", LastName, FirstName); } }Also in the derived class
[DataContract] public class MyFoo : FooBase { public string DoSomethingElse() { return "I did something else."; }http://shanmugam-netguru.blogspot.com
Follow me in Linkedin
nijhawan.sau...
All-Star
16460 Points
3178 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 21, 2012 06:25 AM|LINK
If you want to access GetLastNameFirstName() , you need to create this method in your ServiceContract not DataContract and decorate it with [OperationContract] attribute
kewlpack
0 Points
7 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 21, 2012 08:34 PM|LINK
Yeah, I understand how to do that. My question is more around the embedded methods of base class. This base class is used to create derivative classes which are sent from the WCF service methods. I can't get to the embedded methods of the base class on the client side.
Class example:
MyEmployee : EmployeeBase
EmployeeBase includes properties for FirstName and LastName, and a virtual method for GetLastNameFirstName() which returns a formatted version of the two properties. Note: MyEmployee is my derived object, using the EmployeeBase class which contains properties and virtual methods that have implementation.
WCF method example:
public MyEmployee GetEmployee(string foo) {
MyEmployee emp = BLL.GetEmployee(foo);
return emp;
}
This gets the data and creates a new MyEmployee object which is returned to the calling client.
Consuming client example:
protected void Button_Click (sender, e) {
using (proxy...)
{
MyEmployee sEmp = proxy.GetEmployee(123); Get a MyEmployee object from the service. This "should" have everything in it.
Response.Write(sEmp.GetLastNameFirstName()); This virtual method which in the base class isn't available to the client...
}
}
Is this possible with standard WCF services? Or do we have to look at remoting or something?
Peter pi - M...
Star
12871 Points
1786 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 25, 2012 03:36 AM|LINK
As nijhawan.sau mentioned, if you want to access GetLastNameFirstName() method from WCF client, you need to move this method to your ServiceContract and decorate it with [OperationContract] attribute.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
imghani
Member
154 Points
39 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 25, 2012 07:54 AM|LINK
Dear kewlpack,
Important thing that need to understand is that
"Unless you mark your method with [OperationContract] attribute, it will not be available to client, because while creating proxy only those methods will be exposed which have that specific attribute and inside a service, others will be ignored".
WCF Tutorials
kewlpack
0 Points
7 Posts
Re: Can't access abstract base class' virtual methods in client of WCF service
Jun 25, 2012 02:50 PM|LINK
Thanks all -
I understand the Operation Contract, etc. After more research, I guess what I was looking to do would be more remoting than standard WCF service functionality.