Reputation: 6881
I'm new to C# and to WCF, coming from a Java background.
I'm have a solution with a WCF service hosted on IIS that has an Employee class, and a method...
public Employee getEmployee(int loginId)
This method takes the loginId, queries the database and creates an Employee object with the results, and then returns the Employee object. Simple enough.
So I have a client, and thus far all it does is call a simple "Hello World" web method to prove to me that it's configured right to talk to the service. Now I want a login button to take the entered loginId, call getEmployee, and create a local Employee object with the returned value.
So, I'm confused on the point of how I should define the Employee class at the client? There's not alot of info out there on how to do something so simple; like with AXIS2 and NetBeans for Java this would be utterly simple with lots of tutorials to show how, but for WCF it seems every tutorial just assumes certain knowledge that I don't have.
So the post below says that this can be done by having my Employee class defined in a separate assembly, and reference the .dll from both the WCF Service project and the client project (both are in separate solutions by the way).
WCF passing a custom object to client and running it's methods
It says "you can create a contracts assembly (an assembly that contains your thin domain models (e.g. Person etc) which you can add your own logic to.", but doesn't explain how to do this or give any reference to where I can find more information or instructions.
My Employee class is already defined directly in the WCF Service project anyways. If someone can give any info on how to move it to a "contracts assembly" (I'm C# noob and haven't even ever created a DLL before; just Windows Forms projects), that would be much appreciated.
Also, I see in another answer on the same post the following...
"1.If you can't change sources of the dll and want to call public method of the dll, it is better to use reflection. So, you receive object from WCF, sets Person properties with values returned, call method."
I understand the concept above; I would define a separate Employee class at the client, and then somehow use reflection to get the values from the object that the web method returns, and assign them to a new Employee object, right?. Only, I don't have any idea how to use reflection to get values for that.
What's best practice? Should I define an IEmployee interface like this other post suggests, and then put it in a DLL with the regular Employee class, and reference that from both server and client, and return IEmployee instead of Employee from the web method? If that's the best thing to do, is there anything special that has to be done, or can I literally just do something like this so long as such a DLL is references on both sides?
int loginId = Int32.Parse(this.loginInputTxt.Text);
LaborService.LaborServiceClient proxy = new LaborService.LaborServiceClient();
Employee emp = (Employee)proxy.getEmployee(loginId);
Upvotes: 4
Views: 2135
Reputation: 6008
WCF supports sharing Entities out-of-the-box.
You need to perform the following steps:
Contract
class library. Make sure that [DataContract]
and [DataMember]
atributes are in place.Client
and Server
projects.Upvotes: 3
Reputation: 43743
When you add the reference to the WCF service, it will create the proxy classes for you in the client project automatically. They get created within a sub-namespace. You can see them under the web reference if you show all files in your solution explorer.
Upvotes: 1