public static void MessageContract_RoundTrips_Properties_And_Headers() { MockChannelFactory <IRequestChannel> mockChannelFactory = null; MockRequestChannel mockRequestChannel = null; RequestBankingData_4_4_0 request = null; RequestBankingData_4_4_0 reply = null; // *** SETUP *** \\ // Intercept the creation of the factory so we can intercept creation of the channel Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) => { mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory()); mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) => { // When the channel is created, override the Request method to capture properties and headers mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via); mockRequestChannel.RequestOverride = (msg, timeout) => { // Echo back a fresh copy of the request MessageBuffer buffer = msg.CreateBufferedCopy(Int32.MaxValue); Message replyMsg = buffer.CreateMessage(); return(replyMsg); }; return(mockRequestChannel); }; return(mockChannelFactory); }; MockTransportBindingElement mockBindingElement = new MockTransportBindingElement(); mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction; CustomBinding binding = new CustomBinding(mockBindingElement); EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000"); var factory = new ChannelFactory <IMessageContractRoundTrip_4_4_0>(binding, address); IMessageContractRoundTrip_4_4_0 channel = factory.CreateChannel(); // Prepare a strongly-typed request request = new RequestBankingData_4_4_0(); request.accountName = "My account"; request.transactionDate = DateTime.Now; request.requestTestProperty = "test property"; request.testValue = "test value"; request.testValues = new string[] { "test", "values" }; request.testValuesArray = new string[] { "test", "values", "array" }; // *** EXECUTE *** \\ reply = channel.MessageContractRoundTrip(request); // *** VALIDATE *** \\ Assert.True(String.Equals(request.requestTestProperty, reply.requestTestProperty), String.Format("TestProperty expected = '{0}' actual = '{1}'", request.requestTestProperty, reply.requestTestProperty)); ValidateArray("TestValues", request.testValues, reply.testValues); ValidateArray("TestValuesArray", request.testValuesArray, reply.testValuesArray); }
public static void Message_With_MessageHeaders_RoundTrips() { BasicHttpBinding binding = null; IWcfService_4_4_0 clientProxy = null; RequestBankingData_4_4_0 requestData = null; ChannelFactory <IWcfService_4_4_0> factory = null; // *** SETUP *** \\ try { binding = new BasicHttpBinding(); factory = new ChannelFactory <IWcfService_4_4_0>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_4_4_0_Basic)); clientProxy = factory.CreateChannel(); requestData = new RequestBankingData_4_4_0(); requestData.accountName = "Michael Jordan"; requestData.transactionDate = DateTime.Now; requestData.amount = 100.0M; // post-1.1.0 features requestData.requestSingleValue = "test single value"; requestData.requestMultipleValues = "test,multiple,value".Split(','); requestData.requestArrayMultipleValues = "test,array,multiple,value".Split(','); // *** EXECUTE *** \\ ReplyBankingData_4_4_0 replyData = clientProxy.MessageContractRequestReply(requestData); // *** VALIDATE *** \\ Assert.True(String.Equals(requestData.accountName, replyData.accountName), String.Format("Expected Customer = '{0}', actual = '{1}'", requestData.accountName, replyData.accountName)); Assert.True(requestData.amount == replyData.amount, String.Format("Expected Amount = '{0}', actual = '{1}'", requestData.amount, requestData.amount)); Assert.True(String.Equals(requestData.requestSingleValue, replyData.replySingleValue), String.Format("Expected RequestSingleValue = '{0}', actual = '{1}", requestData.requestSingleValue, replyData.replySingleValue)); ValidateArray("MultipleValue", requestData.requestMultipleValues, replyData.replyMultipleValues); ValidateArray("ArrayMultipleValue", requestData.requestArrayMultipleValues, replyData.replyArrayMultipleValues); // *** CLEANUP *** \\ factory.Close(); ((ICommunicationObject)clientProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)clientProxy, factory); } }
public static void Message_With_MessageHeaders_RoundTrips() { BasicHttpBinding binding = null; IWcfService_4_4_0 clientProxy = null; RequestBankingData_4_4_0 requestData = null; ChannelFactory<IWcfService_4_4_0> factory = null; // *** SETUP *** \\ try { binding = new BasicHttpBinding(); factory = new ChannelFactory<IWcfService_4_4_0>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_4_4_0_Basic)); clientProxy = factory.CreateChannel(); requestData = new RequestBankingData_4_4_0(); requestData.accountName = "Michael Jordan"; requestData.transactionDate = DateTime.Now; requestData.amount = 100.0M; // post-1.1.0 features requestData.requestSingleValue = "test single value"; requestData.requestMultipleValues = "test,multiple,value".Split(','); requestData.requestArrayMultipleValues = "test,array,multiple,value".Split(','); // *** EXECUTE *** \\ ReplyBankingData_4_4_0 replyData = clientProxy.MessageContractRequestReply(requestData); // *** VALIDATE *** \\ Assert.True(String.Equals(requestData.accountName, replyData.accountName), String.Format("Expected Customer = '{0}', actual = '{1}'", requestData.accountName, replyData.accountName)); Assert.True(requestData.amount == replyData.amount, String.Format("Expected Amount = '{0}', actual = '{1}'", requestData.amount, requestData.amount)); Assert.True(String.Equals(requestData.requestSingleValue, replyData.replySingleValue), String.Format("Expected RequestSingleValue = '{0}', actual = '{1}", requestData.requestSingleValue, replyData.replySingleValue)); ValidateArray("MultipleValue", requestData.requestMultipleValues, replyData.replyMultipleValues); ValidateArray("ArrayMultipleValue", requestData.requestArrayMultipleValues, replyData.replyArrayMultipleValues); // *** CLEANUP *** \\ factory.Close(); ((ICommunicationObject)clientProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)clientProxy, factory); } }
public static void MessageContract_Builds_Correct_Request_Message_Properties_And_Headers() { MockChannelFactory <IRequestChannel> mockChannelFactory = null; MockRequestChannel mockRequestChannel = null; RequestBankingData_4_4_0 request = null; Dictionary <string, string> properties = new Dictionary <string, string>(); Dictionary <string, List <string> > headers = new Dictionary <string, List <string> >(); // *** SETUP *** \\ // Intercept the creation of the factory so we can intercept creation of the channel Func <Type, BindingContext, IChannelFactory> buildFactoryAction = (Type type, BindingContext context) => { mockChannelFactory = new MockChannelFactory <IRequestChannel>(context, new TextMessageEncodingBindingElement().CreateMessageEncoderFactory()); mockChannelFactory.OnCreateChannelOverride = (EndpointAddress endpoint, Uri via) => { // When the channel is created, override the Request method to capture properties and headers mockRequestChannel = (MockRequestChannel)mockChannelFactory.DefaultOnCreateChannel(endpoint, via); mockRequestChannel.RequestOverride = (msg, timeout) => { // Capture properties and headers before the message is read and closed foreach (var property in msg.Properties) { properties[property.Key] = property.Value.ToString(); } foreach (MessageHeader header in msg.Headers) { List <string> values = null; if (!headers.TryGetValue(header.Name, out values)) { values = new List <string>(); headers[header.Name] = values; } string headerValue = header.ToString().Replace(Environment.NewLine, String.Empty).Replace(" ", String.Empty); values.Add(headerValue); } // Echo back a fresh copy of the request MessageBuffer buffer = msg.CreateBufferedCopy(Int32.MaxValue); return(buffer.CreateMessage()); }; return(mockRequestChannel); }; return(mockChannelFactory); }; MockTransportBindingElement mockBindingElement = new MockTransportBindingElement(); mockBindingElement.BuildChannelFactoryOverride = buildFactoryAction; CustomBinding binding = new CustomBinding(mockBindingElement); EndpointAddress address = new EndpointAddress("myprotocol://*****:*****@"<?xml version=""1.0"" encoding=""utf-16""?>" + @"<h:Request_SingleRequestValue xmlns:h=""http://www.contoso.com"">test value</h:Request_SingleRequestValue>"); ValidateHeader(headers, TestTypeConstants_4_4_0.MessageContract_Request_MultipleValueName, @"<?xml version=""1.0"" encoding=""utf-16""?>" + @"<h:Request_MultipleRequestValue xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:h=""http://www.contoso.com"">" + @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">test</string>" + @"<string xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"">values</string>" + @"</h:Request_MultipleRequestValue>"); ValidateHeader(headers, TestTypeConstants_4_4_0.MessageContract_Request_MultipleArrayValueName, @"<?xml version=""1.0"" encoding=""utf-16""?>" + @"<h:Request_MultipleArrayRequestValue xmlns:h=""http://www.contoso.com"">test</h:Request_MultipleArrayRequestValue>", @"<?xml version=""1.0"" encoding=""utf-16""?>" + @"<h:Request_MultipleArrayRequestValue xmlns:h=""http://www.contoso.com"">values</h:Request_MultipleArrayRequestValue>", @"<?xml version=""1.0"" encoding=""utf-16""?>" + @"<h:Request_MultipleArrayRequestValue xmlns:h=""http://www.contoso.com"">array</h:Request_MultipleArrayRequestValue>"); }