简体   繁体   English

在客户端和服务器之间的每个WCF调用之间共享常量

[英]Share constants across each WCF call between Client and Server

A couple of integer constants are computed by WCF client before making the call to the WCF service. 在调用WCF服务之前,WCF客户端会计算几个整数常量。 I would want these integer constants to be available on WCF service. 我希望这些整数常量在WCF服务上可用。 Please note that I would not want them to be used as method arguments, because these constants are required irrespective of the method in the Service. 请注意,我不希望将它们用作方法参数,因为无论服务中的方法如何,这些常量都是必需的。 Having the same argument for every method would be redundant. 每个方法都具有相同的参数将是多余的。

To be clear, client keeps computing values for 'a' and 'b' before making a WCF service call. 需要明确的是,客户端在进行WCF服务调用之前会一直为“ a”和“ b”计算值。 Once the call is made, throughout the entire call 'a' and 'b' can be accessed by the service. 拨打电话后,服务可以访问整个通话中的“ a”和“ b”。 This is similar to using them as arguments of the method. 这类似于将它们用作方法的参数。 Only difference here is that, 'a' and 'b' are needed by every method in the WCF service. 唯一的区别是WCF服务中的每种方法都需要“ a”和“ b”。 So, I would prefer a better solution than adding 'a' and 'b' as arguments to every method in the WCF call. 因此,我希望有一个更好的解决方案,而不是在WCF调用中为每个方法添加“ a”和“ b”作为参数。

So, let's say I have 3 methods in WCF service-> 因此,假设我在WCF服务中有3种方法->

void Count1()

void Count2()

void Count3()

Client will compute different 'a' and 'b' values for each WCF method call. 客户端将为每个WCF方法调用计算不同的'a'和'b'值。 One solution is to keep the methods in this format. 一种解决方案是使方法保持这种格式。

void Count1(int a, int b)

void Count2(int a, int b)

void Count3(int a, int b)

But, I would like a better solution if possible. 但是,如果可能,我希望有一个更好的解决方案。

Sorry about this long question. 很抱歉这个长问题。

You could create a "Request" class for your service as a data contract, and include in this request a , b and any other value(s) you might want to get from the client. 您可以为服务创建一个“请求”类作为数据合同,并在此请求中包括ab以及您可能希望从客户端获取的任何其他值。 A basic request would only keep a and b , but other requests might derive from it. 基本请求只能保留ab ,但其他请求也可以从中获得。
This way you only send one parameter to the service for every method. 这样,您只为每种方法向服务发送一个参数。

This is not a normal mode for WCF services, however, you can create a "sticky" service class that last for the duration of the client. 对于WCF服务,这不是正常的模式,但是,您可以创建一个持续在客户端持续时间的“粘性”服务类。 Note that the InstanceContextMode is set to session meaning that this class instance will hang around as long as the client has not disconnected/abandoned or a timeout does not occurr (you will need to send a periodic pulse in order to keep it awake if your client is not chatty). 请注意, InstanceContextMode设置为session,这意味着只要客户端尚未断开连接/放弃或没有发生超时,此类实例将一直挂起(如果您的客户端需要发送定期脉冲以使其保持唤醒状态,不chat不休)。 Also, pay attention to the ConcurrencyMode . 另外,请注意ConcurrencyMode

[ServiceBehavior(Name = "MyStickyServicee",
    InstanceContextMode = InstanceContextMode.PerSession,
    ConcurrencyMode = ConcurrencyMode.Single)]

public class MyStickyService : IMyStickyService, IDisposable
{
}

Here is some more information on instancing in wcf . 这是有关在wcf中实例化的更多信息。

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

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