简体   繁体   English

WCF奇怪的行为

[英]WCF strange behaviour

I got this when I consume a webservice: 我在使用Web服务时得到了以下信息:

Operation 'Login' of contract 'IServices' specifies multiple request body parameters to be serialized without any wrapper elements. 合同“ IServices”的“登录”操作指定了要序列化的多个请求正文参数,而没有任何包装元素。 At most one body parameter can be serialized without wrapper elements. 没有包装器元素,最多可以序列化一个body参数。 Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped. 删除多余的正文参数,或将WebGetAttribute / WebInvokeAttribute上的BodyStyle属性设置为Wrapped。

I use interface look like : 我使用的界面看起来像:

namespace DreamServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IServices
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,

        BodyStyle = WebMessageBodyStyle.Wrapped,

        UriTemplate = "LogIn/{username}/{password}")]
        string Login(string username, string password);

        [OperationContract]
        [WebInvoke(
            Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "WaletTotalAmount/{userid}")]
        double? WaletTotalAmount(string userid);

        [OperationContract]
        [WebInvoke(
            Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "UserService/{userid}")]
        IList<UserServiceses> UserService(string userid);

        [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "InsertUpdateWallet/{userid}/{Amount}/{ComissionAmount}")]
        void InsertUpdateWallet(string userid, string Amount, string ComissionAmount);

    }
}

and I host it then I add web reference to my site and modify the web.config such that the will be like 然后托管它,然后将Web引用添加到我的网站并修改web.config,以使像

<system.serviceModel>

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="defaultRest">
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>

    <client>
      <endpoint address="http://localhost:1381/PMAHost/Service.svc" binding="webHttpBinding" contract="ServiceReference.IServices" behaviorConfiguration="web"/>
    </client>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Any idea how to fix this error? 任何想法如何解决此错误?

First I'm not sure why you are using GET operation for login, you should use POST right? 首先我不确定为什么要使用GET操作进行登录,应该使用POST对吗? Next you have defined two parameters in the UriTemplate but the method contain only one. 接下来,您在UriTemplate定义了两个参数,但是该方法仅包含一个。 I would suggest you to use a class as parameter and instead of returning string you could return a model as well. 我建议您使用类作为参数,而不是返回字符串,您也可以返回模型。

public class LoginModel
{
   public string username { get; set; }
   public string password { get; set; }
}

public class Result
{
   public bool success { get; set; }
   public string error { get; set; }
}

[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/LogIn")]
public Result login(LoginModel loginModel)

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

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