在 WCF 服务中使用 JSON 获取对象为空 [英] Get the object is null using JSON in WCF Service

查看:22
本文介绍了在 WCF 服务中使用 JSON 获取对象为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,我创建了一个网站来测试我的服务.但是,该对象在方法中始终为空.我使用 if 语句来检查对象,它为空对象返回 false.有人可以解决我如何修复它.

I have a WCF service and I created a website to test my service. However the object is always null in the method. I used the if statement to check the object and it return false for the null object. Would someone solve me how to fixed it.

有我的服务:

 [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.WrappedRequest,          
       UriTemplate = "BookInfo/")]

    BookingResult Booking(BookInfo bookInfo);


 public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();            
        if (bookInfo == null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }

        return result;
    }

我的网站里有调用服务的方法.

There is the method in my website to call the service.

private string callService(BookInfo input)
    {

        string serviceUrl = "http://localhost:1599026/Booking.svc/BookInfo/";            
        var stringPayload = JsonConvert.SerializeObject(input);   

        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";                    
        client.Encoding = Encoding.UTF8;
        string rtn = client.UploadString(serviceUrl,"POST", stringPayload);
        return rtn;

    }

推荐答案

我做了一个demo,希望对你有用.

I have made a demo, wish it is useful to you.

服务器端.

 class Program
{
    static void Main(string[] args)
    {
        Uri uri = new Uri("http://localhost:2000");
        WebHttpBinding binding = new WebHttpBinding();
        using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
        {
            ServiceEndpoint se = sh.AddServiceEndpoint(typeof(IService), binding, "");
            se.EndpointBehaviors.Add(new WebHttpBehavior());

            Console.WriteLine("service is ready....");
            sh.Open();

            Console.ReadLine();
            sh.Close();
        }
    }
}
[ServiceContract(ConfigurationName ="isv")]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
  ResponseFormat = WebMessageFormat.Xml,
   RequestFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate ="BookInfo/")]
    BookingResult Booking(BookInfo bookInfo);
}
[ServiceBehavior(ConfigurationName = "sv")]
public class MyService : IService
{

    public BookingResult Booking(BookInfo bookInfo)
    {
        BookingResult result = new BookingResult();
        if (bookInfo==null)
        {
            result.isSucceed = false;
        }
        else
        {
            result.isSucceed = true;
        }
        return result;
    }
}
[DataContract]
public class BookInfo
{
    [DataMember]
    public string Name { get; set; }
}
[DataContract]
public class BookingResult
{
    [DataMember]
    public bool isSucceed { get; set; }
}

客户端.

class Program
{
    static void Main(string[] args)
    {
        string uri = "http://localhost:2000/BookInfo";
        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";
        client.Encoding = Encoding.UTF8;
        BookInfo input = new BookInfo()
        {
            Name = "Apple"
        };
        string str2 = "{"bookInfo":" + JsonConvert.SerializeObject(input) + "}";
        string result = client.UploadString(uri, "POST", str2);
        Console.WriteLine(result);
    }
}
[DataContract]
public class BookInfo
{
    [DataMember]
    public string Name { get; set; }
}

结果.

如果我在 Postman 中调用它.

If I call it in Postman.

取决于BodyStyleResquestFormatResponseFormat 的组合.我们会有不同的格式.

Depending on the combination of BodyStyle, ResquestFormat and ResponseFormat. We will have the different formats.

1:

ResponseFormat = WebMessageFormat.Json,RequestFormat =
    WebMessageFormat.Json, BodyStyle =
    WebMessageBodyStyle.WrappedRequest,

请求:

{"bookInfo":{"name":"value"}}

回复:

{"BookingResult":{"isSucceed":value}}

2:

ResponseFormat = WebMessageFormat.Json,RequestFormat =
    WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,

请求:

{"name":"value"}

回复:

{"isSucceed":value}

3:

ResponseFormat = WebMessageFormat.Xml,RequestFormat = WebMessageFormat.Xml,BodyStyle = WebMessageBodyStyle.Bare,

请求:

   <BookInfo xmlns="http://schemas.datacontract.org/2004/07/Server6" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Name>true</Name>
    </BookInfo>

回复.

<BookingResult xmlns="http://schemas.datacontract.org/2004/07/Server6" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <isSucceed>true</isSucceed>
</BookingResult>

4:

 ResponseFormat = WebMessageFormat.Xml,RequestFormat WebMessageFormat.Xml,BodyStyle= WebMessageBodyStyle.Wrapped

请求:

<Booking xmlns="http://tempuri.org/">
    <bookInfo>
    <Name>abcd</Name>
    </bookInfo>
</Booking>

回复:

<BookingResponse xmlns="http://tempuri.org/">
<BookingResult xmlns:a="http://schemas.datacontract.org/2004/07/Server6" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:isSucceed>true</a:isSucceed>
</BookingResult>

我们也可以使用 MessageParameter 属性来手动分配参数的名称.

We could also use the MessageParameter attribute to assign the parameter’s name manually.

[return: MessageParameter(Name ="result")]
BookingResult Booking([MessageParameter(Name ="book")] BookInfo bookInfo);

请求:

{"book":{"Name":"value"}}

回复:

{"result":{"isSucceed":value}}

如果您有任何问题,请随时与我联系.

Feel free to contact me If you have any questions.

这篇关于在 WCF 服务中使用 JSON 获取对象为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆