本文整理汇总了C#中System.ServiceModel.NetTcpBinding.BuildChannelFactory方法的典型用法代码示例。如果您正苦于以下问题:C# NetTcpBinding.BuildChannelFactory方法的具体用法?C# NetTcpBinding.BuildChannelFactory怎么用?C# NetTcpBinding.BuildChannelFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceModel.NetTcpBinding
的用法示例。
在下文中一共展示了NetTcpBinding.BuildChannelFactory方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IDuplexSessionChannel_Tcp_NetTcpBinding
public static void IDuplexSessionChannel_Tcp_NetTcpBinding()
{
StringBuilder errorBuilder = new StringBuilder();
try
{
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
// Create the channel factory
IChannelFactory<IDuplexSessionChannel> factory =
binding.BuildChannelFactory<IDuplexSessionChannel>(
new BindingParameterCollection());
factory.Open();
// Create the channel.
IDuplexSessionChannel channel = factory.CreateChannel(
new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
channel.Open();
// Create the Message object to send to the service.
Message requestMessage = Message.CreateMessage(
binding.MessageVersion,
action,
new CustomBodyWriter(clientMessage));
requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());
// Send the Message and receive the Response.
channel.Send(requestMessage);
Message replyMessage = channel.Receive(TimeSpan.FromSeconds(5));
// If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
if (!String.Equals(replyMessage.Headers.RelatesTo.ToString(), requestMessage.Headers.MessageId.ToString()))
{
errorBuilder.AppendLine(String.Format("The MessageId of the incoming Message does not match the MessageId of the outgoing Message, expected: {0} but got: {1}", requestMessage.Headers.MessageId, replyMessage.Headers.RelatesTo));
}
// Validate the Response
var replyReader = replyMessage.GetReaderAtBodyContents();
string actualResponse = replyReader.ReadElementContentAsString();
string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
if (!string.Equals(actualResponse, expectedResponse))
{
errorBuilder.AppendLine(String.Format("Actual MessageBodyContent from service did not match the expected MessageBodyContent, expected: {0} actual: {1}", expectedResponse, actualResponse));
}
replyMessage.Close();
channel.Close();
factory.Close();
}
catch (Exception ex)
{
errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString()));
}
Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: CustomBindingTest FAILED with the following errors: {0}", errorBuilder));
}
示例2: IDuplexSessionChannel_Tcp_NetTcpBinding
public static void IDuplexSessionChannel_Tcp_NetTcpBinding()
{
IChannelFactory<IDuplexSessionChannel> factory = null;
IDuplexSessionChannel channel = null;
Message replyMessage = null;
try
{
// *** SETUP *** \\
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
// Create the channel factory
factory = binding.BuildChannelFactory<IDuplexSessionChannel>(new BindingParameterCollection());
factory.Open();
// Create the channel.
channel = factory.CreateChannel(new EndpointAddress(Endpoints.Tcp_NoSecurity_Address));
channel.Open();
// Create the Message object to send to the service.
Message requestMessage = Message.CreateMessage(
binding.MessageVersion,
action,
new CustomBodyWriter(clientMessage));
requestMessage.Headers.MessageId = new UniqueId(Guid.NewGuid());
// *** EXECUTE *** \\
// Send the Message and receive the Response.
channel.Send(requestMessage);
replyMessage = channel.Receive(TimeSpan.FromSeconds(5));
// *** VALIDATE *** \\
// If the incoming Message did not contain the same UniqueId used for the MessageId of the outgoing Message we would have received a Fault from the Service
Assert.Equal(requestMessage.Headers.MessageId.ToString(), replyMessage.Headers.RelatesTo.ToString());
// Validate the Response
var replyReader = replyMessage.GetReaderAtBodyContents();
string actualResponse = replyReader.ReadElementContentAsString();
string expectedResponse = "[client] This is my request.[service] Request received, this is my Reply.";
Assert.Equal(expectedResponse, actualResponse);
// *** CLEANUP *** \\
replyMessage.Close();
channel.Close();
factory.Close();
}
finally
{
// *** ENSURE CLEANUP *** \\
ScenarioTestHelpers.CloseCommunicationObjects(channel, factory);
}
}
示例3: SendMessages
private static void SendMessages()
{
var binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
var factory = binding.BuildChannelFactory<IDuplexSessionChannel>(binding);
factory.Open();
var proxy = factory.CreateChannel(new EndpointAddress(address));
proxy.Open();
var msg = "Hello";
proxy.Send(Message.CreateMessage(version, "Test", msg));
var message = proxy.Receive();
Assert.AreEqual("Echo:" + msg, message.GetBody<string>());
}