C# 使用netTcpBinding的单向WCF调用:通过配置添加OneWayBindingElement
我想实现一个从ASP.NET应用程序到WCF服务(托管在Windows服务中)的单向、即发即忘调用。这是服务端的一个长时间运行的操作(否则我只在ASP.NET应用程序中执行),因此在操作完成之前,客户端(通道)不应保持打开状态 无论这个场景看起来多么微不足道,我已经寻找了几个小时的优雅解决方案,却找不到任何我喜欢的。关于这一点,有几个问题和博客帖子,似乎有两种可能的解决方案:C# 使用netTcpBinding的单向WCF调用:通过配置添加OneWayBindingElement,c#,wcf,wcf-binding,nettcpbinding,isoneway,C#,Wcf,Wcf Binding,Nettcpbinding,Isoneway,我想实现一个从ASP.NET应用程序到WCF服务(托管在Windows服务中)的单向、即发即忘调用。这是服务端的一个长时间运行的操作(否则我只在ASP.NET应用程序中执行),因此在操作完成之前,客户端(通道)不应保持打开状态 无论这个场景看起来多么微不足道,我已经寻找了几个小时的优雅解决方案,却找不到任何我喜欢的。关于这一点,有几个问题和博客帖子,似乎有两种可能的解决方案: 按照建议,使用消息队列作为通信层 按照建议使用OneWayBindingElement 我宁愿不使用消息队列,在服务器环
OneWayBindingElement
剩下的就是添加
OneWayBindingElement
。但我发现的所有示例都是在代码中这样做的,我宁愿不这样做,因为这样就排除了使用VisualStudioWCFSVCHost的可能性。是否可以通过配置扩展我的netTcpBinding,使用这个臭名昭著的OneWayBindingElement
为完整起见,以下是我的代码中的关键部分:
服务交互
[ServiceContract]
public interface ICalculationService
{
[OperationContract(IsOneWay = true)]
void StartTask(TaskType type);
[OperationContract(IsOneWay = true)]
void AbortTask(TaskType type);
}
服务实施
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.PerCall)]
public class CalculationService : ICalculationService
{
// ...
public void StartTask(TaskType type)
{
// ...
}
}
<services>
<service name="...CalculationService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/PLU0006B/"/>
<add baseAddress="net.tcp://localhost:8733/PLU0006B/"/>
</baseAddresses>
</host>
<endpoint
address="CalculationService"
binding="netTcpBinding"
bindingConfiguration="SatOneWayBinding"
contract="....ICalculationService" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="SatOneWayBinding">
<!-- I'm hoping I can configure 'OneWayBindingElement' here ? -->
<security mode="None">
</security>
</binding>
</netTcpBinding>
</bindings>
客户端代码,内部控制器
public class SourcesController
{
[HttpPost]
public ActionResult Upload(UploadFilesVM files)
{
// ...
using(var svcClient = GetSvcClientInstance())
{
svcClient.StartTask(TaskType.RefreshInspectionLotData);
svcClient.StartTask(TaskType.RefreshManugisticsData);
}
// ...
return RedirectToAction("Progress");
}
private CalculationServiceClient GetSvcClientInstance()
{
var client = new CalculationServiceClient();
client.Endpoint.Behaviors.Add(new SatBehavior(Context));
client.Open();
return client;
}
}
服务配置
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.PerCall)]
public class CalculationService : ICalculationService
{
// ...
public void StartTask(TaskType type)
{
// ...
}
}
<services>
<service name="...CalculationService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/PLU0006B/"/>
<add baseAddress="net.tcp://localhost:8733/PLU0006B/"/>
</baseAddresses>
</host>
<endpoint
address="CalculationService"
binding="netTcpBinding"
bindingConfiguration="SatOneWayBinding"
contract="....ICalculationService" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="SatOneWayBinding">
<!-- I'm hoping I can configure 'OneWayBindingElement' here ? -->
<security mode="None">
</security>
</binding>
</netTcpBinding>
</bindings>
“但我发现的所有示例都是在代码中这样做的,我宁愿不这样做,因为这排除了使用Visual Studio WcfSvcHost的可能性。”。。如何排除使用WcfSvcHost的可能性?[OperationContract(IsOneWay=true)]足以在“火灾和遗忘”中调用此操作behaviour@SirajMansour:不幸的是,事情似乎没那么容易。尽管代码在发出呼叫后会立即“返回”,因此看起来是单向的,但WCF会在服务端操作持续的时间内保持通道打开。因此,ASP.NET会话将使用它。尝试“强制”关闭客户端将停止执行,直到引发超时异常。