1

当我使用网络服务时,我得到了这个:

合约“IServices”的“登录”操作指定了多个要序列化的请求主体参数,而无需任何包装器元素。最多一个 body 参数可以在没有包装元素的情况下被序列化。删除额外的正文参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped。

我使用的界面如下:

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);

    }
}

然后我托管它,然后我向我的网站添加网络引用并修改 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>

知道如何解决此错误吗?

4

1 回答 1

0

首先我不确定你为什么使用 GET 操作登录,你应该使用 POST 对吗?接下来,您在 中定义了两个参数,UriTemplate但该方法仅包含一个。我建议你使用一个类作为参数,而不是返回字符串,你也可以返回一个模型。

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)
于 2012-07-13T18:35:07.687 回答