开发者

What is the best way to develop an asp.net Webservice which will have methods that return list, individual objects or other complex datatypes

We are using .Net 2.0 to create webservices. We would like to design webservices in ASP.Net 2.0.

Currently the webservices we have return either a single parameter like

  <s:element name="ChangePassword">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="userName" type="s:string" />
        <s:element minOccurs="0" maxOccurs="1" name=开发者_运维百科"currentPassword" type="s:string"/>
        <s:element minOccurs="0" maxOccurs="1" name="newPassword" type="s:string" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:element name="ChangePasswordResponse">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="1" maxOccurs="1" name="ChangePasswordResult" type="s:boolean" />
      </s:sequence>
    </s:complexType>
  </s:element>

We have also designed webservices that return a model like

  <s:element name="GetCreditBalance">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="userName" type="s:string" />
        <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:element name="GetCreditBalanceResponse">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="GetCreditBalanceResult" type="tns:UserCreditsModel" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:complexType name="UserCreditsModel">
    <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
      <s:element minOccurs="1" maxOccurs="1" name="CreditBalance" type="s:decimal" />
      <s:element minOccurs="1" maxOccurs="1" name="ValidityDate" type="s:dateTime" />
    </s:sequence>
  </s:complexType>

We have also designed webservices that return a list of models like

  <s:element name="GetHistory">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="userName" type="s:string" />
        <s:element minOccurs="0" maxOccurs="1" name="password" type="s:string" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:element name="GetHistoryResponse">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="GetHistoryResult" type="tns:ArrayOfSMSCreditHistoryModel" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:complexType name="ArrayOfSMSCreditHistoryModel">
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="unbounded" name="SMSCreditHistoryModel" nillable="true" type="tns:SMSCreditHistoryModel" />
    </s:sequence>
  </s:complexType>
  <s:complexType name="SMSCreditHistoryModel">
    <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="CreditHistoryId" type="s:int" />
      <s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
      <s:element minOccurs="1" maxOccurs="1" name="PaymentDate" type="s:dateTime" />
      <s:element minOccurs="0" maxOccurs="1" name="PaymentRefNo" type="s:string" />
      <s:element minOccurs="1" maxOccurs="1" name="TotalAmount" type="s:decimal" />
      <s:element minOccurs="1" maxOccurs="1" name="CreditsEarned" type="s:int" />
      <s:element minOccurs="1" maxOccurs="1" name="ValidityDate" type="s:dateTime" />
      <s:element minOccurs="0" maxOccurs="1" name="Mode" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="Status" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="ChequeNO" type="s:string" />
      <s:element minOccurs="1" maxOccurs="1" name="ChequeDate" type="s:dateTime" />
      <s:element minOccurs="0" maxOccurs="1" name="ChequeBankName" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="Remarks" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="ValidityDateTime" type="s:string" />
    </s:sequence>
  </s:complexType>

The current approach is that if while any operation failure like authentication failure, we throw an exception from the webservice.

we would like to know which is the best approach to design a webservice so it can be consumed across various platforms without having technology issues.

Would use of json help. Would returning a pure XML will help.


In general, you should throw a SoapException from an ASMX web service to indicate a failure. This will return a SOAP Fault message back to the client.

However, ASMX web services do not have proper support for SOAP Faults. The generated WSDL will not indicate to your clients that your operations can return faults. This will prevent some clients from properly processing those faults.

So, you should really move up to WCF, which solves this problem. Your alternatives, if you must continue using .NET 2.0, are to not use SoapException, or to create your own WSDL, which should properly describe the faults returned from your operations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜