简体   繁体   English

WCF 传递复杂 type_either 的问题与调用一起使用

[英]WCF problem passing complex type_either worked with invoke

I want to pass complex type ( list of integers and other integer) in my service wcf.我想在我的服务 wcf 中传递复杂类型(整数列表和其他整数)。 here is my service:这是我的服务:

    [OperationContract]        
     [WebInvoke(Method = "Get", UriTemplate = "GetUserByID")]    
    List<User> GetUserByID(UserIdParams userIdParams);

This is my class type:这是我的 class 类型:

public class UserIdParams : CommonParams
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]
    public List<int> ListUserId { get; set; }
}
  • If I test it with WCF Test Client, it is ok,it worked如果我用 WCF 测试客户端测试它,没关系,它工作在此处输入图像描述

  • but if I test it with Postman, i have error !但如果我用 Postman 测试它,我有错误! 在此处输入图像描述

  • List item项目清单

*** The behavior: *** 行为:

 <behavior name="restBehavior">
      <webHttp helpEnabled="true"/>  
    </behavior>

The XML of WCF Test Client: 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>

So how can i fix it?那么我该如何解决呢?

Thanks a lot for dnxit, he offered me a solution by always working with GET,非常感谢 dnxit,他通过始终与 GET 合作为我提供了解决方案,

  • My old class:我的旧 class:

     public class UserIdParams: CommonParams { [DataMember] public int UserId { get; set; } [DataMember] public List<int> ListUserId { get; set; } }
  • and the old service:和旧服务:

     [OperationContract] [WebInvoke(Method = "Get", UriTemplate = "GetUserByID")] List<User> GetUserByID(UserIdParams userIdParams);

Now for fix this bug and work execute WCF REST with a parameter Array: * the modified class:现在修复此错误并使用参数数组执行 WCF REST:* 修改后的 class:

public class UserIdParams : CommonParams
{
    [DataMember]
    public int UserId { get; set; }
    [DataMember]     
    public string DelimitedUserIds { get; set; }
}
  • the modified service:修改后的服务:

     [OperationContract] [WebGet(UriTemplate = "GetUserByID?DelimitedUserIds={DelimitedUserIds}")] List<User> GetUserByID(string DelimitedUserIds);

And the most important thing is to add: (exemple)最重要的是添加:(示例)

 string DelimitedUserIds = "9,3,12,43,2"
 List<int> UserIds = DelimitedUserIds .Split(',').Select(int.Parse).ToList();

WCF Test client test this operation as SOAP service not rest service .Please refer XML tab in test client. WCF 测试客户端测试此操作为SOAP 服务而不是rest 服务。请参阅客户端中的 Z3501BB093D3963ED83FB867 服务选项卡

I think you've found that with the restful style of the WCF service, the Get method does not support using complex objects as parameters by default, thereby please consider using the post/put method.我想你已经发现WCF服务的restful风格,Get方法默认不支持使用复杂对象作为参数,因此请考虑使用post/put方法。 This also does not conform to the design guidelines for the HTTP get method.这也不符合 HTTP 获取方法的设计指南。
https://blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/wcf-extensibility-querystringconverter/ https://blogs.msdn.microsoft.com/carlosfigueira/2011/08/08/wcf-extensibility-querystringconverter/
In addition, Postman is usually used to test restful services.另外,Postman通常用于测试restful服务。
Here is a configuration which applies on restful style WCF service.这是适用于宁静风格 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>

Feel free to let me know if there is anything I can help with.如果有什么我可以帮忙的,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM