WCF奇怪的行为 [英] WCF strange behaviour

查看:211
本文介绍了WCF奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个,当我消耗了web服务:

I got this when I consume a webservice:

合同IServices'操作'登录'指定多个请求体参数要序列化的,没有任何包装元素。在大多数一种身体参数可以在不包装元素序列化。要么删除多余的车身参数或设置在WebGetAttribute / WebInvokeAttribute的BodyStyle属性来包裹

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.

我使用的界面如下:

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引用到我的网站,并修改web.config使得会像

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?

推荐答案

首先,我不知道为什么你正在使用GET操作为登录,您应该使用POST吧?接下来,您必须在 UriTemplate 定义的两个参数,但该方法只包含一个。我建议你​​使用一个类作为参数,而不是返回字符串可以返回一个模型为好。

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)

这篇关于WCF奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆