C# 无法显示客户端Web窗体中的计数
我试图显示从客户端windows窗体应用程序调用方法的次数。下面是如何定义服务和客户机的 在我的日志文件中,我看到每次方法调用的计数都是递增的,但我无法看到从客户机表单中放入列表的总计数C# 无法显示客户端Web窗体中的计数,c#,wcf,C#,Wcf,我试图显示从客户端windows窗体应用程序调用方法的次数。下面是如何定义服务和客户机的 在我的日志文件中,我看到每次方法调用的计数都是递增的,但我无法看到从客户机表单中放入列表的总计数 IOperator { SendMessage(string strMsgId, string strMessage); [OperationContract] List<int> GetCount(); } [ServiceBehavior(Namespace =
IOperator
{
SendMessage(string strMsgId, string strMessage);
[OperationContract]
List<int> GetCount();
}
[ServiceBehavior(Namespace = "http://X.org/MessageService/"]
Operator: IOperator
{
private List<Int32> TotalCount = new List<Int32>();
public static List<int> TotalCount
{
get { return _totalCount; }
set { _totalCount = value; }
}
SendMessage(string strMsgId, string strMessage)
{
if (strMsgId == "02")
{
lock (_lock)
{
++_count;
TotalCount.Add(_count);
}
string debugFileName = "C:\\Test.txt";
// Write to the file:
inboundMessageLog.WriteLine("{0}{1}", "Inbound Message:", strMessage.Substring(549, 27));
inboundMessageLog.WriteLine("{0}{1}", "count:", _count);
inboundMessageLog.WriteLine("{0}{1}", "Total Count:", TotalCount.Count);
result = 0;
}
}
public List<int> GetCount()
{
return TotalCount;
}
}
IOperator
{
SendMessage(字符串strMsgId,字符串strMessage);
[经营合同]
List GetCount();
}
[ServiceBehavior(命名空间=”http://X.org/MessageService/"]
操作员:IOperator
{
私有列表TotalCount=新列表();
公共静态列表总数
{
获取{return\u totalCount;}
设置{u totalCount=value;}
}
SendMessage(字符串strMsgId,字符串strMessage)
{
如果(strMsgId==“02”)
{
锁
{
++_计数;
TotalCount.Add(_count);
}
string debugFileName=“C:\\Test.txt”;
//写入文件:
inboundMessageLog.WriteLine(“{0}{1}”,“入站消息:”,strMessage.Substring(549,27));
inboundMessageLog.WriteLine(“{0}{1}”,“count:”,\u count);
inboundMessageLog.WriteLine(“{0}{1}”,“总数:”,TotalCount.Count);
结果=0;
}
}
公共列表GetCount()
{
返回TotalCount;
}
}
编辑
我正在尝试在给定时间的某个会话中保存该总计数,并在文本框中获取该计数。我希望该总计数与客户端数量无关。TotalCount是静态的,定义为私有静态列表_TotalCount=new List();带有getter TotalCount 我没有为服务明确定义InstanceContextMode,是的,totalcount显示为0 客户:
var clientA = new SendServiceReference.SendService();
Operator clientB = new Operator();
while ((DateTime.Now - startTime) <= timeoutSpan)
{
// Send request to external service and all the requests will be logged to my service since I don't have control over the external service.
sendMessageResult = clientA.SendMessageToExternalService("01", txtRequest.Text);
}
//display the total request received from client A for the give time span
responseCount.Text = clientB.GetCount().Count.ToString();
var clientA=new-SendServiceReference.SendService();
操作符clientB=新操作符();
虽然((DateTime.Now-startTime)您没有指明您正在使用的绑定,或者您是否已明确定义了服务的绑定,但是从您描述的行为来看,它听起来像是默认的PerSession
,它为每个客户机创建了一个新的服务实例
最有可能发生的情况是,您正在创建一个客户端来发送消息,这就是为什么您看到计数器递增。然后,您创建了第二个客户端(client2=new Operator();
),这将创建服务的另一个实例,这意味着TotalCount
为0或null(因为您没有指出您遇到了错误,所以我猜测计数TotalCount
为0)。换句话说,您不再访问/使用增加计数的服务实例,而是一个具有自己的TotalCount
字段/属性的全新服务实例
有几种方法可以解决这个问题,具体取决于您的需求
如果无论客户端数量多少,您都需要总计数,您可以将TotalCount
设置为静态,也可以将InstanceContextMode
设置为Single
。我不建议使用InstanceContextMode.Single
,因为这可能导致缩放问题,而使用静态TotalCount
如果需要每个客户端的总计数,则需要使用在循环中进行10次调用的同一客户端来调用GetCount()
。例如:
Operator client1 = new Operator();
for (int i =0; i < 10; i++)
{
// Send your messages
}
responseCount.Text = client1.GetCount().Count.ToString();
Operator client1=new Operator();
对于(int i=0;i<10;i++)
{
//发送您的消息
}
responseCount.Text=client1.GetCount().Count.ToString();
有一篇关于CodeProject的文章举例说明了3种不同的InstanceContextModes
可能对您有用:我试图在给定时间的某个会话中保存该总计数,并将该计数保存到我的文本框中。客户端a正在调用某个外部服务,所有请求都将记录到我的服务中。我想计算到我的服务的请求数,并保存总计数并以任何方式显示它。如果您只想在某一段时间内包含计数,则可以设置计时器并在该计时器期间使用同一客户端。或者将总数字段设为静态,然后重置它(通过另一个服务调用)。