Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous reference in WCF and client application

Tags:

wcf

ambiguous

I've managed to reproduce one of the errors in a test project with a similar structure to my production code. It consists of three simple projects:

Common (class library):

namespace Common
{
    public enum PrimaryColor
    {
        Red,
        Green,
        Blue
    };
}

Library (WCF service library), which has a reference to Common:

using Common;

namespace Library
{
    [ServiceContract]
    public interface ILibrary
    {
        [OperationContract]
        PrimaryColor GetColor();
    }

    public class Library : ILibrary
    {
        public PrimaryColor GetColor()
        {
            return PrimaryColor.Red;
        }
    }
}

ClientApp (console application), which has a reference to Common, and a service reference to Library called "LibraryServiceReference":

using Common;
using ClientApp.LibraryServiceReference;

namespace ClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            LibraryClient client = new LibraryClient("WSHttpBinding_ILibrary");
            PrimaryColor color = client.GetColor();
        }
    }
}

The app.config files in ClientApp and Library are auto-generated and I have not modified them, and I have not changed the default configuration for the LibraryServiceReference in ClientApp.

When I compile this solution, I get the following errors in the ClientApp project:

Error 1

'PrimaryColor' is an ambiguous reference between 'Common.PrimaryColor' and 'ClientApp.LibraryServiceReference.PrimaryColor'

Error 2

Cannot implicitly convert type 'ClientApp.LibraryServiceReference.PrimaryColor' to 'Common.PrimaryColor'. An explicit conversion exists (are you missing a cast?)

please help me to fix this.

like image 216
CuriousBenjamin Avatar asked Jun 18 '12 13:06

CuriousBenjamin


3 Answers

Make sure that 'Reuse types in all referenced assemblies' is selected in the Advanced options of Add service reference or Configure Service Reference.

enter image description here

like image 183
VJAI Avatar answered Sep 24 '22 05:09

VJAI


it's because you're building x64 not "AnyCpu". I am running across this right now, and am trying to figure out if it's a bug, or if it's expected behavior.

like image 41
eric frazer Avatar answered Sep 25 '22 05:09

eric frazer


  1. Decorate your enum like this:

    namespace Common
    {
        [DataContract]
        public enum PrimaryColor
        {
            [EnumMember]
            Red,
            [EnumMember]
            Green,
            [EnumMember]
            Blue
        };
    }
    
  2. Update Your service reference (with checking reuse types just like Mark stated).

  3. Rebuild your client code.

like image 22
Grzegorz W Avatar answered Sep 25 '22 05:09

Grzegorz W