简体   繁体   English

WCF服务参考 - 获取“XmlException:名称不能以'<'字符开头,十六进制值0x3C”在客户端

[英]WCF Service Reference - Getting “XmlException: Name cannot begin with the '<' character, hexadecimal value 0x3C” on Client Side

I have a smart client application communicating with its server via WCF. 我有一个智能客户端应用程序通过WCF与其服务器通信。 Data is created on the client and then sent over the service to be persisted. 数据在客户端上创建,然后通过服务发送以进行持久化。 The server and client use the same domain classes via a shared dll and I'm using the handy "Add Service Reference" functionality in Visual Studio that wraps SvcUtil.exe and generates the client and proxy classes. 服务器和客户端通过共享dll使用相同的域类,我在Visual Studio中使用方便的“添加服务引用”功能包装SvcUtil.exe并生成客户端和代理类。

I get the following error when trying to call the service: 尝试调用服务时出现以下错误:

System.Xml.XmlException occurred
  Message=Name cannot begin with the '<' character, hexadecimal value 0x3C.
  Source=System.Xml
  LineNumber=0
  LinePosition=1
  StackTrace:
       at System.Xml.XmlConvert.VerifyNCName(String name, ExceptionType exceptionType)
  InnerException: 

This is particularly troublesome because the service will work for weeks at a time without this error occurring and then suddenly and without warning it will appear again. 这特别麻烦,因为该服务一次可以工作数周而不会发生此错误,然后突然且没有警告它将再次出现。 I haven't been able to figure out what causes it at all. 我根本无法找出导致它的原因。 When it does happen, I will do a deep dive into researching how to fix it and usually don't come up with anything more than people who have experienced the same error while actually trying to serialize things to xml programmatically. 当它确实发生时,我会深入研究如何解决它,并且通常不会提出任何比实际尝试以编程方式将事物序列化为xml而遇到相同错误的人。 I am using nothing but the generated client and proxies to try and send this data. 我只使用生成的客户端和代理来尝试发送此数据。

I have looked into the generated proxies in the Service References\\AwesomeService folder of my solution and see nothing that looks out of the ordinary. 我已经查看了我的解决方案的Service References\\AwesomeService文件夹中生成的代理,看不出任何看起来不寻常的东西。 The only occurrences of angle brackets in the generated files are: 生成的文件中唯一出现的尖括号是:

  • xml tags in the *.svcinfo, Reference.svcmap, AwesomeService.disco, AwesomeService.wsdl and *.xsd files * .svcinfo,Reference.svcmap,AwesomeService.disco,AwesomeService.wsdl和* .xsd文件中的xml标记
  • arguments to generic collections in the Reference.cs file Reference.cs文件中泛型集合的参数

The code I'm using to call the service is such: 我用来调用服务的代码是这样的:

using (var client = new AwesomeServiceClient())
{
    client.SaveAwesomeness(instanceOfAwesomeness);
}

This is the stack beggining with the first frame above the calling code listed above: 这是在上面列出的调用代码上方的第一帧开始的堆栈:

System.Xml.dll!System.Xml.XmlConvert.VerifyNCName(string name, System.Xml.ExceptionType exceptionType) + 0xb5 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.IsValidNCName(string name) + 0x27 bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.EncodeLocalName(string localName) + 0x1d bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.ImportDataMembers() + 0x2e1 bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper.ClassDataContractCriticalHelper(System.Type type) + 0x10d bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(int id, System.RuntimeTypeHandle typeHandle, System.Type type) + 0x198 bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(int id, System.RuntimeTypeHandle typeHandle, System.Type type) + 0x57 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(int id, System.RuntimeTypeHandle typeHandle) + 0x37 bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) + 0x49 bytes    
[Lightweight Function]  
System.Runtime.Serialization.dll!System.Runtime.Serialization.ClassDataContract.WriteXmlValue(System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.Runtime.Serialization.XmlObjectSerializerWriteContext context) + 0x25 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) + 0x18 bytes   
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, object obj, System.RuntimeTypeHandle declaredTypeHandle) + 0x49 bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0xdf bytes  
System.Runtime.Serialization.dll!System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0x26 bytes 
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(System.Runtime.Serialization.XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) + 0x60 bytes    
System.Runtime.Serialization.dll!System.Runtime.Serialization.XmlObjectSerializer.WriteObject(System.Xml.XmlDictionaryWriter writer, object graph) + 0x2d bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo part, object graph) + 0x38 bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo part, object graph) + 0xbe bytes 
System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo[] parts, object[] parameters) + 0x3e bytes  
System.ServiceModel.dll!System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion version, string action, System.ServiceModel.Description.MessageDescription messageDescription, object returnValue, object[] parameters, bool isRequest) + 0x68 bytes   
System.ServiceModel.dll!System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion version, object[] parameters, object returnValue, bool isRequest) + 0x7b bytes   
System.ServiceModel.dll!System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer) + 0x4f bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.BodyWriter.WriteBodyContents(System.Xml.XmlDictionaryWriter writer) + 0xf8 bytes   
System.ServiceModel.dll!System.ServiceModel.Channels.BodyWriterMessage.OnBodyToString(System.Xml.XmlDictionaryWriter writer) + 0x1f bytes   
System.ServiceModel.dll!System.ServiceModel.Channels.Message.ToString(System.Xml.XmlDictionaryWriter writer) + 0xaa bytes   
System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogTraceRecord.WriteTo(System.Xml.XmlWriter writer) + 0x166 bytes    
System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogger.LogInternal(System.ServiceModel.Diagnostics.MessageLogTraceRecord record) + 0x77 bytes    
System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogger.LogMessageImpl(ref System.ServiceModel.Channels.Message message, System.Xml.XmlReader reader, System.ServiceModel.Diagnostics.MessageLoggingSource source) + 0x104 bytes  
System.ServiceModel.dll!System.ServiceModel.Diagnostics.MessageLogger.LogMessage(ref System.ServiceModel.Channels.Message message, System.Xml.XmlReader reader, System.ServiceModel.Diagnostics.MessageLoggingSource source) + 0x3a bytes   
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.PrepareCall(System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, bool oneway, ref System.ServiceModel.Dispatcher.ProxyRpc rpc) + 0x436 bytes 
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.Call(string action, bool oneway, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, object[] ins, object[] outs, System.TimeSpan timeout) + 0x12b bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage methodCall, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation) + 0x64 bytes    
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage message) + 0x6a bytes    
mscorlib.dll!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref System.Runtime.Remoting.Proxies.MessageData msgData, int type) + 0xee bytes

What causes this and how do I prevent it? 是什么导致这种情况,我该如何预防? Or, also welcomed, how do I go about troubleshooting this further? 或者,也欢迎,我该如何进一步解决这个问题?

Have a look at your DataTables (if that's what you are using to transport data). 看看你的DataTables(如果你用它来传输数据)。

If the DataTable name is empty, then the Serializer might get confused and serialise things incorrectly. 如果DataTable名称为空,则Serializer可能会混淆并错误地序列化。

Otherwise, if you are using typed, [Serializable] objects, I have found that sometimes the Serializer also gets confused if you use dynamic property declarations, eg: 否则,如果您使用的是类型化的[Serializable]对象,我发现如果您使用动态属性声明,有时串行器也会混淆,例如:

public string MyName { get; set; }

But this would be a easily repeatable error. 但这将是一个容易重复的错误。

Personnally, I got the same problem with serialization of class hierarchy (not DataTables). 个人而言,我在类层次结构的序列化(而不是DataTables)中遇到了同样的问题。

My problem was not related to automatic property at all, in fact I have many. 我的问题根本与自动财产无关,事实上我有很多。 My problem was that I forgot to include reference to " System.Runtime.Serialization " in one of my dll and I also forgot to add some attributes [DataContract] on some classes referenced by upper [DataMember] attributes up in the hierarchy. 我的问题是我忘了在我的一个dll中包含对“ System.Runtime.Serialization ”的引用,我也忘了在层次结构中的upper [DataMember]属性引用的某些类上添加一些属性[DataContract]

To track my problem I started from my root class and removed some [DataMember] down the hierarchy until it pointed out the exact problem. 为了跟踪我的问题,我从我的根类开始,并从层次结构中删除了一些[DataMember] ,直到它指出了确切的问题。 It could takes some times depending on your hierarchy levels... 根据您的层次结构级别,可能需要一些时间...

Hope it helps! 希望能帮助到你! Eric 埃里克

A similar error tripped me up, but it turned out my config file (actually a clientconfig file for silverlight) contained the following 类似的错误使我失望,但事实证明我的配置文件(实际上是silverlight的clientconfig文件)包含以下内容

   <<security mode="Transport">
       <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
       <message clientCredentialType="Certificate" algorithmSuite="Default" />
   </security>

So sometimes messages about extra < characters should be taken literally! 因此,有时候应该从字面上理解关于额外<字符的消息!

Either use full properties with [Serializable] , or use [DataContract] and [DataMember] . 使用[Serializable]完整属性,或使用[DataContract][DataMember]

The following was giving me an error, probably because .Net was creating a backing variable under the hood, with some character that the XmlSerializer didn't like. 以下是给我一个错误,可能是因为.Net在引擎盖下创建了一个支持变量,其中有一些XmlSerializer不喜欢的特性。

[Serializable]
public class MyClass
{
    public int MyValue { get; private set; }
    ...
}

Either create full properties 创建完整属性

[Serializable]
public class MyClass
{
    int _myValue;
    public int MyValue
    {
        get { return _myValue; }
        private set { _myValue = value; }
    }
    ...
}

Or use the DataContract and DataMember attributes 或者使用DataContractDataMember属性

[DataContract]
public class MyClass
{
    [DataMember]
    public int MyValue { get; private set; }
    ...
}

In my case, one of the classes had a property, whose datatype was object. 在我的例子中,其中一个类有一个属性,其数据类型是object。 Something like this: 像这样的东西:

public class BuyAddOnServiceRequest
{
    object site_id
}

after changing this to: 将此更改为:

public class BuyAddOnServiceRequest
{
    string site_id
}

it worked! 有效!

Ok just came across another scenario on this one. 好的,刚刚遇到了另一个场景。 I had a Serializable type used as a parameter for one of my Operation Contract methods. 我有一个Serializable类型用作我的一个Operation Contract方法的参数。

Commenting out this specific method from use allowed me to find the issue. 从使用中评论这个特定的方法让我找到了问题。 In this case, the parameter was a model deserialized from a file, so I just replaced the implementation with a parameter of byte[] and ran deserialization logic at the other end. 在这种情况下,参数是从文件反序列化的模型,所以我只是用byte []参数替换了实现,并在另一端运行反序列化逻辑。

While not necessarily an answer for all, in the case of parameter types on your Operation Contract methods that are Serializable, you may run into this exception also. 虽然不一定是所有人的答案,但对于可操作合同方法的参数类型是Serializable,您也可能遇到此异常。 I would imagine decorating them with correct DataContract attributes would assist in rectifying this problem. 我想用正确的DataContract属性来装饰它们将有助于纠正这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称不能以 '&lt;' 字符十六进制值 0x3c 开头 - Name cannot begin with the '<' character hexadecimal value 0x3c XDocument错误名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C - XDocument Error Name cannot begin with the '<' character, hexadecimal value 0x3C 获取System.Xml.XmlException:名称不能以&#39;&#39;字符开头,十六进制值为0x20。 第42行,位置36 - Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36 &#39;&lt;&#39;,十六进制值 0x3C,是无效的属性字符 - '<', hexadecimal value 0x3C, is an invalid attribute character 名称不能以&#39;&#39;字符开头,十六进制值0x20 - Name cannot begin with the ' ' character, hexadecimal value 0x20 DataContractSerializer-名称不能以“。”开头 字符,十六进制值0x2E - DataContractSerializer - Name cannot begin with the '.' character, hexadecimal value 0x2E System.Xml.XmlException: &#39;:&#39; 字符,十六进制值 0x3A,不能包含在名称中 - System.Xml.XmlException: The ':' character, hexadecimal value 0x3A, cannot be included in a name System.Xml.XmlException:“‘:’字符,十六进制值 0x3A,不能包含在名称中。” - System.Xml.XmlException: „The ':' character, hexadecimal value 0x3A, cannot be included in a name.” 名称不能以“1”字符开头,十六进制值0x31。 从xml文件中读取时 - Name cannot begin with the '1' character, hexadecimal value 0x31. while reading from an xml file 名称中不能包含&#39;/&#39;字符十六进制值0x2F的XmlException - XmlException for the '/' character hex value 0x2F cannot be included in a name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM