【问题标题】:passing objects in WCF service在 WCF 服务中传递对象
【发布时间】:2012-03-29 00:03:11
【问题描述】:

我是新的 WCF 和 resful 服务。我正在学习如何在 WCF 服务中传递对象。

我有粘贴代码和 web.config 文件。我不知道为什么我会看到这个错误。

请帮忙..

Operation 'saveDataGet' in contract 'IRestServiceImpl' has a query variable named   '  param1' of type 'GainSoft.TaskManager.Service.InputData', but type 'GainSoft.TaskManager.Service.InputData' is not convertible by 'QueryStringConverter'.  Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'. 
Description: An unhandled exception occurred during the execution of the current web request. 
Please review the stack trace for more information about the error and where it  

originated in the code. 


Exception Details: System.InvalidOperationException: Operation 'saveDataGet' in 
contract 'IRestServiceImpl' has a query variable named 'param1' of type 
'GainSoft.TaskManager.Service.InputData', but type 
'GainSoft.TaskManager.Service.InputData' is not convertible by    
 'QueryStringConverter'.  Variables for UriTemplate query values must have types that
 can be converted by 'QueryStringConverter'.

public class RestServiceImpl : IRestServiceImpl
{
    public string saveDataGet(InputData param1)
    {
        return "Via GET: " + param1.FirstName + " " + param1.LastName;
    }
    public string saveDataPost(InputData param1)
    {
        return "Via POST: " + param1.FirstName + " " + param1.LastName;
    }



    public class MyQueryStringConverter : QueryStringConverter
    {
        public override bool CanConvert(Type type)
        {
            return (type == typeof(InputData)) || base.CanConvert(type);
        }
        public override object ConvertStringToValue(string parameter, Type parameterType)
        {
            if (parameterType == typeof(InputData))
            {
                string[] parts = parameter.Split(',');
                return new InputData { FirstName = parts[0], LastName = parts[1] };
            }
            else
            {
                return base.ConvertStringToValue(parameter, parameterType);
            }
        }
    }
    public class MyWebHttpBehavior : WebHttpBehavior
    {
        protected override QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
        {
            return new MyQueryStringConverter();
        }
    }

public interface IRestServiceImpl
{

    [OperationContract]
    [WebGet(UriTemplate = "/InsertData?param1={param1}")]

    string saveDataGet(InputData param1);

   [OperationContract]
   [WebInvoke(UriTemplate = "/InsertData")]
   string saveDataPost(InputData param1);
 }

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour" >
      <!-- Add the following element to your service behavior configuration. -->
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="MyWebHttpBehavior">
      <webHttp/>

    </behavior>
  </endpointBehaviors>
</behaviors>

【问题讨论】:

标签: wcf rest


【解决方案1】:

我认为您不能将任何复杂的数据类型作为查询参数传递。根据您的要求使用另一个动词,例如 POST 或 PUT。您还可以将 InputData 序列化为 json 字符串或 smth。并以这种方式传递。

【讨论】:

  • 可以从代码示例中显示,链接到博客或帖子也会有所帮助。
  • 当然,给你,希望对你有帮助link
【解决方案2】:

做一些这样的事情:

在合同文件中

[OperationContract]
[WebGet(UriTemplate = "/TabelasAuxiliares?requestex={requestex}", ResponseFormat = WebMessageFormat.Xml)]
    CadastrodeEscolasResponse TabelasAuxiliares(string requestex);

在服务文件中

public CadastrodeEscolasResponse TabelasAuxiliares(string requestex) {

    XmlSerializer serializer = new XmlSerializer(typeof(CadastrodeEscolasRequest));
    StringReader rdr = new StringReader(requestex);
    CadastrodeEscolasRequest request = (CadastrodeEscolasRequest)serializer.Deserialize(rdr);
}

结论:通过向字符串参数发送XML格式数据来调用服务。然后将 XML 转换为所需的类对象。这样可以避免创建非常麻烦的 QueryStringConvertor。希望这会有所帮助!此帮助适用于所有人,而不仅仅是这篇文章。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多