我写了一个WCF程序,想测试一下如何从外部控制里面内部变量,可是没有成功,如果不用WCF,则测试没有问题,完全正常,问问高手该如何解决这个问题啊?很头疼多谢代码如下:直接建立一个Console Application,然后把我的代码Copy进去就能运行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;namespace ConsoleApplication1
{
[ServiceContract]
public interface IGo
{
[OperationContract]
void Start(); [OperationContract]
void Stop();
} public class Go : IGo
{
bool keepRunning = true;
Action a; // 我用了一个 Action 代理来运行主程序,因为我要异步执行
public Go()
{
a = () =>
{
while (keepRunning)
{
System.Threading.Thread.Sleep(400);
Console.WriteLine("Running,Time = {0}, keepingRunning = {1}",DateTime.Now.ToString(), keepRunning.ToString());
}
};
} public void Start()
{
// 开始异步调用,在非WCF的程序里运行正常,但是在WCF的程序里,keepRunning永远是True,即使我在Stop()
// 函数里把keepRunning的值变为False也没用,这到底是怎么回事?
a.BeginInvoke(
s =>
{
Action x = (Action)s.AsyncState;
x.EndInvoke(s);
}, a);
} public void Stop()
{
keepRunning = false;
Console.WriteLine("keepRunning = {0}",keepRunning.ToString());
}
} class Program
{
static void Main(string[] args)
{
// 这个函数运行正常,在执行Stop()的时候,主程序马上结束
RunWithoutWCF(); // 这个函数运行不正常,在执行Stop()的时候,主程序不予响应,问问为什么?该如何解决?
RunWithWCF()
} static void RunWithWCF()
{
// 生成WCF的Host
ServiceHost host = new ServiceHost(typeof(ConsoleApplication1.Go));
host.Open(); Console.WriteLine("server started, press enter to start the GO service");
Console.ReadLine(); ChannelFactory<ConsoleApplication1.IGo> factory = new ChannelFactory<ConsoleApplication1.IGo>(
new BasicHttpBinding(), new EndpointAddress(new Uri(@"http://localhost:8000/MattFirstService")));
ConsoleApplication1.IGo go = factory.CreateChannel(); // 从这里开始2段程序完全一样
go.Start(); Console.WriteLine("press Enter to stop");
Console.ReadLine();
go.Stop();
Console.WriteLine("Go.Stop() has been executed, the process should be stopped, press enter to quit");
Console.ReadLine();
} static void RunWithoutWCF()
{
Console.WriteLine("press enter to start the GO service");
Console.ReadLine();
ConsoleApplication1.IGo go = new Go(); // 从这里开始2段程序完全一样
go.Start(); Console.WriteLine("press Enter to stop");
Console.ReadLine();
go.Stop();
Console.WriteLine("Go.Stop() has been executed, the process should be stopped, press enter to quit");
Console.ReadLine();
}
}
}app.config 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Go" behaviorConfiguration="mexServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/MattFirstService"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="ConsoleApplication1.IGo" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>程序有2个函数,一个是启用WCF的--static void RunWithWCF(),一个是没有启用WCF的--static void RunWithoutWCF()RunWithoutWCF() 运行正常,RunWithWCF()运行不正常,当我执行Stop()的时候,明明keepRunning的值变为False,可是在主程序里,keepRunning的值还是True,好像根本不听我指挥,这到底是怎么回事?我该怎么办?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;namespace ConsoleApplication1
{
[ServiceContract]
public interface IGo
{
[OperationContract]
void Start(); [OperationContract]
void Stop();
} public class Go : IGo
{
bool keepRunning = true;
Action a; // 我用了一个 Action 代理来运行主程序,因为我要异步执行
public Go()
{
a = () =>
{
while (keepRunning)
{
System.Threading.Thread.Sleep(400);
Console.WriteLine("Running,Time = {0}, keepingRunning = {1}",DateTime.Now.ToString(), keepRunning.ToString());
}
};
} public void Start()
{
// 开始异步调用,在非WCF的程序里运行正常,但是在WCF的程序里,keepRunning永远是True,即使我在Stop()
// 函数里把keepRunning的值变为False也没用,这到底是怎么回事?
a.BeginInvoke(
s =>
{
Action x = (Action)s.AsyncState;
x.EndInvoke(s);
}, a);
} public void Stop()
{
keepRunning = false;
Console.WriteLine("keepRunning = {0}",keepRunning.ToString());
}
} class Program
{
static void Main(string[] args)
{
// 这个函数运行正常,在执行Stop()的时候,主程序马上结束
RunWithoutWCF(); // 这个函数运行不正常,在执行Stop()的时候,主程序不予响应,问问为什么?该如何解决?
RunWithWCF()
} static void RunWithWCF()
{
// 生成WCF的Host
ServiceHost host = new ServiceHost(typeof(ConsoleApplication1.Go));
host.Open(); Console.WriteLine("server started, press enter to start the GO service");
Console.ReadLine(); ChannelFactory<ConsoleApplication1.IGo> factory = new ChannelFactory<ConsoleApplication1.IGo>(
new BasicHttpBinding(), new EndpointAddress(new Uri(@"http://localhost:8000/MattFirstService")));
ConsoleApplication1.IGo go = factory.CreateChannel(); // 从这里开始2段程序完全一样
go.Start(); Console.WriteLine("press Enter to stop");
Console.ReadLine();
go.Stop();
Console.WriteLine("Go.Stop() has been executed, the process should be stopped, press enter to quit");
Console.ReadLine();
} static void RunWithoutWCF()
{
Console.WriteLine("press enter to start the GO service");
Console.ReadLine();
ConsoleApplication1.IGo go = new Go(); // 从这里开始2段程序完全一样
go.Start(); Console.WriteLine("press Enter to stop");
Console.ReadLine();
go.Stop();
Console.WriteLine("Go.Stop() has been executed, the process should be stopped, press enter to quit");
Console.ReadLine();
}
}
}app.config 代码如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Go" behaviorConfiguration="mexServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/MattFirstService"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="ConsoleApplication1.IGo" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>程序有2个函数,一个是启用WCF的--static void RunWithWCF(),一个是没有启用WCF的--static void RunWithoutWCF()RunWithoutWCF() 运行正常,RunWithWCF()运行不正常,当我执行Stop()的时候,明明keepRunning的值变为False,可是在主程序里,keepRunning的值还是True,好像根本不听我指挥,这到底是怎么回事?我该怎么办?
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
这个属性就可以了,就是在go这个类的上面