当前位置: 首页>>代码示例>>C#>>正文


C# NetTcpBinding.BuildChannelFactory方法代码示例

本文整理汇总了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));
    }
开发者ID:SoumikMukherjeeDOTNET,项目名称:wcf,代码行数:57,代码来源:DuplexChannelShapeTests.cs

示例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);
        }
    }
开发者ID:weshaggard,项目名称:wcf,代码行数:52,代码来源:DuplexChannelShapeTests.4.0.0.cs

示例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>());
 }
开发者ID:headinthebox,项目名称:IoFx,代码行数:13,代码来源:ChannelModelTests.cs


注:本文中的System.ServiceModel.NetTcpBinding.BuildChannelFactory方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。