我有一个用C ++ COM +(服务器端)和VB6(客户端)编写的项目。我们决定使用C#语言在WCF架构中重新设计项目。 作为我学习的一部分,我在WCF中创建了一个简单的测试项目,如下所示。 我们的想法是,当WBWindowsService启动时,它应该创建一个TASWB对象,然后运行一个线程来计算权重。因此,当客户端调用Getweight()方法时,它应该能够接收线程计算的权重。 但是当我尝试时,我总是得到0这是初始值。所以我认为我缺少的是如何在WCF中管理对象。 请帮助。感谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
namespace TASWeighBridge
{
public class WBWindowsService : ServiceBase
{
public ServiceHost serviceHost = null;
private TASWB obj;
public WBWindowsService()
{
// Name the Windows Service
ServiceName = "WBWindowsService";
}
public static void Main()
{
ServiceBase.Run(new WBWindowsService());
}
// Start the Windows service.
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
obj = new TASWB();
obj.RunThread();
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
serviceHost = new ServiceHost(typeof(TASWB));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
}
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
namespace TASWeighBridge
{
// Define a service contract.
[ServiceContract]
public interface IWeighBridge
{
[OperationContract]
double Getweight();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
using System.Threading;
namespace TASWeighBridge
{
// Implement the ICalculator service contract in a service class.
public class TASWB : IWeighBridge
{
private double weight = 0;
public void RunThread()
{
Thread T1 = new Thread(new ThreadStart(CalcWeight));
T1.Start();
//T1.Join();
}
private void CalcWeight()
{
Thread.Sleep(1000);
weight = weight + 1;
}
public double Getweight()
{
return weight;
}
}
}
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel> <services>
<!-- This section is optional with the new configuration model
introduced in .NET Framework 4. -->
<service name="TASWeighBridge.TASWB"
behaviorConfiguration="TASWBBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/TASWBService"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/TASWBService -->
<endpoint address=""
binding="wsHttpBinding"
contract="TASWeighBridge.IWeighBridge" />
<!-- the mex endpoint is exposed at http://localhost:8000/TASWBService/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TASWBBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>