WCF服务参考-获取&;XmlException:名称不能以'<'字符,十六进制值0x3C"在

本文关键字:字符 十六进制 0x3C quot 参考 服务 获取 XmlException WCF 不能 | 更新日期: 2023-09-27 18:04:52

我有一个智能客户端应用程序通过WCF与其服务器通信。数据在客户机上创建,然后通过要持久化的服务发送。服务器和客户端通过共享的dll使用相同的域类,我使用Visual Studio中方便的"添加服务引用"功能,该功能包装SvcUtil.exe并生成客户端和代理类。

当尝试调用服务时,我得到以下错误:

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: 

这是特别麻烦的,因为服务将工作数周的时间没有这个错误发生,然后突然,没有警告它会再次出现。我还没弄清楚是什么引起的。当它确实发生时,我将深入研究如何修复它,并且通常不会想出比那些在实际尝试以编程方式将内容序列化到xml时遇到相同错误的人更多的东西。我只使用生成的客户端和代理来尝试发送此数据。

我已经查看了我的解决方案的Service References'AwesomeService文件夹中生成的代理,没有看到任何异常。在生成的文件中出现尖括号的唯一情况是:

  • xml标签中的*。svcinfo,参考。svcmap AwesomeService。迪斯科,AwesomeService。WSDL和*。xsd文件
  • 在Reference.cs文件
  • 中给泛型集合的参数
我用来调用服务的代码如下:
using (var client = new AwesomeServiceClient())
{
    client.SaveAwesomeness(instanceOfAwesomeness);
}

这是从上面列出的调用代码上方的第一帧开始的堆栈:

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

是什么原因导致的,我如何预防它?或者,我该如何进一步进行故障排除?

WCF服务参考-获取&;XmlException:名称不能以'<'字符,十六进制值0x3C"在

看看你的数据表(如果这是你用来传输数据的)。

如果DataTable名称为空,那么序列化器可能会混淆并错误地序列化内容。

否则,如果你使用类型化,[Serializable]对象,我发现如果你使用动态属性声明,有时序列化器也会混淆,例如:

public string MyName { get; set; }

就我个人而言,我在序列化类层次结构(不是datatable)时遇到了同样的问题。

我的问题与自动属性无关,事实上我有很多。我的问题是,我忘记在我的一个dll中包括对"System.Runtime.Serialization"的引用,我也忘记在层次结构中添加一些由上层[DataMember]属性引用的类上的一些属性[DataContract]

为了跟踪我的问题,我从我的根类开始,并从层次结构中删除了一些[DataMember],直到它指出确切的问题。这可能需要一些时间,这取决于您的层次结构级别…

希望有帮助!Eric

一个类似的错误绊倒了我,但它证明我的配置文件(实际上是silverlight的clientconfig文件)包含以下

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

所以有时候关于额外<字符的消息应该从字面上理解!

要么使用[Serializable]的全部属性,要么使用[DataContract][DataMember]

下面给了我一个错误,可能是因为。net正在创建一个后台变量,其中包含XmlSerializer不喜欢的一些字符。

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

创建完整的属性

[Serializable]
public class MyClass
{
    int _myValue;
    public int MyValue
    {
        get { return _myValue; }
        private set { _myValue = value; }
    }
    ...
}
或者使用DataContractDataMember属性
[DataContract]
public class MyClass
{
    [DataMember]
    public int MyValue { get; private set; }
    ...
}

在我的例子中,其中一个类有一个属性,其数据类型是object。像这样:

public class BuyAddOnServiceRequest
{
    object site_id
}

:

public class BuyAddOnServiceRequest
{
    string site_id
}

工作!

刚刚遇到了另一种情况。我使用了一个Serializable类型作为我的一个操作契约方法的参数。

从使用中注释出这个特定的方法使我能够找到问题。在本例中,参数是从文件反序列化的模型,因此我只是用byte[]参数替换了实现,并在另一端运行反序列化逻辑。

虽然不一定是所有问题的答案,但在操作契约方法的参数类型是可序列化的情况下,您也可能遇到此异常。我想用正确的datcontract属性来修饰它们将有助于纠正这个问题。