0

I want to create a class that implementing 2 service contracts, and exposing 2 end-points on different ports (I get the ports as input to the program).

My question is very similar to this question, but I want two end-points each one on different port.

EDIT:

Using this code:

var aConnectionString = "http://localhost:8200/A";
var bConnectionString = "http://localhost:8201/B";

ServiceHost host= new ServiceHost(typeof(abImp));

var binding = new BasicHttpBinding();

host.AddServiceEndpoint(typeof(A), binding, aConnectionString);
host.AddServiceEndpoint(typeof(B), binding, bConnectionString);

{
    // Check to see if the service host already has a ServiceMetadataBehavior
    ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();

    // If not, add one
    if (smb == null)
        smb = new ServiceMetadataBehavior();

    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);


    host.AddServiceEndpoint(
        ServiceMetadataBehavior.MexContractName,
        MetadataExchangeBindings.CreateMexHttpBinding(),
        aConnectionString
        );

}
host.Open();

I'm getting this error message on the host.Open() operation:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll

Additional information: A binding instance has already been associated to listen URI 'http://localhost:8200/A'. 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.

SOLUTION

            var aConnectionString = "http://localhost:" + portA+ "/A";
            var bConnectionString = "http://localhost:" + portB+ "/B";


            ServiceHost aHost = new ServiceHost(instance, new Uri(aConnectionString));
            ServiceHost bHost = new ServiceHost(instance, new Uri(bConnectionString));

            aHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;
            bHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;

            aHost.AddServiceEndpoint(typeof(A), new BasicHttpBinding(), aConnectionString);
            bHost.AddServiceEndpoint(typeof(B), new BasicHttpBinding(), bConnectionString);


            {
                // Check to see if the service host already has a ServiceMetadataBehavior
                var smb = aHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior();
                aHost.Description.Behaviors.Add(smb);

                aHost.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    aConnectionString + "/Mex"
                    );
            }
            {
                // Check to see if the service host already has a ServiceMetadataBehavior
                var smb = bHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior();
                bHost.Description.Behaviors.Add(smb);

                bHost.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    bConnectionString + "/Mex"
                    );
            }

            aHost.Open();
            bHost.Open();
Community
  • 1
  • 1
user3343396
  • 725
  • 2
  • 13
  • 25
  • Have a look at this: http://stackoverflow.com/a/16501801/594832 – Jan Köhler Dec 30 '14 at 11:07
  • You'll need to add an MetadataExchange (MEX) endpoint: http://msdn.microsoft.com/en-us/library/aa738489%28v=vs.110%29.aspx – Jan Köhler Dec 30 '14 at 11:46
  • See my edited question. What am I doing wrong? – user3343396 Dec 30 '14 at 12:02
  • The error is still the same? – Jan Köhler Dec 30 '14 at 12:16
  • No, sorry. I forgot to update it. now it's updated. – user3343396 Dec 30 '14 at 13:31
  • I assume A points now to `http://localhost:8200/Offers`. Make sure that your MEX endpoint points to another URI. It seems it points to the same: `MetadataExchangeBindings.CreateMexHttpBinding(), aConnectionString );` – Jan Köhler Dec 30 '14 at 13:35
  • using aConnectionString + "/Mex" works (if I remove smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;) but WcfTestClient shows both endpoints, not only A's endpoint (which is what I want). – user3343396 Dec 30 '14 at 16:21
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 04 '15 at 21:48

1 Answers1

0

Given that you want two completely different addresses, only same implementation class, I'd recommend use of two different ServiceHost instances.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
  • Thanks for the reply. But, as I wrote in my question, I need those two services on different ports. – user3343396 Dec 30 '14 at 19:11
  • @user3343396 If you explicitly want the service on completely different uris, then why not just use two different `ServiceHost`. You want same implementation class, which will still be the case. Is there any reason why you'd want to do this with only and exactly one `ServiceHost` instance? – Vikas Gupta Dec 30 '14 at 19:54
  • Because I want the concrete class to be singleton. – user3343396 Dec 30 '14 at 22:25
  • In that case, you can and see if following works with *two* instances of `ServiceHost`, but singleton instance of your service class. - http://msdn.microsoft.com/en-us/library/ms585487(v=vs.110).aspx – Vikas Gupta Dec 30 '14 at 23:15