开发者

Backslash character in string causes string to evaluate as "nothing" in WCF

I have a WCF application, written in VB.NET that takes a generic Dictionary(Of String, String) as one of the parameters. When I pass in a Key/Value pair that has a backslash \ as one of the characters in the value, the client automatically changes the entire value to Nothing or in the XML it shows this:

<Value i:nil="true />

Is there some special way to escape backslashes when passing them in a string to a WCF service? As far as I 开发者_运维技巧know the backslash isn't a reserved character in XML.


How are you calling your service? I've just tried this scenario (see code below), and the server prints the values correctly.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜