android 连接 wcf rest注意点

Posted liangouyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 连接 wcf rest注意点相关的知识,希望对你有一定的参考价值。

1.datacontract 的元素要有默认值,null 值序列化为json时会有问题,异常并不会在调试中跳出,只是tcp tracer中不会response信息,然后

android 客户端报System.EOF错误,或者IOException:java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

 [DataContract]
    public class UserData
    {
        private long userId = (long)0;

        private string username = string.Empty;

        private string nickName = string.Empty;

        private string sex = string.Empty;

        private string icon = string.Empty;

        private string mobilePhone = string.Empty;


        private DateTime registerDate = DateTime.Now;

       

        private string customId = string.Empty;

        private string flag = string.Empty;
        private string errorInfo = string.Empty;

        [DataMember]
        public long UserId
        {
            get
            {
                return userId;
            }

            set
            {
                userId = value;
            }
        }
        [DataMember]
        public string UserName
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
            }
        }
        [DataMember]
        public string NickName
        {
            get
            {
                return nickName;
            }

            set
            {
                nickName = value;
            }
        }
        [DataMember]
        public string Sex
        {
            get
            {
                return sex;
            }

            set
            {
                sex = value;
            }
        }
        [DataMember]
        public string Icon
        {
            get
            {
                return icon;
            }

            set
            {
                icon = value;
            }
        }
        [DataMember]
        public string MobilePhone
        {
            get
            {
                return mobilePhone;
            }

            set
            {
                mobilePhone = value;
            }
        }
        [DataMember]
        public DateTime RegisterDate
        {
            get
            {
                return registerDate;
            }

            set
            {
                registerDate = value;
            }
        }
        [DataMember]
        public string CustomId
        {
            get
            {
                return customId;
            }

            set
            {
                customId = value;
            }
        }
        [DataMember]
        public string Flag
        {
            get
            {
                return flag;
            }

            set
            {
                flag = value;
            }
        }
        [DataMember]
        public string ErrorInfo
        {
            get
            {
                return errorInfo;
            }

            set
            {
                errorInfo = value;
            }
        }
    }
View Code

 

2.把wcf 的异常输出到指定文件中

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <behaviors>
      <serviceBehaviors>
        <behavior name="webHttpServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="metadata"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttpendpointBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      

    </behaviors>
    
    <bindings>
      <webHttpBinding>
        <binding
          crossDomainScriptAccessEnabled="true"
          name="webHttpBinding" maxBufferSize="2147483647"
          maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                        maxNameTableCharCount="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="ChatWithLocationServiceLib.services.LoginService"
              behaviorConfiguration="webHttpServiceBehavior">
        <endpoint
                  address="Http://192.168.0.175:3721/LoginService"
                  binding="webHttpBinding"
                  behaviorConfiguration="webHttpendpointBehavior"
                  contract="ChatWithLocationServiceLib.interfaces.ILoginService"/>
      </service>
     
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <!--保存具体的错误信息到svclog文件中-->
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true">
        <listeners>
          <add name="xml" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="F:\\wcf.svclog" />
    </sharedListeners>
  </system.diagnostics>
</configuration>
View Code

3.声明服务接口

  [ServiceContract]

    public interface ILoginService
    {
        [OperationContract(Name = "Login")]
        [WebInvoke(Method = "GET",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "Login/{GID}/{PWD}")]
        UserData Login(string GID, string PWD);

        [OperationContract(Name = "RegistLogin")]
        [WebInvoke(Method = "POST",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "RegistLogin")]
        UserData RegistLogin(UserData baseInfo);
    }
View Code

4.实现服务接口

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class LoginService : ILoginService
    {
        public UserData Login(string GID, string PWD)
        {
            Console.WriteLine("GID" + GID + ";PWD:" + PWD);
            UserData data = new UserData();
            data.UserId = 44;
            data.UserName = "erer";
            data.ErrorInfo = "username fail";
            return data;
        }

        public UserData RegistLogin(UserData baseInfo)
        {
            UserData data = new UserData();
            data.UserId = 0;
            data.UserName = baseInfo.UserName;
            return data;
        }
    }
View Code

 

以上是关于android 连接 wcf rest注意点的主要内容,如果未能解决你的问题,请参考以下文章

Android将文件作为ByteArray上传到WCF REST服务

WCF REST tcp 错误

使用 WCF/RESTful 服务的 Android 应用程序

在 Android 中使用 Restful WCF 服务

使用 WCF Rest 服务通信 Android 应用程序

将字符串从 android 传递到 wcf restful 服务变得空