Search code examples
.netvisual-studio-2010dynamics-crm-2011odata

Bypassing member names cannot be the same as their enclosing type


Similar to: member names cannot be the same as their enclosing type except I can't change the metadata, I want to just ignore the class entirely.

I am trying to add a Service Reference in Visual Studio 2010 but I get the following error.

The custom tool ‘DataServiceClientGenerator’ failed. Data service client code-generation failed: Schema specified is not valid. Errors: (0,0) : error 0042: Name foo cannot be used in type Microsoft.Crm.Sdk.Data.Services.foo. Member names cannot be the same as their enclosing type..

I understand that this error is because someone named a field foo in the object foo but I can't change that..

so without being able to make any changes within CRM, how can I generate my service reference?

I have thought of downloading the $metadata and then removing the offending type and then saving it (as the type is not one that I will be using anyway). But I do not know how to get this to work because it's not overly straight forward.

I have tried making the service reference with both C# and VB.NET and neither worked, ideally it would be a solution that works for both, but at this point I would be open to a solution that only works for one of these.


Solution

  • I have worked out how to resolve this issue.

    http://localhost/myWcfDataService.svc/$metadata
    

    Save this result to disk such as c:\metadata.csdl

    Then manually edit the file and remove (or rename) the offending field (this will result in it not being usable in your service, but it is better than the whole service not working).

    Then run the following:

    c:\Windows\Microsoft.NET\Framework\v4.0.30319\DataSvcUtil.exe /language:CSharp /in:c:\metadata.csdl /out:c:\serviceReference1.cs
    

    Now add the newly generated serviceReference1.cs file to your visual studio project, look for the class that inherits from System.Data.Services.Client.DataServiceContext this is your service entry point.

    Further information can be found here.