WCF如何找到Ip连接/连接客户端?

时间:2016-11-06 15:54:22

标签: c# wcf

Hello它应该在主机一侧WCF必须连接一个事件/客户端断开连接。在这种情况下,在会话生命周期之后发生禁用。

我必须识别的每个客户端(在客户端设置中提供的IP地址或特定信息并不重要,主要用于识别))。我使用端点行为的重新定义。

class ClientTrackerEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {

        endpointDispatcher.ChannelDispatcher.ChannelInitializers.Add(new ClientTrackerChannelInitializer());
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}
  

ClientTrackerChannelInitializer

  class ClientTrackerChannelInitializer : IChannelInitializer
    {

    internal static int ConnectedClientCount = 0;
public static ISubject<ClientConnectionInfo> ClientConnectChange { get; } = new Subject<ClientConnectionInfo>();


        public void Initialize(IClientChannel channel) //!!!!!!!I would like to see here about the Clint.
        {
            ConnectedClientCount++;
            Console.WriteLine("Client {0} initialized", channel.SessionId);
            channel.Closed += ClientDisconnected;
            channel.Faulted += ClientDisconnected;

            ClientConnectChange.OnNext(new ClientConnectionInfo { Action = channel.State, SessionId = channel.SessionId, IpAdress = channel.LocalAddress.ToString()});
        }
        static void ClientDisconnected(object sender, EventArgs e)
        {
            var channel = (IClientChannel) sender;
            ConnectedClientCount--;
            ClientConnectChange.OnNext(new ClientConnectionInfo { Action = channel.State, SessionId = channel.SessionId, IpAdress = channel.LocalAddress.ToString() });
        }
    }
  

主持人开始

            string baseAddress = "http://192.168.1.33:4000/Service";
            service = new ServiceHost(typeof(Service), new Uri(baseAddress));

            //MEX EndPoint
            ServiceMetadataBehavior smb = service.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
                smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            service.Description.Behaviors.Add(smb);
            service.AddServiceEndpoint( ServiceMetadataBehavior.MexContractName,   MetadataExchangeBindings.CreateMexHttpBinding(),"mex");

            //IContract EndPoint
            WSHttpBinding binding = new WSHttpBinding(SecurityMode.None, true);
            binding.OpenTimeout = new TimeSpan(0, 0, 20);
            binding.CloseTimeout = new TimeSpan(0, 0, 20);
            binding.SendTimeout = new TimeSpan(0, 0, 5);      
            binding.ReceiveTimeout = new TimeSpan(0, 0, 25);  
            ServiceEndpoint endpoint = service.AddServiceEndpoint(typeof(IContract), binding, "");
            endpoint.Behaviors.Add(new ClientTrackerEndpointBehavior());

            service.Open();
  

正确触发事件Initialize和ClientDisconnected。但是没有关于客户端的信息,我可以从中学习而不是channel.RemoteAddress == null。

提前谢谢大家!!

0 个答案:

没有答案