传递复杂 type_either 的 WCF 问题与调用一起使用
WCF problem passing complex type_either worked with invoke
我想在我的服务 wcf 中传递复杂类型(整数列表和其他整数)。
这是我的服务:
[OperationContract]
[WebInvoke(Method = "Get", UriTemplate = "GetUserByID")]
List<User> GetUserByID(UserIdParams userIdParams);
这是我的 class 类型:
public class UserIdParams : CommonParams
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public List<int> ListUserId { get; set; }
}
如果我用 WCF 测试客户端测试它,没问题,它工作正常
但是如果我用 Postman 测试它,我有错误!
- 列表项
*** 行为:
<behavior name="restBehavior">
<webHttp helpEnabled="true"/>
</behavior>
WCF 测试客户端XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpsBinding_IServiceDataExtractor"
sendTimeout="00:05:00">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="https://localhost:44310/ServiceDataExtractor.svc/soap"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpsBinding_IServiceDataExtractor"
contract="IServiceDataExtractor"
name="BasicHttpsBinding_IServiceDataExtractor" />
</client>
</system.serviceModel>
</configuration>
那我该如何解决呢?
WCF 测试客户端测试此操作作为 SOAP 服务 而不是 rest 服务。请参考 XML测试客户端中的选项卡。
我想你已经发现WCF服务的restful风格,Get方法默认不支持使用复杂对象作为参数,请考虑使用post/put方法.这也不符合 HTTP get 方法的设计准则。
https://blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/wcf-extensibility-querystringconverter/
另外,通常使用Postman来测试restful个服务。
这是适用于 restful 样式 WCF 服务的配置。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="webHttpBinding" scheme="http"/>
</protocolMapping>
</system.serviceModel>
如果有什么我可以帮忙的,请随时告诉我。
非常感谢 dnxit,他通过始终使用 GET 为我提供了一个解决方案,
我老class:
public class UserIdParams : CommonParams
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public List<int> ListUserId { get; set; }
}
和旧服务:
[OperationContract]
[WebInvoke(Method = "Get", UriTemplate = "GetUserByID")]
List<User> GetUserByID(UserIdParams userIdParams);
现在修复此错误并使用参数数组执行 WCF REST:
* 修改后的 class:
public class UserIdParams : CommonParams
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public string DelimitedUserIds { get; set; }
}
修改后的服务:
[OperationContract]
[WebGet(UriTemplate = "GetUserByID?DelimitedUserIds={DelimitedUserIds}")]
List<User> GetUserByID(string DelimitedUserIds);
最重要的是添加:(示例)
string DelimitedUserIds = "9,3,12,43,2"
List<int> UserIds = DelimitedUserIds .Split(',').Select(int.Parse).ToList();
我想在我的服务 wcf 中传递复杂类型(整数列表和其他整数)。 这是我的服务:
[OperationContract]
[WebInvoke(Method = "Get", UriTemplate = "GetUserByID")]
List<User> GetUserByID(UserIdParams userIdParams);
这是我的 class 类型:
public class UserIdParams : CommonParams
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public List<int> ListUserId { get; set; }
}
如果我用 WCF 测试客户端测试它,没问题,它工作正常
但是如果我用 Postman 测试它,我有错误!
- 列表项
*** 行为:
<behavior name="restBehavior">
<webHttp helpEnabled="true"/>
</behavior>
WCF 测试客户端XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpsBinding_IServiceDataExtractor"
sendTimeout="00:05:00">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint
address="https://localhost:44310/ServiceDataExtractor.svc/soap"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpsBinding_IServiceDataExtractor"
contract="IServiceDataExtractor"
name="BasicHttpsBinding_IServiceDataExtractor" />
</client>
</system.serviceModel>
</configuration>
那我该如何解决呢?
WCF 测试客户端测试此操作作为 SOAP 服务 而不是 rest 服务。请参考 XML测试客户端中的选项卡。
我想你已经发现WCF服务的restful风格,Get方法默认不支持使用复杂对象作为参数,请考虑使用post/put方法.这也不符合 HTTP get 方法的设计准则。
https://blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/wcf-extensibility-querystringconverter/
另外,通常使用Postman来测试restful个服务。
这是适用于 restful 样式 WCF 服务的配置。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<protocolMapping>
<add binding="webHttpBinding" scheme="http"/>
</protocolMapping>
</system.serviceModel>
如果有什么我可以帮忙的,请随时告诉我。
非常感谢 dnxit,他通过始终使用 GET 为我提供了一个解决方案,
我老class:
public class UserIdParams : CommonParams { [DataMember] public int UserId { get; set; } [DataMember] public List<int> ListUserId { get; set; } }
和旧服务:
[OperationContract] [WebInvoke(Method = "Get", UriTemplate = "GetUserByID")] List<User> GetUserByID(UserIdParams userIdParams);
现在修复此错误并使用参数数组执行 WCF REST: * 修改后的 class:
public class UserIdParams : CommonParams
{
[DataMember]
public int UserId { get; set; }
[DataMember]
public string DelimitedUserIds { get; set; }
}
修改后的服务:
[OperationContract] [WebGet(UriTemplate = "GetUserByID?DelimitedUserIds={DelimitedUserIds}")] List<User> GetUserByID(string DelimitedUserIds);
最重要的是添加:(示例)
string DelimitedUserIds = "9,3,12,43,2"
List<int> UserIds = DelimitedUserIds .Split(',').Select(int.Parse).ToList();