I am trying to send a non datamember class reference in a Operation contract method.
I have integrated a console app projected reference in wcf. I want that reference class object should be created for each session and that object refrence should be maintained on each subsequent client call.
Eg : EmployeRef class is in the console app which is integrated with the wcf 4.0
I want 2 methods
1 - which will create session specific object for EmployeeRef :
[OperationContract]
EmployeeRef CreateSessionObject()
initially the client can call this method to maintain a object of EmployeeRef.
2 - which will accept EmployeeRef as argument so that further calculation can be done using EmployeeRef object.
As EmployeeRef class is not in the DataContract so i am unable to achive this. Please let me know how to achieve this or is there a better way to do this.
As far as i know, anything that needs to expose through service will be serialized at one end and deserialized at the other end. So one thing is for sure that it must be serializable.Right?
Now in this scenario, if you wanted to pass that the object as method parameter or return type and its referenced from other application. Important point is "EmployeeRef type is serializable or not? means did you define it as DataContract or XmlSerializer
etc.".
No i dint define "EmployeRef" in the DataContract as this class is created inside the referenced dll file. Is there a way to define this class in the DataContract.
Create your own DataContract for EmployeeRef in your application and expose it to your client. It will act as a wrapper to your actual DLL.EmployeeRef. Your wrapper will be an exact copy of EmployeeRef.
but when ever i try to make an call to CalculateEmployeeData in client i get an error that object reference not set to an instance.
The wrapper class can not maintain an instace of EmployeeRef. I think in order to access EmployeeRef, it should be set in a public property (DataMember of EmployeWrapper) but i can not set EmployeeRef as DataMember. Is there a way to access the methods
inside EmployeeRef ?
EmployeeWrapper is your DataContract. right? So, you will just expose it to get the all possible parameter values required for EmployeeRef. So nothing else you will do with this DataContract class. Also DataContract is not designed to perform some functionality.
For Example, your EmployeeWrapper is:
[DataContract]
public class EmployeeWrapper
{
[DataMember]
public string EmployeeUserId
{
get { }
set { }
}
}
Now, while implementing your methods
public string CalculateEmployeeData(EmployeeWrapper objWrapperEmp)
{
//Call whatever the functionality of EmployeeRef passing the values of objWrapperEmp
}
So, In this approach, EmployeeRef will never be required to expose on client, but whatever input parameter required by EmployeeRef will be served with EmployeeWrapper.
I want to maintain object of "EmployeeRef" for each user. Like you can see that in order to access EmployeeRef methods i have to create a object of EmployeeRef inside operation contract "CalculateEmployeeData". I dont want to create a object of EmployeeRef
at each call.
I want EmployeeRef object should be created only once for each user .
Sandipa Moha...
Member
32 Points
13 Posts
How to send non datamember reference in wcf
Jun 25, 2012 05:57 AM|LINK
Hi Everyone ,
I am trying to send a non datamember class reference in a Operation contract method.
I have integrated a console app projected reference in wcf. I want that reference class object should be created for each session and that object refrence should be maintained on each subsequent client call.
Eg : EmployeRef class is in the console app which is integrated with the wcf 4.0
I want 2 methods
1 - which will create session specific object for EmployeeRef :
[OperationContract]
EmployeeRef CreateSessionObject()
initially the client can call this method to maintain a object of EmployeeRef.
2 - which will accept EmployeeRef as argument so that further calculation can be done using EmployeeRef object.
[OperationContract]
string CalculateEmployeeData(string strUserId, EmployeeRef objEmp)
As EmployeeRef class is not in the DataContract so i am unable to achive this. Please let me know how to achieve this or is there a better way to do this.
Thanks in advance
imghani
Member
154 Points
39 Posts
Re: How to send non datamember reference in wcf
Jun 25, 2012 06:16 AM|LINK
Sandipa,
As far as i know, anything that needs to expose through service will be serialized at one end and deserialized at the other end. So one thing is for sure that it must be serializable.Right?
Now in this scenario, if you wanted to pass that the object as method parameter or return type and its referenced from other application. Important point is "EmployeeRef type is serializable or not? means did you define it as DataContract or XmlSerializer etc.".
Please clear that point.
WCF Tutorials
Sandipa Moha...
Member
32 Points
13 Posts
Re: How to send non datamember reference in wcf
Jun 25, 2012 03:08 PM|LINK
No i dint define "EmployeRef" in the DataContract as this class is created inside the referenced dll file. Is there a way to define this class in the DataContract.
imghani
Member
154 Points
39 Posts
Re: How to send non datamember reference in wcf
Jun 26, 2012 10:27 AM|LINK
There can be a work around for this issue.
Create your own DataContract for EmployeeRef in your application and expose it to your client. It will act as a wrapper to your actual DLL.EmployeeRef. Your wrapper will be an exact copy of EmployeeRef.
WCF Tutorials
Sandipa Moha...
Member
32 Points
13 Posts
Re: How to send non datamember reference in wcf
Jun 28, 2012 06:04 AM|LINK
Hi Imghani,
I have created a wrapper class for EmployeeRef
Eg:
public class EmployeeWrapper
{
private EmployeeRef objEmpRef;
public EmployeeWrapper()
{
objEmpRef = new EmployeeRef();
}
public CalculateEmployeeData(string strUserId)
{
objEmpRef.Calculate(strUersId);
}
}
and two operation contracts
[OperationContract]
EmployeeWrapper CreateSessionObject()
[OperationContract]
string CalculateEmployeeData(string strUserId, EmployeeWrapper objEmp)
but when ever i try to make an call to CalculateEmployeeData in client i get an error that object reference not set to an instance.
The wrapper class can not maintain an instace of EmployeeRef. I think in order to access EmployeeRef, it should be set in a public property (DataMember of EmployeWrapper) but i can not set EmployeeRef as DataMember. Is there a way to access the methods inside EmployeeRef ?
Sandipa Moha...
Member
32 Points
13 Posts
Re: How to send non datamember reference in wcf
Jun 28, 2012 06:30 AM|LINK
Bit of modificaition to my above post :
I have created the Operation contract as follows rather than "string CalculateEmployeeData(string strUserId, EmployeeWrapper objEmp)"
string CalculateEmployeeWrapperData(string strUserId, EmployeeWrapper objEmp)
{
objEmp.CalculateEmployeeData(strUserId);//Here i get object reference not set to an instance.
}
imghani
Member
154 Points
39 Posts
Re: How to send non datamember reference in wcf
Jun 28, 2012 06:48 AM|LINK
Sandipa,
EmployeeWrapper is your DataContract. right? So, you will just expose it to get the all possible parameter values required for EmployeeRef. So nothing else you will do with this DataContract class. Also DataContract is not designed to perform some functionality.
For Example, your EmployeeWrapper is:
[DataContract]
public class EmployeeWrapper
{
[DataMember]
public string EmployeeUserId
{
get { }
set { }
}
}
Now, while implementing your methods
public string CalculateEmployeeData(EmployeeWrapper objWrapperEmp)
{
//Call whatever the functionality of EmployeeRef passing the values of objWrapperEmp
}
So, In this approach, EmployeeRef will never be required to expose on client, but whatever input parameter required by EmployeeRef will be served with EmployeeWrapper.
Hopefully, you got the idea.
WCF Tutorials
Sandipa Moha...
Member
32 Points
13 Posts
Re: How to send non datamember reference in wcf
Jul 01, 2012 07:59 AM|LINK
Thanks Imghani. I got your point. Now the CalculateEmployeeData operation contract should be like this :
public string CalculateEmployeeData(EmployeeWrapper objWrapperEmp)
{
EmployeeRef objEmp = new EmployeeRef(); //new object of EmployeeRef
objEmp.Calculate(objWrapperEmp.EmployeeUserId);
}
But my requirement is to have different instance of "EmployeeRef" for each client , rather than creating a new object at each request.
imghani
Member
154 Points
39 Posts
Re: How to send non datamember reference in wcf
Jul 01, 2012 09:39 AM|LINK
Sorry, I did'nt get you. You said "different instance of EmployeeRef rather than creating a new object at each request"???
Its same or one thing? Please explain.
WCF Tutorials
Sandipa Moha...
Member
32 Points
13 Posts
Re: How to send non datamember reference in wcf
Jul 01, 2012 01:53 PM|LINK
I want to maintain object of "EmployeeRef" for each user. Like you can see that in order to access EmployeeRef methods i have to create a object of EmployeeRef inside operation contract "CalculateEmployeeData". I dont want to create a object of EmployeeRef at each call.
I want EmployeeRef object should be created only once for each user .