【问题标题】:soapURL to call C# Windows service via Soap in PHP在 PHP 中通过 Soap 调用 C# Windows 服务的soapURL
【发布时间】:2018-02-26 12:29:00
【问题描述】:

问题:

我继承了一个使用 PHP 调用 C# windows 服务的软件。它不能正常工作,所以我才刚刚开始使用 WCF 和 SOAP。

我的主要问题是我无法使用 PHP 和 Soap 调用 Windows 服务。当我尝试它时,PHP 崩溃了。

我完成了微软教程: https://docs.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial 当我按照教程中的描述使用 C# 客户端时,一切都像魅力一样工作。我添加了一个安装程序以使整个程序可作为 Windows 服务使用。长话短说,在 C# 中一切正常。

我假设它只是没有为 SOAP 调用正确配置。我已经通读了:http://www.rizalalmashoor.com/blog/calling-a-wcf-service-from-php/,但我仍然无法运行它。

问题:

源代码在下面。我想先提出我的问题:

1)我在基地址公开服务,对吧??

http://localhost:1234/GettingStartedLib/CalculatorService

2) http 端点为空。 PHP 代码中的soapURL 是否正确?

3) 有没有办法调试soap 调用?即使我在 ZendStudio 中将它作为 CLI 应用程序进行调试,我也没有得到任何进一步的信息。

如果需要或解释不充分,我很乐意提供更多信息。

  • 在有人回答之前:在 php.ini 中启用了 Soap。已经检查过了。

源码:

PHP:

$soapURL = 'http://localhost:1234/GettingStartedLib/CalculatorService?wsdl';
$soapAttributes = array('soap_version' => SOAP_1_1);

$result = new SoapClient($soapURL, $soapAttributes);

WCF库的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="GettingStartedLib.CalculatorService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:1234/GettingStartedLib/CalculatorService"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.ICalculator">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
</configuration>

C# 接口

namespace GettingStartedLib
{
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
        [OperationContract]
        string SoapCall();
    }
}

C# 服务本身:

namespace GettingStartedLib
{
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2) {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            // Code added to write output to the console window.  
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2) {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2) {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2) {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
        public string SoapCall() => "SoapCall";
    }
}

【问题讨论】:

  • “PHP 崩溃”看来你需要一个 PHP 教程,因为你说 c# 端可以正常工作
  • 错误是什么?将您的 PHP 包装在 try/catch 中并将其用作捕获:catch (SoapFault $fault) { echo(var_dump($fault)); }
  • 感谢您的意见。有时进步可能很容易。我收到以下错误 SOAP-ERROR: Parsing WSDL: Couldn't load from ...。它看起来像一个 Windows 问题。当我使用 file_get_contents('localhost:1234/GettingStartedLib/CalculatorService?wsdl'); 运行脚本时,即使我在控制台中以管理员权限运行脚本,它也会说计算机拒绝权限。我在这里的 Windows 配置中遗漏了什么吗?
  • 如果想将本地文件复制到 php 应用程序可以读取的文件系统,您可以使用本地文件而不是托管 wsdl。
  • 该文件已经位于我的文件系统中。我很高兴人们花时间尝试提供帮助,但我的前两个问题尚未得到解答。我对这个话题研究得越多,我就越想回答这两个问题。此外,如果我的假设是正确的,还有什么可能出错?

标签: php wcf soap windows-services windows-administration


【解决方案1】:

解决了这个问题。我必须输入

<baseAddresses><add baseAddress="localhost:1234/GettingStartedLib/CalculatorService.vsc"/>
</baseAddresses>

由于我将http端点的地址留空,所以soap URL是localhost:1234/GettingStartedLib/CalculatorService.vsc?wsdl

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2022-07-03
    相关资源
    最近更新 更多