【问题标题】:Push message from wcf service to Windows Universal App将消息从 wcf 服务推送到 Windows 通用应用程序
【发布时间】:2018-12-20 23:09:52
【问题描述】:

我有一个托管在 Windows 服务上的 wcf 服务。 WinForm 和 aspnet 站点从该服务中获取信息。我创建了 Uwp 应用程序并将其连接到相同的服务。 Uwp 可以向服务发送请求并接收返回的数据。问题是当服务使用回调将数据推送到客户端时。当服务向应用广播时,应用什么也得不到。下次服务尝试广播或客户端尝试发送请求时,频道已损坏。客户端没有异常,只是没有收到响应。服务获取:

Cannot access a disposed object.

对象名称:'System.ServiceModel.Channels.ServiceChannel'。

没有内部异常。堆栈是:

Server stack trace: 

在 System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen() 在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时) 在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime 操作) 在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

在 [0] 处重新抛出异常:

主机配置:

<system.serviceModel>
<services>
  <!-- This section is optional with the new configuration model  
       introduced in .NET Framework 4. -->

  <service behaviorConfiguration="ServiceBehavior"
  name="TL.TrayNotify.WcfService.WcfService">
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://server:8089/TeamCity"/>
      </baseAddresses>
    </host>
    <endpoint address="net.tcp://server:8089/TeamCityService/TeamCity/tcp" 
              binding="netTcpBinding" contract="TL.TrayNotify.WcfService.IWcfService" 
              bindingConfiguration="tcpBinding">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>

    <endpoint address="net.tcp://server:8090/Calculator/mex" binding="mexTcpBinding"
     contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="false"/>
      <serviceDebug includeExceptionDetailInFaults="true "/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <netTcpBinding>
    <binding name="tcpBinding">
      <security mode="None"></security>
      <reliableSession enabled="false" inactivityTimeout="00:20:00" />
    </binding>
  </netTcpBinding>
</bindings>

Uwp 连接:

NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.Security.Mode = SecurityMode.None;
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;
//new client
m_client = new WcfServiceClient(tcpBinding, new EndpointAddress("net.tcp://server:8089/TeamCityService/TeamCity/tcp"));
m_client.ClientCredentials.Windows.ClientCredential.UserName = "user";
m_client.ClientCredentials.Windows.ClientCredential.Password = "password";
//bind call back event
m_client.BroadcastToClientReceived += MyTeamCityClient_ReceiveTeamCityBroadcastReceived;
await m_client.OpenAsync();
await this.m_client.RegisterClientAsync(m_deviceName);

MyTeamCityClient_ReceiveTeamCityBroadcastReceived永远不会被调用,即使服务确实向该客户端广播。

【问题讨论】:

  • 我已经创建了一个简单的测试项目来重现这里的问题:github.com/klimkina/CSharp/tree/master/UwpCalculator Calculator 工作正常,直到它注册服务消息。在来自服务的第一条消息(未收到)之后,通道被破坏。
  • 目标版本:(10.0; Build 10586) 最低版本:(10.0; Build 10240)
  • 我已帮助您直接在以下位置报告此问题:github.com/dotnet/wcf/issues/3108
  • 谢谢你,Barry Wang - MSFT
  • 我已经公开了一个 Web API 并将我的 UWP 连接到它。

标签: c# wcf callback uwp duplex-channel


【解决方案1】:

如果这个http://github.com/klimkina/CSharp/tree/master/UwpCalculator 重现了这个问题,我有一个修复程序让应用程序工作:在 reference.cs 添加以下 ICalculatorCallback 声明后跟第 #157 行,测试应用程序没有问题。

[System.ServiceModel.OperationContractAttribute(IsOneWay = true, Action = "http://tempuri.org/ICalculator/BroadcastToClient")]
void BroadcastToClient(string message);

原因: 在 UWP 项目的 reference.cs 文件中,ICalculatorCallback 定义缺少 BroadcastToClient(string) 方法的声明,尽管在名为 CalculatorClientCallbackICalculatorCallback 实现中找到了 BroadcastToClient(string) 方法的定义。

【讨论】:

    猜你喜欢
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2019-01-16
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多