2012-01-06 74 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配置。