这实际上是由 WebMessageBodyStyle.WrappedResponse 引起的。如果将 WebMessageBodyStyle 更改为 Wrapped,WCF 将再次封装该对象。这是一个演示:
[OperationContract]
[WebInvoke(
Method = "POST",
ResponseFormat = WebMessageFormat.Json, RequestFormat =
WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedResponse
)]
string GetAccount(UserData requestParams);
WrappedResponse 将封装响应。
因为你的WebMessageBodyStyle是WrappedRequest,所以你在客户端发送的对象一定要封装,否则会出现400 Bad Request。
我认为最好的解决方案是将 WebMessageBodyStyle 设置为 Bare,这是一个演示:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(
Method = "POST",
ResponseFormat = WebMessageFormat.Json, RequestFormat =
WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare
)]
string GetAccount(UserData requestParams);
}
[DataContract(Name = "user")]
public class UserData
{
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Password")]
public string Password { get; set; }
[DataMember(Name = "Email")]
public string Email { get; set; }
}
public class Service1 : IService1
{
public string GetAccount(UserData requestParams)
{
return "OK";
}
}
class Program
{
static void Main(string[] args)
{
ServiceHost selfHost = new ServiceHost(typeof(Service1));
selfHost.Open();
Console.WriteLine("Service Open");
Console.ReadKey();
selfHost.Close();
}
}
这是 Program.cs。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.serviceModel>
<services>
<service name="ConsoleApp113.Service1" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8012/ServiceModelSamples/service"/>
</baseAddresses>
</host>
<endpoint address=""
binding="webHttpBinding"
contract="ConsoleApp113.IService1"
behaviorConfiguration="ESEndPointBehavior" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
这是 web.config。
class Program
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(
Method = "POST",
ResponseFormat = WebMessageFormat.Json, RequestFormat =
WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare
)]
string GetAccount(UserData requestParams);
}
[DataContract(Name = "user")]
public class UserData
{
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Password")]
public string Password { get; set; }
[DataMember(Name = "Email")]
public string Email { get; set; }
}
static void Main(string[] args)
{
var myBinding = new WebHttpBinding();
var myEndpoint = new EndpointAddress("http://localhost:8012/ServiceModelSamples/service");
using (var factory = new ChannelFactory<IService1>(myBinding, myEndpoint))
{
IService1 apiService = null;
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
apiService = factory.CreateChannel();
UserData userData = new UserData();
userData.Email = "Test";
userData.Name = "Test";
userData.Password = "Test";
string result = apiService.GetAccount(userData);
((ICommunicationObject)apiService).Close();
factory.Close();
Console.WriteLine(result);
Console.ReadLine();
}
}
}
这是客户端代码。
更新:
很遗憾,WCF 中没有这样的设置。但是你可以自己封装一个封装的类型对象:
[DataContract]
public class GetAccount {
[DataMember(Name = "RequestParams ")]
public RequestParams requestParams ;
}
您可以将RequestParams封装到GetAccount中,然后将GetAccount直接发送给服务器。所以为了成功调用WCF服务你必须修改服务接口:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest
)]
RequestParams GetAccount(GetAccount requestParams);