简体   繁体   English

从C#调用soap服务

[英]calling a soap service from C#

I have working PHP code which calls a SOAP service and it works. 我有工作的PHP代码调用SOAP服务,它的工作原理。 Its as follows: 其内容如下:

<?php
try
{
    $client = new SoapClient(null, array(
        'location' => "http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice?wsdl",
        'uri' => "http://zdemo2.zenprise.com",
        'login' => "Admin",
        'password'=> "XXXXX"));

    $properties=$client->getDeviceProperties("XXXXXXXX",null);

    for($i=0;$i<count($properties);$i++) {
        printf ("name: %s, value: %s\n" , $properties[$i]->name, $properties[$i]->value);
    }
}
catch (Exception $e)
{
    print_r($e); exit;
}
?>

I need to access the same service from C#. 我需要从C#访问相同的服务。 I have tried adding Service Reference to http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice?wsdl and this added the following section in my app.config. 我尝试将Service Reference添加到http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice?wsdl ,这在我的app.config中添加了以下部分。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="EveryWanDeviceSoapBinding" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://108-168-196-91.mycitrixdemo.net/zdm/services/EveryWanDevice"
            binding="basicHttpBinding" bindingConfiguration="EveryWanDeviceSoapBinding"
            contract="ServiceReference1.DeviceService" name="EveryWanDevice" />
    </client>
</system.serviceModel>

I am now provided with the proxy classes but I dont know how to set them up so I could call this service. 我现在提供代理类,但我不知道如何设置它们所以我可以调用此服务。

I am doing it as follows in C#: 我在C#中做如下:

DeviceService srv = new DeviceServiceClient();//
srv.authenticateUser(new authenticateUserRequest("Admin", "XXXXXX"));

var devices = srv.getDeviceProperties(new getDevicePropertiesRequest("99000067296308", null));

But the srv.authenticateUser line throws the following exception: 但是srv.authenticateUser行会抛出以下异常:

RPC Message getDeploymentHistoRequest1 in operation getDeploymentHisto1 has an invalid body name getDeploymentHisto. It must be getDeploymentHisto1

I have no idea what does this error mean. 我不知道这个错误是什么意思。 Can anybody help? 有人可以帮忙吗?

This is due to using a WCF reference versus a standard service reference. 这是由于使用WCF引用与标准服务引用。

Take a look at WCF: Svcutil generates invalid client proxy, Apache AXIS Web Service, overload operations for further discussion. 看看WCF:Svcutil生成无效的客户端代理,Apache AXIS Web Service,重载操作以供进一步讨论。

In short, use Add Web Reference on the Advanced page of Add Service Reference: 简而言之,在Add Service Reference的Advanced页面上使用Add Web Reference:

添加Web引用

To me this error looks like an issue with generating your proxy file. 对我来说,这个错误看起来像是生成代理文件的问题。 You probably want to re-proxy by using svcutil to make sure your proxy file is being generated correctly. 您可能希望使用svcutil重新代理,以确保正确生成代理文件。 In your case, the command in your Visual Studio Developer Command tool would look like so... 在您的情况下,Visual Studio Developer Command工具中的命令将如此...

svcutil.exe /language:cs /out:Proxies.cs /config:output.config [service url]

It also doesn't look like you're establishing a secure session in the first Place. 它看起来也不像你在第一时间建立安全会话。 Change your binding to the following... 将绑定更改为以下内容...

<binding name="EveryWanDeviceSoapBinding" 
         closeTimeout="00:01:00" 
         openTimeout="00:01:00" 
         receiveTimeout="00:10:00" 
         sendTimeout="00:01:00" 
         allowCookies="false" 
         bypassProxyOnLocal="false" 
         hostNameComparisonMode="StrongWildcard" 
         maxBufferSize="6553666" 
         maxBufferPoolSize="524288" 
         maxReceivedMessageSize="6553666" 
         messageEncoding="Text" 
         textEncoding="utf-8" 
         transferMode="Buffered" 
         useDefaultWebProxy="true">
    <security mode="Transport">
        <transport clientCredentialType="Basic" 
                   proxyCredentialType="Basic" 
                   realm="" />
        <message clientCredentialType="UserName" 
                 algorithmSuite="Default" />
    </security>
</binding>

The vast majority of that are just basic defaults for a service binding. 绝大多数只是服务绑定的基本默认值。 The security part is what should let you establish a secure connection to the service like so... security部分应该让您建立与服务的安全连接,如此...

var srv = new DeviceServiceClient();

srv.ClientCreditials.UserName.UserName = "Admin";
srv.ClientCreditials.UserName.Password = "XXXXX";

Finally you can call the getDeviceProperties method with your arguments and get some sort of a response back. 最后,您可以使用您的参数调用getDeviceProperties方法并获得某种响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM