Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

Unable to receive '' character in post WCF request .NET C#

{ "string001Value":"LITTELFUSE \/ 772291851034","string002Value":"772291851034","string003Value":"","string004Value":""... }

When sending a Json object the WCF service receives it without the backslash '', the string001Value property receives the value of "LITTELFUSE / 772291851034".

How can I receive this type of characters in the service so that the value is "LITTELFUSE / 772291851034"?

this is the WebInvoke Method:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, 
           RequestFormat = WebMessageFormat.Json, 
           ResponseFormat = WebMessageFormat.Json, 
           UriTemplate = "warehouse_shipping_advice")]
_ResponseDetail WarehouseShippingAdvice(_WarehouseShipping newWarehouseShipping);
question from:https://stackoverflow.com/questions/65545474/unable-to-receive-character-in-post-wcf-request-net-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to your description, I did a test but did not find the problem, here is my 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();
            }
        }
    }

This is my service. I enabled the help document in the service.

enter image description here

According to the help document, we can request the service correctly.

enter image description here

UPDATE

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...