Я сделал демо, желаю, чтобы это было полезно для вас.
На стороне сервера.
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; }
}
Результат.

Если я позвоню в Почтальон.

В зависимостина комбинацию BodyStyle , ResquestFormat и ResponseFormat .У нас будут разные форматы.
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 для назначения имени параметра вручную.
[return: MessageParameter(Name ="result")]
BookingResult Booking([MessageParameter(Name ="book")] BookInfo bookInfo);
Запрос:
{“book”:{“Name”:”value”}}
Ответ:
{“result”:{“isSucceed”:value}}
Не стесняйтесь связатьсяя Если у вас есть какие-либо вопросы.