简体   繁体   中英

WCF strange behaviour

I got this when I consume a webservice:

Operation 'Login' of contract 'IServices' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to 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

<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? Next you have defined two parameters in the UriTemplate but the method contain only one. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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