问题描述:

Hello It should be on the side of the host WCF have to connect an event / client disconnects. Disabling occurs in this case after the session life time.

Every client I have to recognize (Ip address or specific information provided in the client settings is not important, the main thing to identify)). I use the re definition of the behavior of the endpoint.

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() });

}

}

Host Start

 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();

Events Initialize and ClientDisconnected triggered properly. But no information about the client, I can learn from them not channel.RemoteAddress == null.

In advance thank you all !!

相关阅读:
Top