在另一个 WCF 服务中的阻塞调用中轮询 WCF 服务方法
Polling a WCF service method in a blocking call within a another WCF service
更新:
问题已解决。这两段代码实际上都有效。一个潜在的问题导致失败。
我不是线程专家,但这是我的场景。
我有两个 WCF
服务(ServiceA
和 ServiceB
)
ServiceA
必须
- 每秒或配置的时间间隔轮询
ServiceB
,
- 对于某种状态,
- 阻塞直到
ServiceB
返回所需状态
- ServiceA 方法然后继续其下一个操作
专注于 Service A
的实现以实现要求,并假设我正在使用为 Service B
生成的服务引用,干净地处理和关闭,定义了接口:
public class ServiceA : IServiceA
{
public ResultObject ServiceAMethod()
{
var serviceBClient = new ServiceBReference.ServiceBClient();
//do sometthing
//call ServiceB every 1second until the status changes or a WCF timeout ends the process
return new ResultObject{/*set whatever properties need to be set*/}
}
}
我试过没有屏蔽的:
尝试 1
public class ServiceA : IServiceA
{
public ResultObject ServiceAMethod()
{
var serviceBClient = new ServiceBReference.ServiceBClient();
//do sometthing
//call ServiceB every 1second until the status changes or a WCF timeout ends the process
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
SomeStatusEnum status;
int pollingInterval = 1000;
var listener = Task.Factory.StartNew(() =>
{
status = serviceBClient.GetStatus();
while (status != SomeStatusEnum.Approved)
{
Thread.Sleep(pollingInterval);
if (token.IsCancellationRequested)
break;
status = serviceBClient.GetStatus();
}
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
return new ResultObject{/*set whatever properties need to be set determined by status*/}
}
}
}
尝试 2
public class ServiceA : IServiceA
{
public ResultObject ServiceAMethod()
{
var serviceBClient = new ServiceBReference.ServiceBClient();
//do sometthing
//call ServiceB every 1second until the status changes or a WCF timeout ends the process
SomeStatusEnum status;
int pollingInterval = 1000;
status = serviceBClient.GetStatus();
while (status == SomeStatusEnum.Approved)
{
status = serviceBClient.GetStatus();;
if (status != SomeStatusEnum.Approved)
{
break;
}
Thread.Sleep(pollingInterval);
}
return new ResultObject{/*set whatever properties need to be set determined by status*/}
}
}
在这两种情况下,状态都不会设置为预期值。我做错了什么?是否存在可能导致此问题的与 WCF 应用程序关联的行为?
我的来源:
- WCF Thread Sleep
- C# error System.NullReferenceException
- Calling a method every x minutes
- When to use Task.Delay, when to use Thread.Sleep?
这两段代码确实有效。
可以删除问题,但选择保留它以供参考,因为它还包括可能对下一个人有帮助的参考。
更新:
问题已解决。这两段代码实际上都有效。一个潜在的问题导致失败。
我不是线程专家,但这是我的场景。
我有两个 WCF
服务(ServiceA
和 ServiceB
)
ServiceA
必须
- 每秒或配置的时间间隔轮询
ServiceB
, - 对于某种状态,
- 阻塞直到
ServiceB
返回所需状态 - ServiceA 方法然后继续其下一个操作
专注于 Service A
的实现以实现要求,并假设我正在使用为 Service B
生成的服务引用,干净地处理和关闭,定义了接口:
public class ServiceA : IServiceA
{
public ResultObject ServiceAMethod()
{
var serviceBClient = new ServiceBReference.ServiceBClient();
//do sometthing
//call ServiceB every 1second until the status changes or a WCF timeout ends the process
return new ResultObject{/*set whatever properties need to be set*/}
}
}
我试过没有屏蔽的:
尝试 1
public class ServiceA : IServiceA
{
public ResultObject ServiceAMethod()
{
var serviceBClient = new ServiceBReference.ServiceBClient();
//do sometthing
//call ServiceB every 1second until the status changes or a WCF timeout ends the process
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
SomeStatusEnum status;
int pollingInterval = 1000;
var listener = Task.Factory.StartNew(() =>
{
status = serviceBClient.GetStatus();
while (status != SomeStatusEnum.Approved)
{
Thread.Sleep(pollingInterval);
if (token.IsCancellationRequested)
break;
status = serviceBClient.GetStatus();
}
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
return new ResultObject{/*set whatever properties need to be set determined by status*/}
}
}
}
尝试 2
public class ServiceA : IServiceA
{
public ResultObject ServiceAMethod()
{
var serviceBClient = new ServiceBReference.ServiceBClient();
//do sometthing
//call ServiceB every 1second until the status changes or a WCF timeout ends the process
SomeStatusEnum status;
int pollingInterval = 1000;
status = serviceBClient.GetStatus();
while (status == SomeStatusEnum.Approved)
{
status = serviceBClient.GetStatus();;
if (status != SomeStatusEnum.Approved)
{
break;
}
Thread.Sleep(pollingInterval);
}
return new ResultObject{/*set whatever properties need to be set determined by status*/}
}
}
在这两种情况下,状态都不会设置为预期值。我做错了什么?是否存在可能导致此问题的与 WCF 应用程序关联的行为?
我的来源:
- WCF Thread Sleep
- C# error System.NullReferenceException
- Calling a method every x minutes
- When to use Task.Delay, when to use Thread.Sleep?
这两段代码确实有效。
可以删除问题,但选择保留它以供参考,因为它还包括可能对下一个人有帮助的参考。