使用WSDL(SOAP)将PayPal集成到C#/ .NET解决方案中

环境:Visual Studio 2010 Professional .NET Framework 4 C#

使用以下WSDL添加了服务引用: 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 = "xxxxxxxx@xxxxxx.com", 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协议进行成功的事务。

我觉得这很简单。 简单地添加服务引用,并使用Classes及其在WSDL代理中创建的属性和方法。

我哪里错了?

我使用错误的WSDL吗? 我想首先测试Sandbox,然后去Live。

如果我对WSDL是正确的,看起来类PayPalAPIAAInterfaceClient不知道它的端点 ,我不知道我是否想要手动设置,因为它已经在WSDL定义中已经存在(检查出来) )。 我认为类本身应该知道要调用哪个端点,具体取决于我是使用Signature还是Certificate来填充CustomSecurityHeaderType。

但PayPalAPIAAInterfaceClient类如何知道我是否正在尝试调用Sandbox(测试)或者它是一个实时事务?

PayPal过去常常为Sandbox和Live提供两种不同的WSDL。 它们可以在这里找到: – > https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_soap_PayPalSOAPAPIArchitecture

在得到他们的支持后,我被要求对Sandbox和Live使用以下WSDL: – > https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl

但是,当我想要执行Live或Sandbox测试时,如何告诉PayPalAPIAAInterfaceClient类。 以及根据我的SOAP和Signature方法使用的终点。 这里提到了PayPal的端点:

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

救命 !

你在这里遇到了一些问题,但没有一个问题太难解决。 首先,当我在你的post顶部链接的WSDL中添加一个服务引用时,我没有你描述的命名空间的任何问题。 可能是您自己的命名空间/引用在某种程度上与自动生成的术语发生冲突,或者您在添加引用过程中选择了一些奇怪的选项? 删除和重新添加可能会解决问题,或者我猜你可以忽略它,因为你已经解决了它。 (但是,编辑自动生成的代码有点麻烦,所以你应该最终计划修复。)

要解决InvalidOperationException ,您可能只需要指定Visual Studio自动添加到app.config文件中的一个端点。 你应该在配置文件中有这样的东西:

       

您可以将所需端点的名称传递给代理类的构造函数。 还有其他选项可以解决此问题,但只需指定端点即可轻松而干净。 (注意:如果您的配置文件中没有此部分,那么在添加服务引用阶段出现问题。再次,我建议您重新设置项目并重新添加引用。)

最后,当您使用代理类时,尽管它是IDisposable ,您不希望使用using块。 基本上, WCF中存在设计错误 。

我遇到了同样的问题,因为我正在进行unit testing。

您必须将application.config文件复制到测试项目,否则它将找不到WCF配置。