将消息从wcf服务推送到Windows Universal App

问题描述 投票:0回答:1

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

Cannot access a disposed object.

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

没有内在的例外。堆栈是:

Server stack trace: 

处于System.ServiceModel.Channels.Cmunnels.CommunicationObject.ThrowIfDisposedOrNotOpen()处于System.ServiceModel的System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)。 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)中的Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)

在[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也永远不会被调用。

c# wcf callback uwp duplex-channel
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.