Denying WCF Common Calls ClientChannelWrapper async

I recently developed a Silverlight application that uses Mark J Millers ClientChannelWrapper to communicate with the WCF service level (effectively killing a service link and wrapping IClientChannel and ClientChannelFactory). Here is the interface:

public interface IClientChannelWrapper<T> where T : class { IAsyncResult BeginInvoke(Func<T, IAsyncResult> function); void Dispose(); void EndInvoke(Action<T> action); TResult EndInvoke<TResult>(Func<T, TResult> function); } 

Wrapper mainly uses a common asynchronous service interface (which can be generated by slsvcutil or manually processed after WCF ServiceContract) and terminates calls to ensure that a new channel is created in the event of a channel failure. A typical use is as follows:

  public WelcomeViewModel(IClientChannelWrapper<IMyWCFAsyncService> service) { this.service = service; this.synchronizationContext = SynchronizationContext.Current ?? new SynchronizationContext(); this.isBusy = true; this.service.BeginInvoke(m => m.BeginGetCurrentUser(new AsyncCallback(EndGetCurrentUser), null)); } private void EndGetCurrentUser(IAsyncResult result) { string strResult = ""; service.EndInvoke(m => strResult = m.EndGetCurrentUser(result)); this.synchronizationContext.Send( s => { this.CurrentUserName = strResult; this.isBusy = false; }, null); } 

Everything works fine, but now I would like to unit test view models that use ClientChannelWrapper. I installed a simple unit test using Moq:

 [TestMethod] public void WhenCreated_ThenRequestUserName() { var serviceMock = new Mock<IClientChannelWrapper<IMyWCFAsyncService>>(); var requested = false; //the following throws an exception serviceMock.Setup(svc => svc.BeginInvoke(p => p.BeginGetCurrentUser(It.IsAny<AsyncCallback>(), null))).Callback(() => requested = true); var viewModel = new ViewModels.WelcomeViewModel(serviceMock.Object); Assert.IsTrue(requested); } 

I get a NotSupportedException: Unsupported expression: p => p.BeginGetCurrentUser (IsAny (), null). I am new to Moq, but I think there are some problems with ClientChannelWrapper using common service interfaces. Trying to wrap your head around this for quite some time, maybe someone has an idea. Thanks.

+4
source share
1 answer

Sorry to answer my own question, but finally understood. Here are the details:

How often, the solution was right in front of me, since it was in Mark J Millers that a specific implementation of IClientChannelWrapper was implemented. There, he provides two constructors, one of which accepts a string of the WCF endpoint name (which I use in production code) and the second:

 public ClientChannelWrapper(T service) { m_Service = service; } 

Without further ado, here is my new test code:

 [TestMethod] public void WhenCreated_ThenRequestUserName() { var IMySvcMock = new Mock<IMyWCFAsyncService>(); var serviceMock = new ClientChannelWrapper<IMyWCFAsyncService>(IMySvcMock.Object); var requested = false; IMySvcMock.Setup(svc => svc.BeginGetCurrentUser(It.IsAny<AsyncCallback>(), null)).Callback(() => requested = true); var viewModel = new ViewModels.WelcomeViewModel(serviceMock); Assert.IsTrue(requested); } 

Therefore, I basically ignore the ChannelWrapper interface and create a new instance using the Mock Object of my WCF service interface. Then I configure the service call, which will be used in the constructor of my view model, as shown above. Now everything works like a charm. Hope this is useful to someone, as I think the idea of ​​ClientChannelWrapper is great for Silverlight ↔ WCF communication. Please feel free to comment on this decision!

+3
source

Source: https://habr.com/ru/post/1341405/


All Articles