Procedura: creare una channel factory e usarla per la creazione e la gestione di canaliHow to: Create a Channel Factory and Use it to Create and Manage Channels

La classe DuplexChannelFactory<TChannel> fornisce i mezzi per creare e gestire canali duplex di diversi tipi utilizzati dai client per scambiare messaggi con gli endpoint di servizio.The DuplexChannelFactory<TChannel> class provides the means to create and manage duplex channels of different types that clients use to send and receive messages to and from service endpoints.


Nel codice seguente viene illustrato come creare una channel factory e come utilizzarla per creare e gestire canali.The following code shows how to create a channel factory and use it to create and manage channels.

  // Construct InstanceContext to handle messages on the callback interface. 
  // An instance of ChatApp is created and passed to the InstanceContext.
InstanceContext site = new InstanceContext(new ChatApp());

  // Create the participant with the given endpoint configuration.
  // Each participant opens a duplex channel to the mesh.
  // Participant is an instance of the chat application that has opened a channel to the mesh.

using (DuplexChannelFactory<IChatChannel> cf =
	new DuplexChannelFactory<IChatChannel>(site,"ChatEndpoint"))
	X509Certificate2 issuer = GetCertificate(
		StoreLocation.CurrentUser, "CN=" + issuerName,
	cf.Credentials.Peer.Certificate =
		"CN=" + member,
	cf.Credentials.Peer.PeerAuthentication.CertificateValidationMode  =
	cf.Credentials.Peer.PeerAuthentication.CustomCertificateValidator =
		new IssuerBasedValidator();

	using (IChatChannel participant = cf.CreateChannel())
   // Retrieve the PeerNode associated with the participant and register for online/offline events.
   // PeerNode represents a node in the mesh. Mesh is the named collection of connected nodes.
		IOnlineStatus ostat = participant.GetProperty<IOnlineStatus>();
		ostat.Online += new EventHandler(OnOnline);
		ostat.Offline += new EventHandler(OnOffline);

		Console.WriteLine("{0} is ready", member);
		Console.WriteLine("Press <ENTER> to send the chat message.");                   

   // Announce self to other participants.
		participant.Chat(member, "Hi there - I am chatting");

		Console.WriteLine("Press <ENTER> to terminate this instance of chat.");
   // Leave the mesh and close the client.

Vedere ancheSee Also