以下是双工服务及其 WebConfig 文件。在服务中,发送客户端通过TransmitUserData方法向其发送数据,接收客户端通过PublishUserData从 CallBackContract 接收数据。
现在的问题是,如果我第一次运行客户端,它可以完美运行,但是如果我重新运行客户端,它会在传输时出现以下错误。
通信对象 System.ServiceModel.Channels.ServiceChannel 不能用于通信,因为它已被中止。
双工服务合同
[ServiceContract(CallbackContract=typeof(IContactManagerCallBackService))]
public interface IContactManagerUserService
{
[OperationContract(IsOneWay = true)]
void SubscribeAsReceiver();
[OperationContract(IsOneWay = true)]
void SubscribeAsTransmitter();
[OperationContract]
void TransmitUserData(UserInfo info);
}
[ServiceContract]
public interface IContactManagerCallBackService
{
[OperationContract(IsOneWay = true)]
void PublishUserData(UserInfo info);
}
[DataContract]
public class UserInfo
{
[DataMember]
public bool PaidUser { get; set; }
[DataMember]
public string ProfileName{ get; set;}
[DataMember]
public string ComputerName { get; set; }
[DataMember]
public string IPAddress { get; set; }
[DataMember]
public string MACAddress { get; set; }
}
服务实施
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class ContactManagerUserService : IContactManagerUserService
{
SynchronizedCollection<IContactManagerCallBackService> receiverClients;
public ContactManagerUserService()
{
receiverClients = new SynchronizedCollection<IContactManagerCallBackService>();
}
public void SubscribeAsReceiver()
{
IContactManagerCallBackService client = OperationContext.Current.GetCallbackChannel<IContactManagerCallBackService>();
receiverClients.Add(client);
}
public void TransmitUserData(UserInfo info)
{
foreach (IContactManagerCallBackService receiverClient in receiverClients)
{
receiverClient.PublishUserData(info);
}
}
网页配置
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true"
logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<bindings>
<netTcpBinding>
<binding name="NewBinding0" maxConnections="1000"
portSharingEnabled="true">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="ContactManagerService.ContactManagerUserService">
<endpoint address="mex" binding="netTcpBinding" bindingConfiguration=""
name="dataEndPoint" contract="ContactManagerService.IContactManagerUserService" />
<endpoint binding="mexTcpBinding" bindingConfiguration="" name="mexHttp"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost/ContactManagerUserService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
编辑:
代码第一次完美运行。实际上问题是由 Transmitter 造成的,我们从不订阅或取消订阅。