Ambiguous reference in WCF and client application

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

General (class library):

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

Library (WCF service library) that has a link 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 link to Common, and a link to a service in the 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 automatically generated, and I did not modify them, and I did not change the default configuration for LibraryServiceReference in ClientApp.

When I compile this solution, I see 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 fix this.

+5
source share
8 answers

Make sure that 'Reuse types in all reference assemblies are selected in the Advanced Options Add Service Link or Configure Service Directory .

enter image description here

+4
source
  • Decorate your listing as follows:

     namespace Common { [DataContract] public enum PrimaryColor { [EnumMember] Red, [EnumMember] Green, [EnumMember] Blue }; } 
  • Update the link to the service (with verification of the types of reuse, as indicated in the statement of Mark).

  • Recover the client code.

+1
source

this is because you are building x64, not "AnyCpu". I am running through this right now, and trying to figure out if this is a bug or expected behavior.

+1
source

It looks like you are managing both the client and the server code. Why do you want to create a link to the service, is there a specific reason or is it simply considered easier?

In projects in which you control both sides of the client server application, you are better off creating a “contract assembly” (which is probably your common assembly). This contains the interfaces and objects associated with the contract and should be referenced by both your client and your server. To communicate with the service, the client creates a proxy class using ChannelFactory , there is no need to have a dedicated WCF client.

Example:

 ChannelFactory<ISampleService> factory = new ChannelFactory<ISampleService>("Binding_from_config"); ISampleService sampleService = factory.CreateChannel(); sampleService.SomeCall(); factory.Close(); 

The factory template also makes it an ideal candidate for entering your proxy server through IoC.

Benefits of linking to a general assembly for creating a service link:

  • There is no ambiguous reference because there is no need for automatically generated classes.
  • You do not need to update your service link every time you change a contract.
0
source

I have had this problem in innocuous, unpredictable manners so many times! I thought I would share how I “fixed” it for the last time.

I am using Visual Studio 2013, but I had a problem with rev.

The ambiguous link seems to be on its own. I did not notice anything to cause this. In the last instance, I was debugging some kind of code, and suddenly I had 7, then 22, and then 49 errors - all the same.

I completely deleted the service link and added it again. Simply changing the reuse type did nothing. My solution has WCF service, class library, user interface and management library. I also removed the use - in some code behind the class library.

This is an extremely troublesome problem, which, fortunately, occurs only every few weeks. Why did it work? Besides my salary. I feel your pain! Hope this helps. In this case, the error reappeared when I opened the code on the xaml page.

enter image description here

0
source

Whatever the cost, I ran into the same error after moving my data contracts to a separate library. I updated service links several times and tried all combinations of settings to reuse the assembly, but to no avail.

Ultimately, it was for me: 1) restart Visual Studio and 2) update the service link. The automatically generated code in Reference.cs in the service definition looked completely different and did not duplicate my data contract class. He used the correct link from my library. So something must be cached in the IDE.

Hope someone else finds this helpful.

0
source

The problem is that PrimaryColor exists in both Common and ClientApp.LibraryServiceReference, and you reference both namespaces in your class.

To overcome this problem, I explicitly refer to the required instance, i.e.

Common.PrimaryColor color = ....

or configure an alias:

using Service = ClientLibraryServiceReference; ...

Service.PrimaryColor color = ......

-1
source

When creating a help desk, there are some options that say something like: "include generic types in the generated service contract"?

I have an idea that in your link service classes are “copied” and why you get this error. Inspect the generated service files, delete them and add them again using the “Add Service Link” and see what options you have.

EDIT

Although I'm pretty sure Type PrimaryColor is defined twice. Once in a common project and once in your service a link, you can also try this in your clientApp (to more clearly indicate the type of PrimaryColor):

 Common.PrimaryColor color = client.GetColor(); 
-2
source

All Articles