What Was I Thinking?

Follies & Foils of .NET Development
posts - 87 , comments - 255 , trackbacks - 0

Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

I recently encountered this error when I attempted to consume a new hosted WCF service.  The service used the Request/Response model and had been properly decorated.  The response and request objects were marked as DataContracts and had a specified namespace.   My WCF service interface was marked as a ServiceContract and shared the namespace attribute value.   Everything should have been fine, right?

[ServiceContract(Namespace = "http://schemas.myclient.com/09/12")]
   public interface IProductActivationService
   {
       [OperationContract]
       ActivateSoftwareResponse ActivateSoftware(ActivateSoftwareRequest request);
   }

well, not exactly.  Apparently the WSDL generator was having an issue:

System.Xml.Schema.XmlSchemaException: The global element 'http://schemas.myclient.com/09/12:ActivateSoftwareResponse' has already been declared.

After digging I’ve found the problem; the WSDL generator has some reserved suffixes for its entities, including Response, Request, Solicit (see http://msdn.microsoft.com/en-us/library/ms731045.aspx).  The error message is actually the result of a naming conflict.  The WSDL generator uses the namespace of the service to build its reserved types.  The service contract and data contract share a namespace, which coupled with the response/request name suffixes I was using in my class names, resulted in the SchemaException.

The Fix:

Two options:

  1. Rename my data contract entities to use a non-reserved keyword suffix (i.e.  change ActivateSoftwareResponse to ActivateSoftwareResp). or;
  2. Change the namespace of the data contracts to differ from the service contract namespace.

I chose option 2 and changed all my data contracts to use a “http://schemas.myclient.com/09/12/datanamespace value. This avoided a name collision and I was able to produce my WSDL and consume my service.

Print | posted on Wednesday, April 14, 2010 10:01 AM | Filed Under [ Visual Studio WCF ]

Feedback

Gravatar

# re: Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

Page http://msdn.microsoft.com/en-us/library/ms731045.aspx doesn't mention suffixes Response, Request, Solicit,
but http://www.w3.org/TR/wsdl#_names does
11/17/2011 6:59 PM | Michael Freidgeim
Gravatar

# re: Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

Note that the first comment by Clean Registry is spam.
11/17/2011 7:18 PM | Michael Freidgeim
Gravatar

# re: Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

Good job telling the solution of a problem you encountered. This will help alot of people.

I hope you do the same for other problems :).
1/10/2012 10:55 AM | Jonas
Gravatar

# re: Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

Awesome, solved my problem, nice one :)
2/15/2012 10:23 AM | Steve Wilkes
Gravatar

# re: Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

thank you very much. This help me a lot.
12/1/2013 9:05 PM | luoyong
Gravatar

# re: Solving “XmlSchemaException: The global element '<elementName>' has already been declared.”

Very nice to post the solution to your problem !
It helped me fix mine!
Thanks!!
4/24/2014 6:13 AM | Haly
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: