4

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);
Community
  • 1
  • 1
Jim
  • 6,753
  • 12
  • 44
  • 72
  • 2
    When you add the reference to the WCF service, it will create the proxy classes for you in the client project automatically. – Steven Doggart Jun 08 '12 at 18:30
  • Where can I see those? I didn't see them. That's what I expected to happen, as it does when using Java based AXIS2 with Netbeans, but I didn't see where they were, and trying to declare something as Employee at the client resulted in a compilation error due to an unknown type. – Jim Jun 08 '12 at 18:37
  • Oh, I see now! If I declare it as LaborService.Employee instead of just Employee, it works. And if I look at the LaborService in object browser at the References.cs for the Employee class, it shows the auto-generated proxy class. Cool. I can use the properties from those as-is I guess, but can't use the methods right? – Jim Jun 08 '12 at 18:41
  • 1
    Yes, you can use the methods too, if they exist in the proxy classes. Everything that gets generated in those proxy classes is there because it is provided as part of the service contract (meaning the service will handle that functionality). Just be aware, every time you call a method on a proxy class, it will send the request to the service to perform that action. No functionality of methods in the proxy classes are performed locally on the client. – Steven Doggart Jun 08 '12 at 19:16
  • Thanks. I understand now! I actually went the route of having a shared DLL though that's referenced from both the client and server projects. That seems to work well, and then I can have the objects locally. I'd rather have the actual objects locally in the client, instead of using the proxy objects. Thanks for your help! – Jim Jun 08 '12 at 20:00

2 Answers2

3

WCF supports sharing Entities out-of-the-box.

You need to perform the following steps:

  1. Move Entity classes to separate Contract class library. Make sure that [DataContract] and [DataMember] atributes are in place.
  2. Add reference to Contract project to Client and Server projects.
  3. Generate Service reference. Check that "Reuse type in Contract assembly" is checked. Click "Advanced..." button in Add Service Reference dialog for that.
  4. You proxy class will refer types described in Contract project.
Dmitry Harnitski
  • 5,838
  • 1
  • 28
  • 43
1

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.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105