2012-01-06 68 views
6

環境: 的Visual Studio 2010專業 .NET Framework 4的 下,用下面的WSDL#在C#中集成貝寶/使用WSDL NET解決方案(SOAP)

增值服務參考:https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

問題1:當像這樣編譯時,從Reference.cs文件中得到一堆錯誤。看起來像命名空間錯誤。它提到它無法在我的項目的命名空間中找到服務引用命名空間。因此,我進入Reference.cs文件,無論我遇到這個錯誤,我在方法名稱前刪除了項目的名稱空間,現在編譯它。

終於可以訪問所有的類。 創建和填充DoDirectPaymentReqCustomSecurityHeader具有所需屬性的對象。 創建了一個PayPalAPIAAInterfaceClient類的實例,該類包含方法DoDirectPayment,它接受CustomSecurityHeader和DoDirectPaymentReq類型的參數。看起來是這樣的:

using (var client = new **PayPalAPIAAInterfaceClient**()) 
{ 
    var credentials = new CustomSecurityHeaderType 
    { 
     Credentials = new UserIdPasswordType 
     { 
     Username = "[email protected]", 
     Password = "xxxxxxx", 
     Signature = "jksadfuhasfweakjhasf" 
     } 
    }; 

    _doDirectPaymentResponseType = client.DoDirectPayment(ref credentials, _doDirectPaymentReq); 
} 

問題2:對包含上述代碼的方法寫一個TestMethod的後,我得到的錯誤如下:

System.InvalidOperationException: Could not find default endpoint element that references contract 'Paypal.PayPalAPIAAInterface' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. 

at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName) 
at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName, Configuration configuration) 
at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName) 
at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address) 
at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress) 
at System.ServiceModel.EndpointTrait`1.CreateSimplexFactory() 
at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait) 
at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef() 
at System.ServiceModel.ClientBase`1..ctor() 
at PaymentEngine.Paypal.PayPalAPIAAInterfaceClient..ctor() in Reference.cs: line 30063 

因此,到目前爲止,我還沒有通過在C#中使用WSDL,能夠使用PayPal SOAP協議進行成功的事務。

我當時覺得這很簡單。只需添加服務引用,然後利用WSDL中代理中創建的屬性和方法的類。

我哪裏錯了?

我使用錯誤的WSDL嗎?我想先對Sandbox進行測試,然後進入Live。

如果我是正確的與WSDL,看起來像類PayPalAPIAAInterfaceClient不知道它的終點,我不知道如果我想以手動方式或不設置自已經在WSDL定義在那裏結束(檢查出來)。我認爲類本身應該知道要調用哪個端點,具體取決於我是使用Signature還是Certificate來填充CustomSecurityHeaderType。

但PayPalAPIAAInterfaceClient類如何知道我是否試圖調用沙盒(測試)還是實時事務?

PayPal用於Sandbox和Live兩種不同的WSDL。他們可以在這裏找到: - >https://cms.paypal.com/us/cgi-bin/?cmd=_render-content &的content_id =開發商/ e_howto_api_soap_PayPalSOAPAPIArchitecture

說在他們的支持後,我要求將以下WSDL用於Sandbox和Live: - >https://www.paypalobjects.com/wsdl/PayPalSvc。wsdl

但是,當它假設執行Live或Sandbox測試時,如何告訴PayPalAPIAAInterfaceClient類。並根據我的SOAP和簽名方法使用哪個終點。來自PayPal的終點這裏提到:

https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_endpoints

HELP!

回答

6

這裏有一些問題,但沒有一個應該太痛苦才能解決。首先,當我將WSDL中的服務引用添加到您在帖子頂部鏈接的WSDL時,我沒有任何您描述的名稱空間問題。這可能是你自己的命名空間/引用與自動生成的條款有某種矛盾,或者你在添加引用過程中選擇了一些奇怪的選項?刪除並重新添加可能會解決問題,或者我想你可以忽略它,因爲你已經解決了它。 (但是,編輯自動生成的代碼很麻煩,因此最終應該計劃修復)。

要解決InvalidOperationException,您可能只需指定Visual Studio自動生成的其中一個端點添加到你的app.config文件。你應該有這樣的事情在你的配置文件:

<system.serviceModel> 
    <client> 
    <endpoint name="PayPalAPI" ... /> 
    <endpoint name="PayPalAPIAA" ... /> 
    </client> 
</system.serviceModel> 

你可以通過你想代理類的構造函數的終結點的名稱。有other options來解決這個問題,但只是指定一個端點很容易和乾淨。 (注意:如果你沒有這個部分在您的配置文件,然後出事了在添加服務引用階段再次我只想建議重置您的項目,重新添加引用。)

最後當你使用代理類時,儘管它是IDisposable,但你不想使用using塊。基本上,there's a design bug in WCF

+0

OMG !!!我多愚蠢!爲什麼我沒有看app.config!工作!謝謝你,謝謝你,謝謝你!雖然我從Paypal收回「版本不支持」錯誤。 – dparkar 2012-01-06 20:25:39

+1

不支持的版本可以通過在SOAP請求中發送正確版本來解決:目前最新版本爲84.0。 – Robert 2012-01-06 22:52:04

+0

謝謝羅伯特!這也工作:) – dparkar 2012-01-09 16:26:38

1

我有同樣的問題,因爲我正在做單元測試。

您必須將application.config文件複製到測試項目,否則它將找不到WCF配置。