字符串中的反斜杠字符会导致字符串在WCF中评估为“无”

时间:2011-05-24 21:03:47

标签: vb.net wcf

我有一个用VB.NET编写的WCF应用程序,它将通用Dictionary(Of String, String)作为参数之一。 当我传入一个反斜杠\作为值中的一个字符的键/值对时,客户端会自动将整个值更改为Nothing,或者在XML中显示:

<Value i:nil="true />

在将字符串传递给WCF服务时,是否有一些特殊的方法来转义反斜杠?据我所知,反斜杠不是XML中的保留字符。

1 个答案:

答案 0 :(得分:1)

您如何称呼您的服务?我刚试过这个场景(见下面的代码),服务器正确打印了值。

Public Class StackOverflow_6116861_751090
    <ServiceContract()> _
    Public Interface ITest
        <OperationContract()> Sub Process(ByVal dict As Dictionary(Of String, String))
    End Interface

    Public Class Service
        Implements ITest

        Public Sub Process(ByVal dict As System.Collections.Generic.Dictionary(Of String, String)) Implements ITest.Process
            For Each key In dict.Keys
                Console.WriteLine("{0}: {1}", key, dict(key))
            Next
        End Sub
    End Class

    Public Shared Sub Test()
        Dim baseAddress As String = "http://" + Environment.MachineName + ":8000/Service"
        Dim host As ServiceHost = New ServiceHost(GetType(Service), New Uri(baseAddress))
        host.AddServiceEndpoint(GetType(ITest), New BasicHttpBinding(), "")
        host.Open()
        Console.WriteLine("Host opened")

        Dim factory As ChannelFactory(Of ITest) = New ChannelFactory(Of ITest)(New BasicHttpBinding(), New EndpointAddress(baseAddress))
        Dim proxy As ITest = factory.CreateChannel()

        Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)
        dict.Add("o\ne", "uno")
        dict.Add("two", "do\s")
        dict.Add("th\ree", "tr\es")
        proxy.Process(dict)
    End Sub
End Class