从uwp调用双工wcf服务方法无效

时间:2019-01-23 14:07:26

标签: c# wcf uwp duplex

我正在开发一个UWP应用程序,该应用程序使用我自己提供的wcf双工服务。当我尝试从服务调用方法时,会出现问题,方法实际上不会启动,并且服务代码中没有断点。

这是我在主机应用程序中的配置

<system.serviceModel>
  <services>
    <service name="WcfService.Services.ImageExchangeService" behaviorConfiguration="behaviorConfig">
      <endpoint address="net.tcp://localhost:4322/WcfService/Services/ImageExchangeService/tcp" binding="netTcpBinding" contract="WcfService.Services.IImageExchangeService" bindingConfiguration="tcpBinding"></endpoint>
      <endpoint address="net.tcp://localhost:4323/WcfService/Services/ImageExchangeService/mex" binding="mexTcpBinding" contract="IMetadataExchange" />
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:4321/WcfService/Services/ImageExchangeService" />
        </baseAddresses>
      </host>
    </service>
  </services>  
    <behaviors>
  <serviceBehaviors>
    <behavior name="behaviorConfig">
      <!-- To avoid disclosing metadata information, 
        set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes, 
        set the value below to true.  Set to false before deployment 
        to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <netTcpBinding>
    <binding name="tcpBinding">
      <security mode="None"></security>
      <reliableSession enabled="true"/>
    </binding>
  </netTcpBinding>
</bindings>
</system.serviceModel>

这是我的服务界面

    [ServiceContract(CallbackContract = typeof(IImageExchangeClient), SessionMode =SessionMode.Required)]
    public interface IImageExchangeService
    {
        [OperationContract(IsOneWay = true)]
        void Connect(string deviceIdentifier); 
    }

这是我对上述接口的实现

  [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults =true)]
    public class ImageExchangeService : IImageExchangeService
    {
        private Dictionary<string, IImageExchangeClient> _connectedDevices = new Dictionary<string, IImageExchangeClient>();


        public void Connect(string deviceIdentifier)
        {
           var connection = OperationContext.Current.GetCallbackChannel<IImageExchangeClient>();
            if (!_connectedDevices.ContainsKey(deviceIdentifier))
            {
                _connectedDevices.Add(deviceIdentifier, connection);
            }
            else
            {
                _connectedDevices[deviceIdentifier] = connection;
            }

            Console.WriteLine("\nDevice connected: {0}", deviceIdentifier);


        }
}

最后这是我的UWP代码,我试图在其中调用Connect方法

        public async void InitializeImageExchangeClient()
        {

            var endpoint = new EndpointAddress(new Uri("net.tcp://localhost:4322/WcfService/Services/ImageExchangeService/tcp"));
            var InstanceContext = new InstanceContext(new ImageExchangeCallback());

            var client = new ImageExchangeService.ImageExchangeServiceClient(buildBinding(), endpoint);
            var channelFactory = (DuplexChannelFactory<IImageExchangeService>)(client.ChannelFactory);
            var proxy = channelFactory.CreateChannel(InstanceContext);
            await proxy.ConnectAsync(DeviceIdentifier);
}

我也尝试了校准此方法的其他方法,例如:

            var client = new ImageExchangeServiceClientBase(InstanceContext, buildBinding(), endpoint);
            await client.OpenAsync();
            await client.ConnectAsync(DeviceIdentifier);

buildBinding()函数如下所示:

private NetTcpBinding buildBinding()
        {
            var binding = new NetTcpBinding()
            {

                MaxReceivedMessageSize = int.MaxValue,
                MaxBufferPoolSize = int.MaxValue,
                ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                {
                    MaxArrayLength = int.MaxValue,
                    MaxBytesPerRead = int.MaxValue,
                    MaxDepth = int.MaxValue,
                    MaxNameTableCharCount = int.MaxValue,
                    MaxStringContentLength = int.MaxValue
                },
                OpenTimeout = TimeSpan.MaxValue,
                ReceiveTimeout = TimeSpan.MaxValue,


            };
            binding.Security.Mode = SecurityMode.None;
            binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None;
            binding.Security.Message.ClientCredentialType = MessageCredentialType.None;

            return binding;
        }

实际结果是方法Connect未启动,当我在此方法上切换断点时,未命中。 其他非双工服务也可以正常工作。 当我在WPF应用程序中实现此服务时,该方法可以正确调用并且一切正常,问题是当我尝试在UWP体系结构中实现此服务时。 预先感谢您的帮助。

0 个答案:

没有答案