如何使用在客户端的自定义类型的对象 [英] How to use a custom type object at the client

查看:147
本文介绍了如何使用在客户端的自定义类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#和WCF,从Java背景的。

我有IIS上承载的WCF服务,有一个Employee类和方法的解决方案......

 公务员getEmployee的(INT登录ID)
 

此方法使用登录ID,查询数据库和创建的结果Employee对象,然后返回Employee对象。就这么简单。

所以我有一个客户端,并且到目前为止它是所有调用一个简单的Hello World网站的方法来证明给我看它的配置发言权的服务。现在,我希望有一个登录按钮把输入的登录ID,呼叫getEmployee的,并用返回值来创建一个本地Employee对象。

所以,我很困惑对我应该如何在客户端定义Employee类的地步?这里没有很多的信息在那里如何做这么简单的东西;就像使用Axis2和NetBeans的Java,这将是有很多的教程完全简单的展示如何,但对于WCF似乎每个教程只是假定一定的知识,我没有。

所以下面后说,这可以通过在一个单独的程序定义我的Employee类来完成,无论从WCF服务项目和客户端项目(包括在不同的解决方案的方式)引用该.dll。

<一个href="http://stackoverflow.com/questions/7718875/wcf-passing-a-custom-object-to-client-and-running-its-methods">WCF通过自定义对象的客户端和运行它的方法

它说:你可以创建一个合同组件(包含您的轻薄领域模型(如人物等,您可以添加自己的逻辑来组装)。,但并没有解释如何做到这一点,或提供任何参考到哪里可以找到更多的信息或指令。

我的Employee类已经直接定义在WCF服务项目的反正。如果有人能够就如何将其移动到合同大会的任何信息(我是C#小白,竟然还没有以前创建的DLL,只是Windows窗体的项目),这将是多少AP preciated。

另外,我在另一个答案看到同一职位如下...

1.如果你不能改变的dll源和要调用的DLL的公共方法,这是更好。所以,你从WCF接收对象使用反射,套人的属性与返回的值,调用方法。

我理解上面的概念;我将在客户端定义一个单独的Employee类,然后以某种方式使用反射来从对象获得的值,该Web方法返回,并将其分配给一个新的Employee对象,对不对?只是,我没有任何想法如何使用反射来获取值的。

什么是最好的做法?我应该定义一个IEmployee接口,这样其他职位说明,然后把它放在一个DLL与普通员工类,并引用了来自服务器和客户端,并从Web方法返回IEmployee代替员工?如果这是做的最好的事情,就是那里,有许多工作要做什么特别的东西,或者我可以从字面上只是做这样的事情,只要这样的DLL是双方参考?

  INT登录ID = Int32.Parse(this.loginInputTxt.Text);
LaborService.LaborServiceClient代理=新LaborService.LaborServiceClient();
员工EMP =(雇员)proxy.getEmployee(登录ID);
 

解决方案

WCF支持共享实体外的开箱即用的。

您需要执行以下步骤:

  1. 将实体类分开合同类库。确保 [DataContract] [数据成员] atributes到位。
  2. 添加引用承包工程客户端服务器项目。
  3. 生成服务的参考。查看,在合同组件复用式检查。点击高级...,在添加服务引用对话框按钮的。
  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);

解决方案

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.

这篇关于如何使用在客户端的自定义类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆