我有一个托管在windows服务上的wcf服务。WinForm和aspnet站点从该服务获取信息。我创建了Uwp应用程序,并将其连接到同一服务。Uwp可以向服务发送请求并接收回数据。问题是当服务使用回调将数据推送到客户端时。当服务广播到应用程序时,应用程序什么也得不到。下次服务尝试广播或客户端尝试发送请求时,通道已断开。客户端没有异常,只是没有收到响应。服务获取:
Cannot access a disposed object.
对象名称:'System。ServiceModel。频道。ServiceChannel。
没有内部异常。堆栈是:
Server stack trace:
在系统中。ServiceModel。频道。CommunicationObject。ThrowIfDisposedOrNotOpen((在系统中。ServiceModel。频道。ServiceChannel。调用(字符串操作、布尔单向、ProxyOperationRuntime操作、Object[]输入、Object[]输出、TimeSpan超时(在系统中。ServiceModel。频道。ServiceChannelProxy。InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作(在系统中。ServiceModel。频道。ServiceChannelProxy。调用(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即使服务广播到此客户端,也不会被调用。
如果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)
方法的声明,尽管BroadcastToClient(string)
方法的定义在名为CalculatorClientCallback
的ICalculatorCallback
实现中找到。