views:

43

answers:

4

Hi,

I have a Silverlight 4 application where I am making use of shared classes. In the .Web project, I have a class called "X.Shared.cs". This class has three string properties. When I build the application, it gives an error saying "The type 'X' already contains a definition for 'Y'". It seems that the properties in the generated code in the Silverlight Application are being seen as duplicates. I have tried cleaning my solution and rebuilding, this helps every now and then but is totally inconsistent.

Has anyone had experience in this issue? Am I perhaps doing something wrong?

The code for the shared class (X.Shared.cs) is as follows:

public partial class VideoItem
{
    [Key]
    public String ID
    {
        get;
        set;
    }

    public String ImageURL
    {
        get;
        set;
    }

    public String URL
    {
        get;
        set;
    }
}

Here is a screenshot of the solution explorer showing the generated shared file: alt text

A: 

Have you checked that the class definition in X.Shared.cs is a partial?

i.e.

public partial class MyEntity
{
  ...
}
Rus
Thanks - it is marked as partial
Vixen
Do you have another file that defines VideoItem? In my solution in the .Web project my entity is defined in the MyDomainService.metadata.cs. In that file my entity has the [Key] etc. In the shared I don't have annotations and the properties are in addition to my main entity definition.
Rus
I've solved this by creating an empty .shared.cs class, and then creating another partial class to hold all the properties! This seems to have done the trick. I Cant see why this works - perhaps I dont have a proper understanding as to how the shared classes need to be used.
Vixen
A: 

Did you manually add a reference to the X.shared.cs files in your Silverlight app?

The copy is done automatically by web/client projects that are connected by RIA services. The files wind up in a hidden Generated_Code folder under your client app.

Turn on hidden files and see if you basically have the same file included twice in the Silverlight application.

Enough already
I havent manually added it as it should automatically be referenced because it has the extension .Shared.cs. When I turn on hidden files, I can see the generated code in the application
Vixen
@Vixen: That would be a no then :) We kind of have to guess as there is not much to go on. Rather than clean the project, actually delete all the bin and obj folders and try again (clean does not actually clean much). There is certainly nothing wrong with your actual class (builds fine in a RIA library here).
Enough already
Thanks for the help. Sadly deleting the bin and obj folders doesn't appear to have helped. The error message in the build is :[...]\Generated_Code\Models\VideoItem.shared.cs(24,23): error CS0102: The type '[...].Services.Web.Models.VideoItem' already contains a definition for 'URL'. Its as if its building the generated code first or something...
Vixen
C:\Workspace\App.MediaCenter\Dev\Integration001\Src\App.MediaCenter.Services\Generated_Code\Models\VideoItem.shared.cs(12,23): error CS0102: The type 'App.MediaCenter.Services.Web.Models.VideoItem' already contains a definition for 'ID'C:\Workspace\App.MediaCenter\Dev\Integration001\Src\App.MediaCenter.Services\Generated_Code\App.MediaCenter.Services.Web.g.cs(422,23): (Related location)
Vixen
@Vixen: When this sort of thing happens, we generally add a new class or change the class name or member name (in this case URL). Sometimes we never find the cause, but work around it instead.
Enough already
This is looking like the only option right now, but its certainly not maintainable. If I have to rename / re-add the class with a new name whenever this happens I this myself and the rest of my team might lose our minds :)
Vixen
+1  A: 

To solve this problem, I created a blank .shared.cs class containing no properties (this is obviously a partial class). I then created another partial class in the same namespace with that class name and in here I put all the properties I needed to access.

Vixen
@Vixen: It is bizarre you had to do that. I would like to have gotten to the bottom of the cause as I have not seem this problem. +1 for creative thinking :)
Enough already
Thanks and thanks for the help! If you run into the problem in the future at least you can use this solution! Would love to understand it though...
Vixen
A: 

There is a limitation in the code generation. The to avoid generating duplicate members in Shared or Linked files the members can not use Auto-Implemented Properties. So to work around this you just have to define your own get and set. Keep in mind there should be a good reason not to let the RIA code generation create the members. To let RIA create the members, simply don't link or share your entity with the client but expose it through a Query method.

From http://msdn.microsoft.com/en-us/library/ee707359%28VS.91%29.aspx

Avoiding Duplicated Members

When generating an entity proxy class, it is possible that the same type and member have already been defined in the client project by using partial types. You may have defined the member in shared code or in code that only exists in the client project. RIA Services checks the existing members before generating the proxy class. Any member that is already defined will not be generated in the proxy class. [Not included - Entity members must not use Auto-Implemented Properties. Shared and Linked files from server to client project is the only method to avoid generating the member.]

JosephC