Can not have two operations in the same contract with the same name: Async method generated automatically

advertisements

edit Correction:

My Error was that Instead of using ChatService.IChatService I was using ReflectorLike.ChatServiceReference.IchatService.

In other words , AFAIU I was referencing a reference to the Interface rather than the Interface it self. (If you find better way to explain that please suggest them I'll edit the answer later) If you run in the same error be sure that you use the correct Interface.

Issue:

The Error:

SetUp : System.InvalidOperationException : Cannot have two operations in the same contract with the same name, methods ClientConnectAsync and ClientConnect in type ReflectorLike.ChatReference.IChatService violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.

Summary: I tried to do this Recommended patterns for unit testing web services

But my mocked service throws me an exception because all my methods have twins with same name for instance it has both ClientConnect and ClientConnectAsync which violate WCF Service Rule

I have a service which interface is

namespace ChatService
{
    [ServiceContract]
    public interface IChatService
    {
        [OperationContract]
        ChatUser ClientConnect(string userName);

        [OperationContract]
        void SendNewMessage(ChatMessage newMessage);

        [OperationContract]
        List<ChatUser> GetAllUsers();

        [OperationContract]
        void RemoveUser(ChatUser user);

        [OperationContract]
        List<ChatMessage> GetNewMessages(ChatUser user);
    }

    [DataContract]
    public class ChatMessage
    {
        [DataMember]
        public ChatUser User { get; set; }

        [DataMember]
        public string Message { get; set; }

        private DateTime date;
        [DataMember]
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }
    }

    /// <summary>
    ///
    /// </summary>
    [DataContract]
    public class ChatUser
    {
        [DataMember]
        public string UserName { get; set; }

        [DataMember]
        public string IpAddress { get; set; }

        [DataMember]
        public string HostName { get; set; }

        public ChatUser(string userName)
        {
            this.UserName = userName;
        }

        public override string ToString()
        {
            return this.UserName;
        }
    }

}

I want to test my client using a mock service so I Test it with nunit and nsubstitute

    namespace ReflectorLike.Tests
{
    [TestFixture]
    internal class ChatHubTester
    {

        private ChatHub hub;
        private ServiceHost host;
        private IChatService myMockedService;
        [SetUp]
        public void SetUp()
        {
            Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add<ServiceContractAttribute>();
            myMockedService = Substitute.For<IChatService>();
            host = MockServiceHostFactory.GenerateMockServiceHost(myMockedService, new Uri("http://localhost:12345"), "ServiceEndPoint");
            host.Open();
            hub=new ChatHub();
        }

        [TearDown]
        public void TearDown()
        {
            host.Close();
        }

        [Test]
        public void SomeTest()
        {

            hub.Connect("Test");
        }
    }
}


I wrote the blog post Hosting a Mock as a WCF service which you looked at to create MockServiceHostFactory.

Two things:

  1. You don't need to call the line to exclude the ServiceContractAttribute from being copied to the mock object, this is handled for you by NSubstitute.
  2. There must be some code that you have not given us, I have taken your existing code and run it locally and it mocks fine.

Here is the code that I have that works.

class Foo
{
    [Test]
    public void Should_work()
    {
        var myWcfServiceMock = Substitute.For<IChatService>();
        var mockServiceHost = MockServiceHostFactory.GenerateMockServiceHost(myWcfServiceMock , new Uri("http://localhost:8001"), "MyService");
        mockServiceHost.Open();
        mockServiceHost.Close();
    }

    public static class MockServiceHostFactory
    {
        public static ServiceHost GenerateMockServiceHost<TMock>(TMock mock, Uri baseAddress, string endpointAddress)
        {
            var serviceHost = new ServiceHost(mock, new[] { baseAddress });

            serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
            serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;

            serviceHost.AddServiceEndpoint(typeof(TMock), new BasicHttpBinding(), endpointAddress);

            return serviceHost;
        }
    }
}

Can you show any missing bits of code that might be important especially if you have simplified it for the purpose of asking the question? Can you include the stack trace that shows which line you actually get the error on?