Using Web Service or Web Service Reference

前端 未结 3 904
面向向阳花
面向向阳花 2021-01-01 06:45

This relates specifically to .NET. And I\'ve decided to use the Web service Reference instead of Web Service...I think though my questions below really are for either situa

3条回答
  •  爱一瞬间的悲伤
    2021-01-01 07:35

    There's serious confusion here. Everybody please STOP!

    WSE is obsolete. Don't use it.

    "Web References" relate to the old ASMX web service technology. Microsoft now considers it to be "legacy" code and has stopped fixing bugs. Don't use it if you don't have to.

    The OP should use "Add Service Reference". I just tried it, and it worked.


    Now, to your questions:

    1. How can I specify header params?

      Because this service uses headers, the proxy classes have been generated as Message Contracts, which can have header parts, and body parts. For instance, the RefundTransactionRequest class has a RequesterCredentials property which is of the CustomSecurityHeaderType type. You simply set that before sending the request. In fact, it has generated a constructor for RefundTransactionRequest that takes both the header and the body as parameters, so this is easy.

    2. How is the request/response objects handled?

      You don't have to do anything except create instances of classes, and call methods. All the serialization is done for you. All the class creation is done for you.

    3. When I receive the response, does the Proxy class contain its state?

      Yes. The proxy class will contain all message header and body parts. Of course, there will only be a header part if there was an output header.

    4. When ever would I want to instead just create custom code like I did before and not use web services/reference option with WSDL?

      Never, I think. I've never seen a reason to do so. I guess the only time would be if there was a bug in "Add Service Reference".


    Example:

    using (var svc = new PayPalAPIAAInterfaceClient())
    {
        var paypal = (PayPalAPIInterface) svc;
        var credentials = new CustomSecurityHeaderType
                              {
                                  Credentials =
                                      new UserIdPasswordType
                                          {
                                              AppId = "",
                                              Username = "John",
                                              Password = "John"
                                          }
                              };
        var request = new RefundTransactionRequestType
                          {
                              Amount =
                                  new BasicAmountType
                                      {
                                          currencyID = CurrencyCodeType.USD,
                                          Value = "100.00"
                                      },
                              Memo = "I want my money back",
                              RefundType = RefundType.Full,
                              RefundTypeSpecified = true
                          };
        var refundRequest = new RefundTransactionReq
                                {RefundTransactionRequest = request};
        var result =
            paypal.RefundTransaction(
                new RefundTransactionRequest(credentials, refundRequest));
        var response = result.RefundTransactionResponse1;
        var returnedCredentials = result.RequesterCredentials;
    }
    

提交回复
热议问题