【问题标题】:Unable to receive '\' character in post WCF request .NET C#无法在发布 WCF 请求 .NET C# 中接收“\”字符
【发布时间】:2021-01-03 00:16:57
【问题描述】:
{ "string001Value":"LITTELFUSE \\/ 772291851034","string002Value":"772291851034","string003Value":"","string004Value":""... }

当发送一个 Json 对象时,WCF 服务会在不使用反斜杠 '' 的情况下接收它,string001Value 属性会接收到“LITTELFUSE / 772291851034”的值。

如何在服务中接收此类字符,使其值为“LITTELFUSE \ / 772291851034”?

这是 WebInvoke 方法:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, 
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           UriTemplate = "warehouse_shipping_advice")]
_ResponseDetail WarehouseShippingAdvice(_WarehouseShipping newWarehouseShipping);

【问题讨论】:

  • 您的WebInvoke 定义是什么?例如,以下对我有用:[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)].
  • 我有这个信息 @OfirD [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "warehouse_shipping_advice") ] _ResponseDetail WarehouseShippingAdvice(_WarehouseShipping newWarehouseShipping);
  • 这和我的一样。你的客户代码是什么?请将其添加到您的问题中(同时,将您的 WebInvoke 定义从您的评论中添加到问题中)。
  • 我编辑了@OfirD 的问题,可以吗?
  • 比较好,但是还是缺少客户端代码。您需要使用该 json 添加实际执行 http post 请求的代码,调用 WarehouseShippingAdvice

标签: c# wcf


【解决方案1】:

根据你的描述,我做了测试,没有发现问题,这里是我的demo:

 [ServiceContract]
    public interface IService1
    {
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "warehouse_shipping_advice")]
        void WarehouseShippingAdvice(_WarehouseShipping newWarehouseShipping);
    }
    [DataContract]
    public class _WarehouseShipping
    {
        [DataMember]
        public string string001Value { get; set; }
    }
    public class Service1 : IService1
    {
        public void WarehouseShippingAdvice(_WarehouseShipping newWarehouseShipping)
        {
            Console.WriteLine(newWarehouseShipping.string001Value);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1: Create a URI to serve as the base address.
            Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");

            // Step 2: Create a ServiceHost instance.
            ServiceHost selfHost = new ServiceHost(typeof(Service1), baseAddress);

            try
            {
                // Step 3: Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(IService1), new WebHttpBinding(), "CalculatorService").Behaviors.Add(new WebHttpBehavior() { HelpEnabled=true});

                // Step 4: Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5: Start the service.
                selfHost.Open();
                Console.WriteLine("The service is ready.");

                // Close the ServiceHost to stop the service.
                Console.WriteLine("Press <Enter> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }

这是我的服务。我在服务中启用了帮助文档。

根据帮助文档,我们可以正确请求服务。

更新

【讨论】:

  • 你可以尝试使用在线的\/而不是\\/吗
  • 我更新了我的回复。在c#中,“\\”代表一个反斜杠,更多关于转义序列的信息可以参考这个链接:docs.microsoft.com/en-us/cpp/c-language/…
  • 如果您觉得我的回复对您有帮助,可以标记为回答。
猜你喜欢
  • 2019-12-15
  • 1970-01-01
  • 2021-08-27
  • 2021-05-29
  • 1970-01-01
  • 2020-02-14
  • 2021-08-05
  • 1970-01-01
  • 2013-07-08
相关资源
最近更新 更多