3

I'm new to WCF and C#.

I'm trying to create a WCF Service with 1 Interface (IA) with 1 method (Do) which has 2 implementations (A1 & A2).

A silly example:

IA.cs:

namespace IA_NS
{
    [ServiceContract]
    public interface IA
    {
        [OperationContract]
        int Do(B b);
    }

    [DataContract]
    public class B
    {
        [DataMember]
        public string b1 { get; set; }
    }
}

A1.cs:

namespace A1_NS
{
    public class A1 : IA
    {
        public int Do(B b) {...}
    }
}

A2.cs:

namespace A2_NS
{
    public class A2 : IA
    {
        public int Do(B b) {...}
    }
}

My Self-Hosting-Console, where I host both services:

class Program
{
    static void Main(string[] args)
    {
        ServiceHost hostA1 = new ServiceHost(typeof(A1_NS.A1));
        hostA1.Open();
        ServiceHost hostA2 = new ServiceHost(typeof(A2_NS.A2));
        hostA2.Open();
        Console.WriteLine("Hit any key to exit...");
        Console.ReadLine();
        hostA1.Close();
        hostA2.Close();
    }
}

I want my WCF Client to be able to call both classes:

Client.cs:

namespace Client_NS
{
    class Program
    {
        static void Main(string[] args)
        {
            B myB = new B();
            A1 a1 = new A1();
            A2 a2 = new A2();
            A1.Do(myB);
            A2.Do(myB);
        }
    }
}

No luck ;-(

I tried putting 2 elements in my WCF-Service app.config:

<service name="A1_NS.A1" >
    <endpoint address="http://localhost/A1"
              contract="IA_NS.IA" />
</service>
<service name="A2_NS.A2" >
    <endpoint address="http://localhost/A2"
              contract="IA_NS.IA" />
</service>

When running the debugger - the debugging app (WCF Service Host) lets me test the Do() method of both classes.

I can't get my Client to do it. I added Service References for both services. Is it the Client app.config or am I misunderstanding something?

Rami Rosenbaum
  • 467
  • 5
  • 18
  • Is your error actually reading "No luck ;-("? Because without knowing what your problem is, it's hard to suggest a solution. Please be more precise regarding the problem you have. – nvoigt Sep 12 '13 at 05:33
  • please post the code of where you initialize the wcf services in the client. most likely where your problem is ... – Omri Btian Sep 12 '13 at 06:06
  • nvoigt - "No luck" means I would appreciate some guidance as of where to look and what to post. – Rami Rosenbaum Sep 12 '13 at 09:00
  • @RamiRosenbaum post the code in static void Main(string[] args) { ... B myB; where your client connects to the WCF services ... – Omri Btian Sep 12 '13 at 09:06
  • Omribitan - I'm not sure I'm using the WCF correctly. Is using 2 elements correct? Or 2 elements? To declare an object of class B I need to specifically choose WCFClient.ServiceReferenceA1.B or WCFClient.ServiceReferenceA2.B. Give me a minute, I'll create a project with those names – Rami Rosenbaum Sep 12 '13 at 09:10
  • @RamiRosenbaum There is no problem using 2 services that implement the same contract. there is also no problem using two different endpoints for the two different services. If you are using WCF with Service References, each service should have a unique name so you can contact it. when you create each reference visual studio automatically produces different endpoints for each service. If you understand hebrew (My guess by your name), I suggest you read this post : http://webmaster.org.il/articles/wcf-communicate-with-service – Omri Btian Sep 12 '13 at 09:19
  • I've added the Host code and updated the Client code. My problem is that B (of the client) code is ambiguous: error CS0104: 'B' is an ambiguous reference between 'WCFClient.ServiceReferenceA1.B' and 'WCFClient.ServiceReferenceA2.B' – Rami Rosenbaum Sep 12 '13 at 09:56
  • Omribitan - Thanks, I'll have a look now. גמר חתימה... – Rami Rosenbaum Sep 12 '13 at 09:57
  • I think this [post](http://stackoverflow.com/questions/2327137/multiple-wcf-services-referencing-the-same-data-contracts) is a better description of my problem: 2 services referencing the same data contract. The solutions are not elegant enough... – Rami Rosenbaum Sep 12 '13 at 11:05

2 Answers2

1

You could implement partial classes that allow you to separate your content in individual cs files while maintaing a single interface and endpoint. This isn't the most ideal way, because at the end of the day it is still a single class made up of partial classes, but at least it looks like it in your file structure, thus giving some separation rather than a massive class file.

Example Structure:

IMyService.cs

[ServiceContract]
public interface IMyService
{
   [OperationContract]
   string GenericMethod()

   [OperationContract]
   string GetA(int id)

   [OperationContract]
   string GetB(int id)

}

MyService.cs

//Put any attributes for your service in this class file
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class MyService : IMyService
{
  public string GenericMethod() 
  {
     return "";
  }
}

AService.cs

public partial class MyService
{
    public string GetA(int id) 
    {
       return "";
    }
}

BService.cs

public partial class MyService
{
      public string GetB(int id) 
      {
          return "";
      }
}
Gaff
  • 5,507
  • 3
  • 38
  • 49
0

I see contract twice (under A2_NS.A2 service tag), is that by mistake or if it's really there, then remove 2nd one(after closing endpoint tag) and see if this was related to issues you were getting?

contract="IA_NS.IA" />
    contract="IA_NS.IA" />
LearningNeverEnds
  • 374
  • 1
  • 3
  • 22
  • 1
    That was a typo, fixed, thanks. I learned that my problem was actually: [2 services referencing the same data contract](http://stackoverflow.com/questions/2327137/multiple-wcf-services-referencing-the-same-data-contracts) – Rami Rosenbaum Sep 13 '13 at 12:27