简体   繁体   English

wcf服务类公开的帮助器类中的调用方法

[英]calling method in helper class exposed by wcf service class

In short: Trying to write a wcf service for a winform-app that invokes a stored procedure on a webserver. 简而言之:尝试为winform-app编写一个wcf服务,该服务调用Web服务器上的存储过程。 So far no problem - my service exposes the method "execStp(string nameOfStoredProcedure, stpParamList parameterList)" 到目前为止没有问题-我的服务公开了方法“ execStp(string nameOfStoredProcedure,stpParamList parameterList)”

 [OperationContract]
        int execStp(string stpName, srsWcfLib.Utilities.stpParamList paramList);

where stpParamList is another class holding of a third class stpParams (which basically holds a name/value pair of an SqlParameter 其中stpParamList是另一个持有第三类stpParams的类(它基本上持有SqlParameter的名称/值对

To add parameters to the list, I wrote a method in the second class 为了将参数添加到列表中,我在第二个类中编写了一个方法

public void addParameter(string ParamName, object ParamValue)
        {
            this._Parameters.Add(new stpParam(ParamName, ParamValue));
        } 
List<stpParam> _Parameters = new List<stpParam>();

    [DataMember]
    public List<stpParam> Parameters
    {
        get { return _Parameters; }
        set { _Parameters = value; }
    }

When instantiating the List-class in the win-app 在win-app中实例化List-class时

stpParamList stpParams = new stpParamList();

I can access stpParams.Parameters , but NOT stpParams.addParameter (name, value); 我可以访问stpParams.Parameters ,但不能访问stpParams.Parameters stpParams.addParameter (name, value);

What am I missing (obviously)...? 我想念的是什么(显然)...?

Thank you, Reinhard 谢谢你,莱因哈德

WCF only brings over the properties to the client, not the functions. WCF仅将属性带给客户端,而不是功能。 I forget the term they use, but it is basically just a property/field dump that gets sent through. 我忘记了它们使用的术语,但基本上只是通过它发送的属性/字段转储。

To solve this, reference the same Entity library in the client, and under "configure WCF Service" check "reuse reference" for that library. 要解决此问题,请在客户端中引用相同的实体库,然后在“配置WCF服务”下选中该库的“重用引用”。

Erich has nailed the problem on the head: WCF is a MESSAGE based transport system, eg you will transfer a serialized message of your object. 埃里希(Erich)曾将这个问题摆在头上:WCF是一个基于MESSAGE的传输系统,例如,您将传输对象的序列化消息。 The server-side object with any additional functions will be serialized into XML or a binary format, sent across the wire to the client. 具有任何其他功能的服务器端对象将被序列化为XML或二进制格式,并通过网络发送给客户端。

The client however only has the XSD (XML Schema) file exposed by your service at its disposal - it will create a client-side class that has the same "data signature" on the wire - ie the same fields, by the same name and type - but it cannot magically recreate any functions and/or methods your server-side code has. 但是,客户端只能使用您的服务公开的XSD(XML模式)文件-它将创建一个客户端类,该类具有在线上的相同“数据签名”,即相同的字段,相同的名称和类型-但无法神奇地重新创建服务器端代码具有的任何函数和/或方法。 All it can do is deserialize the message (text or binary) back into a data-only representation of the class. 它所能做的就是将消息(文本或二进制)反序列化为该类的仅数据表示形式。

This is not a bug or a problem of a binding - this is a fundamental design choice for WCF - the only connection between the client and the server is the serialized message - anything that can be represented in XML schema. 这不是错误或绑定问题-这是WCF的基本设计选择-客户端和服务器之间的唯一连接是序列化消息-可以用XML模式表示的任何内容。 It will happily serialize and deserialize data - but it cannot move code / behavior across. 它会愉快地对数据进行序列化和反序列化-但它不能跨代码/行为。 You're NOT passing the actual objects by reference like with a normal function call - the WCF runtime serializes your parameters into XML and sends it across. 不会像普通的函数调用那样通过引用传递实际的对象-WCF运行时会将您的参数序列化为XML并将其发送出去。

Now if you do control both ends of the wire, eg both the client and the server, there is a way around this, which violates the SOA principles a bit (but it can be useful). 现在,如果您确实控制了线路的两端(例如客户端和服务器),则有一种解决方法,这有点违反SOA原则(但它可能很有用)。 If you put your service contracts and data contracts into a separate assembly Contracts.dll (a class library), and then reference it from both the server side and the client side, you can indeed actually share the common .NET type srsWcfLib.Utilities.stpParamList with all its functionality. 如果将服务合同和数据合同放入单独的程序集Contracts.dll (类库)中,然后从服务器端和客户端两者都引用它,则实际上确实可以共享通用的.NET类型srsWcfLib.Utilities.stpParamList及其所有功能。 In those cases, however, you'll need to do a bit more work on the client side and instantiate your client proxy manually in code (rather than have Visual Studio or svcutil create the client proxy for you), since you need to reference that shared contracts assembly and use its types directly (rather than creating client-side classes). 但是,在这些情况下,您需要在客户端上做更多的工作,并在代码中手动实例化客户端代理(而不是让Visual Studio或svcutil为您创建客户端代理),因为您需要引用该共享合同组装并直接使用其类型(而不是创建客户端类)。

Marc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM