无法在 post WCF 请求 .NET C# 中接收“\”字符
Unable to receive '\' character in post WCF request .NET C#
{ "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);
根据你的描述,我测试了一下,没有发现问题,下面是我的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();
}
}
}
这是我的服务。我在服务中启用了帮助文档。
根据帮助文档,我们可以正确请求服务。
更新
{ "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);
根据你的描述,我测试了一下,没有发现问题,下面是我的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();
}
}
}
这是我的服务。我在服务中启用了帮助文档。
根据帮助文档,我们可以正确请求服务。
更新