简体   繁体   中英

Push message from wcf service to Windows Universal App

I have a wcf service hosted on windows service. WinForm and aspnet site get information from this service. I created Uwp app and connected it to the same service. Uwp can send requests to the service and receive back data. Problem is when service push data to the client using callback. When service broadcasts to app, app gets nothing. Next time service try to broadcast or client try to send request the channel is broken. Client gets no exception, just doesn't receive response. Service gets:

Cannot access a disposed object.

Object name: 'System.ServiceModel.Channels.ServiceChannel'.

No inner exception. The stack is:

Server stack trace: 

at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen() at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:

Host config:

<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 connection:

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 never get called even if service do broadcast to this client.

If this http://github.com/klimkina/CSharp/tree/master/UwpCalculator reproduces the issue, I have a fix for the app to work: after adding following declaration for ICalculatorCallback in reference.cs followed by line #157 , the test app work without problem.

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

Cause: In reference.cs file of the UWP project, the ICalculatorCallback definition is missing BroadcastToClient(string) method's declaration although the BroadcastToClient(string) method's definition is found in the ICalculatorCallback implementation named CalculatorClientCallback .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM