Skip to content

Commit

Permalink
Merge pull request dotnet#1604 from roncain/IChannelInitializer
Browse files Browse the repository at this point in the history
Add IChannelInitializer to public API
  • Loading branch information
roncain authored Oct 25, 2016
2 parents 12d528c + 2b0fb79 commit 0165305
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

public class MockChannelInitializer : IChannelInitializer
{
public MockChannelInitializer()
{
InitializeOverride = DefaultInitialize;
}

public Action<IClientChannel> InitializeOverride { get; set; }

public void Initialize(IClientChannel channel)
{
InitializeOverride(channel);
}

public void DefaultInitialize(IClientChannel channel)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

public class MockEndpointBehavior : IEndpointBehavior
{
public MockEndpointBehavior()
{
ValidateOverride = DefaultValidate;
AddBindingParametersOverride = DefaultAddBindingParameters;
ApplyDispatchBehaviorOverride = DefaultApplyDispatchBehavior;
ApplyClientBehaviorOverride = DefaultApplyClientBehavior;
}

public Action<ServiceEndpoint> ValidateOverride { get; set; }
public Action<ServiceEndpoint, BindingParameterCollection> AddBindingParametersOverride { get; set; }
public Action<ServiceEndpoint, EndpointDispatcher> ApplyDispatchBehaviorOverride { get; set; }
public Action<ServiceEndpoint, ClientRuntime> ApplyClientBehaviorOverride { get; set; }

public void Validate(ServiceEndpoint endpoint)
{
ValidateOverride(endpoint);
}
public void DefaultValidate(ServiceEndpoint endpoint)
{
}

public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
AddBindingParametersOverride(endpoint, bindingParameters);
}

public void DefaultAddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ApplyDispatchBehaviorOverride(endpoint, endpointDispatcher);
}

public void DefaultApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
ApplyClientBehaviorOverride(endpoint, clientRuntime);
}
public void DefaultApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ public ClientOperation(System.ServiceModel.Dispatcher.ClientRuntime parent, stri
public sealed partial class ClientRuntime
{
internal ClientRuntime() { }
public System.Collections.Generic.SynchronizedCollection<IChannelInitializer> ChannelInitializers { get { return default(System.Collections.Generic.SynchronizedCollection<IChannelInitializer>); } }
public System.Collections.Generic.ICollection<System.ServiceModel.Dispatcher.IClientMessageInspector> ClientMessageInspectors { get { return default(System.Collections.Generic.ICollection<System.ServiceModel.Dispatcher.IClientMessageInspector>); } }
public System.Collections.Generic.ICollection<System.ServiceModel.Dispatcher.ClientOperation> ClientOperations { get { return default(System.Collections.Generic.ICollection<System.ServiceModel.Dispatcher.ClientOperation>); } }
public System.Type ContractClientType { get { return default(System.Type); } set { } }
Expand Down Expand Up @@ -1445,6 +1446,10 @@ public FaultContractInfo(string action, Type detail) {}
public string Action { get { return default(string); } }
public Type Detail { get { return default(Type); } }
}
public partial interface IChannelInitializer
{
void Initialize(IClientChannel channel);
}
public partial interface IClientMessageFormatter
{
object DeserializeReply(System.ServiceModel.Channels.Message message, object[] parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Threading;
using Infrastructure.Common;
using Xunit;
Expand Down Expand Up @@ -393,6 +395,84 @@ public static void CustomChannel_Factory_Abort_Aborts_Channel()
String.Format("Expected channel's final state to be Closed but was '{0}'", ((ICommunicationObject)channel).State));
}

[WcfFact]
public static void CustomChannel_ChannelInitializer_CreateChannel_Calls_Initialize()
{
string testMessageBody = "CustomChannelTest_Sync";
Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

// *** SETUP *** \\
MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
CustomBinding binding = new CustomBinding(mockBindingElement);
EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

// Create a mock channel initializer to observe whether
// its method is invoked.
bool initializeCalled = false;
MockChannelInitializer mockInitializer = new MockChannelInitializer();
mockInitializer.InitializeOverride = (chnl) =>
{
initializeCalled = true;
mockInitializer.DefaultInitialize(chnl);
};

// Add an endpoint behavior that registers the mock initializer
MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
{
clientRuntime.ChannelInitializers.Add(mockInitializer);
};

factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

// *** EXECUTE *** \\
factory.CreateChannel();

// *** VALIDATE *** \\
Assert.True(initializeCalled, "Initialize was not called.");
}

[WcfFact]
public static void CustomChannel_ChannelInitializer_Failed_Initialize_Throws()
{
string testMessageBody = "CustomChannelTest_Sync";
Message inputMessage = Message.CreateMessage(MessageVersion.Default, action: "Test", body: testMessageBody);

// *** SETUP *** \\
MockTransportBindingElement mockBindingElement = new MockTransportBindingElement();
CustomBinding binding = new CustomBinding(mockBindingElement);
EndpointAddress address = new EndpointAddress("myprotocol://localhost:5000");
var factory = new ChannelFactory<ICustomChannelServiceInterface>(binding, address);

// Create a mock channel initializer to fault inject an exception
InvalidOperationException thrownException = new InvalidOperationException("test threw this");
MockChannelInitializer mockInitializer = new MockChannelInitializer();
mockInitializer.InitializeOverride = (chnl) =>
{
mockInitializer.DefaultInitialize(chnl);
throw thrownException;
};

// Add an endpoint behavior that registers the mock initializer
MockEndpointBehavior mockEndpointBehavior = new MockEndpointBehavior();
mockEndpointBehavior.ApplyClientBehaviorOverride = (ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) =>
{
clientRuntime.ChannelInitializers.Add(mockInitializer);
};

factory.Endpoint.EndpointBehaviors.Add(mockEndpointBehavior);

// *** EXECUTE *** \\
InvalidOperationException caughtException =
Assert.Throws<InvalidOperationException>(() => factory.CreateChannel());

// *** VALIDATE *** \\
Assert.True(String.Equals(caughtException.Message, thrownException.Message),
String.Format("Expected exception message '{0}' but actual was '{1}'",
thrownException.Message, caughtException.Message));
}

#region Helpers

[ServiceContract]
Expand Down

0 comments on commit 0165305

Please sign in to comment.