static WcfRoutingService() { BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); _factory = binding.BuildChannelFactory <IRequestChannel>(); _factory.Open(); }
public static void Main() { var b = new BasicHttpBinding(); b.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; var cc = new ClientCredentials(); cc.UserName.UserName = "******"; IChannelFactory <IRequestChannel> cf = b.BuildChannelFactory <IRequestChannel> (cc); cf.Open(); IRequestChannel req = cf.CreateChannel( new EndpointAddress("http://localhost:8080/")); Console.WriteLine(cf.GetProperty <ClientCredentials> ()); req.Open(); Message msg = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IFoo/Echo", new EchoType("hoge")); //Message ret = req.Request (msg); IAsyncResult result = req.BeginRequest(msg, null, null); //return; Message ret = req.EndRequest(result); using (XmlWriter w = XmlWriter.Create(Console.Out)) { ret.WriteMessage(w); } }
public void BuildChannelFactoryForHttpEndpoint() { var b = new BasicHttpBinding(); b.Security.Mode = BasicHttpSecurityMode.Transport; var cf = b.BuildChannelFactory <IRequestChannel> (); cf.Open(); cf.CreateChannel(new EndpointAddress("http://localhost:8080")); }
public static void Create_HttpBinding_SecurityMode_Without_SecurityBindingElement(BasicHttpSecurityMode securityMode) { BasicHttpBinding binding = new BasicHttpBinding(securityMode); var bindingElements = binding.CreateBindingElements(); var securityBindingElement = bindingElements.FirstOrDefault(x => x is SecurityBindingElement) as SecurityBindingElement; Assert.True(securityBindingElement == null, string.Format("securityBindingElement should be null when BasicHttpSecurityMode is '{0}'", securityMode)); Assert.True(binding.CanBuildChannelFactory <IRequestChannel>(), string.Format("CanBuildChannelFactory should return true for BasicHttpSecurityMode:'{0}'", securityMode)); binding.BuildChannelFactory <IRequestChannel>(); }
public static void IDuplexSessionChannel_Http_BasicHttpBinding() { IChannelFactory <IDuplexSessionChannel> factory = null; IDuplexSessionChannel channel = null; Message replyMessage = null; try { // *** SETUP *** \\ BasicHttpBinding binding = new BasicHttpBinding(); // Create the channel factory factory = binding.BuildChannelFactory <IDuplexSessionChannel>(new BindingParameterCollection()); factory.Open(); // Create the channel. channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_NetHttps)); 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); } }
//[WcfFact] //[OuterLoop] public static void IRequestChannel_Async_Http_BasicHttpBinding() { IChannelFactory <IRequestChannel> factory = null; IRequestChannel channel = null; Message replyMessage = null; try { // *** SETUP *** \\ BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); // Create the channel factory factory = binding.BuildChannelFactory <IRequestChannel>(new BindingParameterCollection()); Task.Factory.FromAsync(factory.BeginOpen, factory.EndOpen, TaskCreationOptions.None).GetAwaiter().GetResult(); // Create the channel. channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_Basic)); Task.Factory.FromAsync(channel.BeginOpen, channel.EndOpen, TaskCreationOptions.None).GetAwaiter().GetResult(); // Create the Message object to send to the service. Message requestMessage = Message.CreateMessage( binding.MessageVersion, action, new CustomBodyWriter(clientMessage)); // *** EXECUTE *** \\ // Send the Message and receive the Response. replyMessage = Task.Factory.FromAsync((asyncCallback, o) => channel.BeginRequest(requestMessage, asyncCallback, o), channel.EndRequest, TaskCreationOptions.None).GetAwaiter().GetResult(); // *** VALIDATE *** \\ // BasicHttpBinding uses SOAP1.1 which doesn't return the Headers.Action property in the Response // Therefore not validating this property as we do in the test "InvokeIRequestChannelCreatedViaBinding" 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(); Task.Factory.FromAsync(channel.BeginClose, channel.EndClose, TaskCreationOptions.None).GetAwaiter().GetResult(); Task.Factory.FromAsync(factory.BeginClose, factory.EndClose, TaskCreationOptions.None).GetAwaiter().GetResult(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects(channel, factory); } }
private void sendButton_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor; // Creating the message to send TransmittedObject to = new TransmittedObject(); to.str = "Hello"; to.i = 5; XmlSerializerWrapper wrapper = new XmlSerializerWrapper(typeof(TransmittedObject)); Message m = Message.CreateMessage (MessageVersion.Soap11, "urn:test", to, wrapper); // Creating the channel to send the message BasicHttpBinding binding = new BasicHttpBinding(); BindingParameterCollection parameters = new BindingParameterCollection(); IChannelFactory <IRequestChannel> channelFactory = binding.BuildChannelFactory <IRequestChannel>(parameters); channelFactory.Open(); // Opening the channel IRequestChannel outChannel = channelFactory.CreateChannel (new EndpointAddress (new Uri("http://<<ServerAddress>>:<<PortNumber>>"))); outChannel.Open(TimeSpan.MaxValue); // Sending the Message and waiting for the reply Message reply = outChannel.Request(m, TimeSpan.MaxValue); // Deserializing and using the Message TransmittedObject to1 = reply.GetBody <TransmittedObject> (new XmlSerializerWrapper(typeof(TransmittedObject))); MessageBox.Show(to1.str + " " + to1.i.ToString()); // Cleaning up outChannel.Close(); channelFactory.Close(); Cursor.Current = Cursors.Default; }
public static void IRequestChannel_Http_BasicHttpBinding() { try { BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); // Create the channel factory IChannelFactory <IRequestChannel> factory = binding.BuildChannelFactory <IRequestChannel>( new BindingParameterCollection()); factory.Open(); // Create the channel. IRequestChannel channel = factory.CreateChannel( new EndpointAddress(Endpoints.HttpBaseAddress_Basic)); channel.Open(); // Create the Message object to send to the service. Message requestMessage = Message.CreateMessage( binding.MessageVersion, action, new CustomBodyWriter(clientMessage)); // Send the Message and receive the Response. Message replyMessage = channel.Request(requestMessage); // BasicHttpBinding uses SOAP1.1 which doesn't return the Headers.Action property in the Response // Therefore not validating this property as we do in the test "InvokeIRequestChannelCreatedViaBinding" 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)) { Assert.True(false, 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) { Assert.True(false, String.Format("Unexpected exception was caught: {0}", ex.ToString())); } }
static void Main(string[] args) { EndpointAddress address = new EndpointAddress("http://127.0.0.1:9999/messagingviabinding"); BasicHttpBinding binding = new BasicHttpBinding(); IChannelFactory <IRequestChannel> channelFactory = binding.BuildChannelFactory <IRequestChannel>(); channelFactory.Open(); IRequestChannel channel = channelFactory.CreateChannel(address); channel.Open(); Message requestMessage = Message.CreateMessage(MessageVersion.Soap11, "http://artech/messagingviabinding", "The is a request message manually created for the purpose of testing."); Message replyMessage = channel.Request(requestMessage); Console.WriteLine("Receive a reply message:\n{0}", replyMessage); channel.Close(); channelFactory.Close(); Console.Read(); }
static void Main(string[] args) { Uri listenUri = new Uri("http://127.0.0.1:3721/listener"); Binding binding = new BasicHttpBinding(); IChannelFactory <IRequestChannel> channelFactory = binding.BuildChannelFactory <IRequestChannel>(); channelFactory.Open(); IRequestChannel channel = channelFactory.CreateChannel(new EndpointAddress(listenUri)); channel.Open(); Message replyMessage = channel.Request(CreateRequestMessage(binding)); Console.WriteLine(replyMessage); Console.Read(); }
/// <summary> /// 通过channel与服务端通信 /// </summary> static public void TestChannel() { BasicHttpBinding binding = new BasicHttpBinding(); CustomBinding cb = new CustomBinding(); BindingParameterCollection param = new BindingParameterCollection(); BindingContext bc = new BindingContext(cb, param); Message request = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IHomeService/TestMessage", "gaofeng"); request.Headers.Add(MessageHeader.CreateHeader("ip", string.Empty, Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString())); IChannelFactory <IRequestChannel> factory = binding.BuildChannelFactory <IRequestChannel>(param); factory.Open(); IRequestChannel channel = factory.CreateChannel(new EndpointAddress("http://127.0.0.1:28200/HomeService")); channel.Open(); var res = channel.Request(request); Console.WriteLine("结果为:" + res.GetBody <int>()); channel.Close(); factory.Close(); }
public static void Custom_Message_RoundTrips() { BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); // Create the channel factory IChannelFactory <IRequestChannel> factory = binding.BuildChannelFactory <IRequestChannel>(new BindingParameterCollection()); factory.Open(); // Create the channel. IRequestChannel channel = factory.CreateChannel(new EndpointAddress(Endpoints.HttpBaseAddress_Basic_Text)); channel.Open(); // Create the Message object to send to the service. using (Message requestMessage = Message.CreateMessage( binding.MessageVersion, action, new CustomBodyWriter(clientMessage))) { // Send the Message and receive the Response. using (Message replyMessage = channel.Request(requestMessage)) { Assert.False(replyMessage.IsFault); Assert.False(replyMessage.IsEmpty); Assert.Equal(MessageState.Created, replyMessage.State); Assert.Equal(MessageVersion.Soap11, replyMessage.Version); var replyReader = replyMessage.GetReaderAtBodyContents(); string actualResponse = replyReader.ReadElementContentAsString(); string expectedResponse = "Test Custom_Message_RoundTrips.[service] Request received, this is my Reply."; Assert.True(string.Equals(actualResponse, expectedResponse), string.Format("Actual MessageBodyContent from service did not match the expected MessageBodyContent, expected: {0} actual: {1}", expectedResponse, actualResponse)); } } channel.Close(); factory.Close(); }
public void SendRequestWithoutSecurity() { string uri = "http://localhost:8888"; // Open frontend Uri listenUri = new Uri(uri); Binding binding = new BasicHttpBinding(BasicHttpSecurityMode.None); MockBrokerQueue queue = new MockBrokerQueue(); queue.DirectReply = true; RequestReplyFrontEnd <IReplyChannel> target = new RequestReplyFrontEnd <IReplyChannel>(listenUri, binding, null, queue); target.Open(); // Build channel to send request IChannelFactory <IRequestChannel> factory = binding.BuildChannelFactory <IRequestChannel>(); factory.Open(); IRequestChannel channel = factory.CreateChannel(new EndpointAddress(uri)); channel.Open(); Message request = Message.CreateMessage(MessageVersion.Soap11, "UnitTest", "Test"); try { Message reply = channel.Request(request); } catch (FaultException fe) { Assert.AreEqual(fe.Code.Name, "DummyReply"); } channel.Close(); factory.Close(); target.Close(); }
static void Main(string[] args) { //创建请求的目的地址和绑定 Uri listenUri = new Uri("http://localhost:9999/listener"); Binding binding = new BasicHttpBinding(); //通过绑定创建信道工厂 IChannelFactory <IRequestChannel> channelFactory = binding.BuildChannelFactory <IRequestChannel>(); //开启信道工厂 channelFactory.Open(); //通过信道工厂创建请求信道 IRequestChannel channel = channelFactory.CreateChannel(new EndpointAddress(listenUri)); //开启请求信道 channel.Open(); //通过请求信道发送请求,并接受请求回复信息 Message replyMessage = channel.Request(CreateRequestMessage(binding)); //输出请求回复信息 Console.WriteLine("接收到回复消息\n{0}", replyMessage); Console.Read(); }
public static void Main() { Binding b = new BasicHttpBinding(); IChannelFactory <IRequestChannel> cf = b.BuildChannelFactory <IRequestChannel> ( new BindingParameterCollection()); cf.Open(); IRequestChannel req = cf.CreateChannel( new EndpointAddress("http://localhost:8080/")); Console.WriteLine(req.State); req.Open(); Message msg = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IFoo/Echo", new EchoType("hoge")); //Message ret = req.Request (msg); IAsyncResult result = req.BeginRequest(msg, null, null); //return; Message ret = req.EndRequest(result); Console.WriteLine(req.State); using (XmlWriter w = XmlWriter.Create(Console.Out)) { ret.WriteMessage(w); } }
public static void Main() { Binding b = new BasicHttpBinding(); IChannelFactory <IRequestChannel> cf = b.BuildChannelFactory <IRequestChannel> ( new BindingParameterCollection()); cf.Open(); IRequestChannel req = cf.CreateChannel( new EndpointAddress("http://localhost:8080/")); Console.WriteLine(req.State); req.Open(); Message msg = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IFoo/ProcessMessage", new EchoType("hoge")); Message ret = req.Request(msg); Console.WriteLine(req.State); var p = ret.Properties [HttpResponseMessageProperty.Name] as HttpResponseMessageProperty; Console.WriteLine(p.StatusCode); using (XmlWriter w = XmlWriter.Create(Console.Out)) { ret.WriteMessage(w); } }
void updateButton_Click(object sender, EventArgs e) { // Make sure a server name was provided. if (hostBox.Text.Trim().Length == 0) { MessageBox.Show("Specify a server name first."); return; } // Since creating a factory and sending a message can take // a noticeable amount of time, let the user know we're working // by setting the wait cursor. Cursor.Current = Cursors.WaitCursor; // Make sure the wait cursor has a chance to show up by calling // DoEvents. Application.DoEvents(); try { // Construct our channel factory, which we'll use to send // uptime requests. // In a production app, we would normally construct the // factory just once for the app, and reuse it to send // every message. var binding = new BasicHttpBinding(); var parameters = new BindingParameterCollection(); var factory = binding.BuildChannelFactory <IRequestChannel>(parameters); factory.Open(); // We send the message in a try block to guarantee that we // close the channel and factory when we're done. try { // Open the channel to the server the user provided, // on the hard-coded port number. var addr = new EndpointAddress("http://" + hostBox.Text + ":" + UptimeRequest.ListeningPort); var channel = factory.CreateChannel(addr); channel.Open(); try { // Formulate our request, which is really simple. var request = new UptimeRequest(); // Serialize that request into a WCF message. Message requestMessage = Message.CreateMessage( binding.MessageVersion, "urn:UptimeRequest", new UptimeRequest(), MessageSerializer.RequestSerializer ); // Send the request and block waiting for a response. var responseMessage = channel.Request(requestMessage); // Deserialize the response. var response = responseMessage.GetBody <UptimeResponse> (MessageSerializer.ResponseSerializer); // Read the response and show results to user. uptimeLabel.Text = response.Uptime.ToString(); timestampLabel.Text = response.Timestamp.ToString(); } finally { channel.Close(); } } finally { factory.Close(); } } catch (Exception ex) { // don't catch fatal exceptions if (ex is OutOfMemoryException || ex is StackOverflowException) { throw; } // Display the error to the user to decide what to do. if (MessageBox.Show(ex.Message, "Error querying server uptime", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Retry) { updateButton_Click(sender, e); // try again } } finally { // Reset wait cursor in finally block to ensure it goes away // even in case of failure. Cursor.Current = Cursors.Default; } }