Wednesday, June 22, 2011

Error on Adding WCF Service Reference to ASP.Net Project

I am writing a small website using EF 4 Code First. There are some legacy tables which don't have primary key. This (not having PK) would have created complications with EF, but I needed to only read data from those table. So I decided to create a WCF service to pull the data. I defined [ServiceContract], [DataContract], [OperationContract] etc. and everything was going fine till I tried add reference to my web project. In the Add Service Reference dialog VS did find service, but when I clicked on service to fetch metadata I got following error:

There was an error downloading 
'http://localhost:8732/Design_Time_Addresses/LegacyDataService/DataService/mex'.
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 
http://localhost:8732/Design_Time_Addresses/LegacyDataService/DataService/mex.

After some head scratching and googling about the error I found out a post on MSDN forum where somebody was putting [DataMember] attribute over an indexer. I checked my [ServiceContract] and [DataContract]s.

My [ServiceContract] read as follows:

[ServiceContract]
public interface IDataService
{
    [OperationContract]
    IList<LegacyUser> GetUsers();
    ......
    ......
}

I tried to comment the IList<LegacyUser> stuff, but the error was still there. It then occurred to me that there was an Address property which returned build user's address by concatenating other fields. After this Address property was commented the error was gone.

Moral of the story is that:
  1. you should not put [DataMember] attribute on Indexers (as mentioned in MSDN post).
  2. you should not put [DataMember] attribute on properties that use other properties with the attribute to calculate return value.

No comments: