gpt4 book ai didi

vb.net - 字符串中的反斜杠字符导致字符串在 WCF 中计算为 "nothing"

转载 作者:行者123 更新时间:2023-12-03 00:08:40 25 4
gpt4 key购买 nike

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

<Value i:nil="true />

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

最佳答案

您如何调用您的服务?我刚刚尝试过这种情况(请参阅下面的代码),并且服务器正确打印了值。

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

关于vb.net - 字符串中的反斜杠字符导致字符串在 WCF 中计算为 "nothing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6116861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com