Search

Loading...

Mar 16, 2010

Two WCF Sharing same a Data Contract - Ambiguous Error

[Problem: Ambiguous reference]

Hello All,

I came across one common issue while working with WCF services, Two WCF exposing same data contract with different namespace. Here are my two service contract with operation contracts.

namespace SVCDataContractTest.WCF
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void DoWork(UserInfo userInfo);

[OperationContract]
void SaveContact(ContactInfo contactInfo);

}

public class Service1 : IService1
{
public void DoWork(UserInfo userInfo)
{
//Do some work
}

public void SaveContact(ContactInfo contactInfo)
{
//Do some work
}
}

[ServiceContract]
public interface IService2
{
[OperationContract]
UserInfo DoWork();
}

public class Service2 : IService2
{
public UserInfo DoWork()
{
//Do some work
return new UserInfo();
}
}
}

While adding them as reference it will generate two separate class having different namespace, we are good here as there won't be any compilation error if a requester don't import both namespace.



You can see there two UserInfo classes. Now problem comes when you imported both of the namespace, what happen to your class? it will start giving you following error.



It says that UserInfo is an ambiguous, as there are two set of classes resides in reference. The problem is when you add Service reference it's not treating UserInfo class as Shared class. Solution for this is to create Proxy with Type Sharing. We can use wsdl.exe to create proxy with shareType switch

wsdl.exe /out:ServiceProxies.cs /shareTypes http://localhost/SVCDataContractTest.WCF/Service1.svc?wsdl http://localhost/SVCDataContractTest.WCF/Service2.svc?wsdl

This will result in one class file called ServiceProxies, which contains the proxy implementation of both service but the implementation of UserInfo will be single.

ProxyCD1

1 comment:

Anonymous said...

Hi Men thanks for your post .. I use this topic in my thesis :)