WCF InvalidOperationException: A binding instance has already been associated to listen URI

I'm a beginner of WCF and I'm studying in Essential WCF.

I encountered a problem when using ServiceContract NameSpace and Name. when I run the code, I catch a bellow InvalidOperationException. But I couldn't understand clearly.

A binding instance has already been associated to listen URI 'http://localhost:8080/NamespaceChange01'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config.

Does anyone knows how to avide the InvalidOperationException ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace NamespaceChange01
{

    [ServiceContract(Name = "MyServiceName", Namespace = "http://ServiceNamespace")]
    public interface IBurgerMaster
    {
        [return: MessageParameter(Name = "myOutput")]
        [OperationContract(Name = "OperationName", Action = "OperationAction", ReplyAction = "ReplyActionName")]
        double GetStockPrice(string ticker);
    }

    [ServiceBehavior(Namespace = "http://MyService")]
    public class BurgerMaster : IBurgerMaster
    {

        public double GetStockPrice(string ticker)
        {
            return 100.99;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(BurgerMaster));
            host.Open();
            Console.ReadLine();
            host.Close();
        }
    }
}
  • app.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="NamespaceChange01.BurgerMaster" behaviorConfiguration="mexServiceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/NamespaceChange01"/>
              </baseAddresses>
            </host>
            <endpoint name="basic" binding="basicHttpBinding" contract="NamespaceChange01.IBurgerMaster"/>
            <endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mexServiceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    

Thanks.

Answers


Two endpoints (basic and mex) couldn't be on the same address. Add some specific address for one of them (or for both ones).

For example:

<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

You're missing the address attribute from your metadata endpoint:

<endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange" address="mex" />

Without it WCF thinks you want to host the mex endpoint at the same address.


While creating your service class, why did you mark it with ServiceContract attribute as your code says?

[ServiceBehavior(Namespace = "http://MyService")]
public class BurgerMaster : IBurgerMaster

Please remove that one and try again.


Need Your Help

Parcelable and inheritance in Android

java android design-patterns inheritance parcelable

I got an implementation of Parcelable working for a single class that involves no inheritance. I have problems figuring out the best way to implement the interface when it come to inheritance. Let'...

Java NullPointerException when adding to ArrayList?

java arraylist nullpointerexception

My code is throwing a NullPointerException, even though the object seems to properly exist.