c# - 可以通过浏览器访问 WCF 服务,但不能通过 PowerShell 中的 GET

标签 c# powershell wcf

我设置了一个 WCF 服务,它可以通过浏览器正常工作,它返回我期望的结果。现在,当尝试通过 PowerShell 访问它时,出现此错误

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.

我将其作为一个应用程序托管在 IIS 上,它指向我系统上服务的位置,称为“ValidationServiceApp”,因此我可以通过以下方式访问它:“https://myPc/ValidationServiceApp/RemoteService.svc/验证结果/"

这是 PowerShell 代码:

[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} #handle unsigned certs as its on my local machine on IIS
$testurl = "https://myPc/ValidationServiceApp/RemoteService.svc/validationresult/"
$result = Invoke-WebRequest -Method GET -Uri $testurl 
Write-Host $result

VS中的服务:

IRemoteService.cs

   [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped,
           UriTemplate = "ValidationResult/")]
        bool ValidationResult();

远程服务.svc.cs

public bool ValidationResult()
{
    return true;
    //throw new NotImplementedException();
}

Web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6"/>
    <httpRuntime targetFramework="4.6"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>    
        <binding name="webHttpTransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="RemoteService.RemoteService" behaviorConfiguration="ServiceBehaviour">        
        <endpoint address ="" 
                  binding="webHttpBinding" 
                  contract="RemoteService.IRemoteService" 
                  bindingConfiguration="webHttpTransportSecurity" 
                  behaviorConfiguration="web" />

        <endpoint address="mex"
                   binding="mexHttpsBinding"
                   contract="IMetadataExchange" /> 
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="false" 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>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>      
      <add binding="webHttpBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>

</configuration>

最佳答案

考虑使用 New-WebServiceProxy在 PowerShell 中使用 WCF Web 服务时的 cmdlet。

$proxy= New-WebServiceProxy -Uri "https://myPc/ValidationServiceApp/RemoteService.svc"
# retrieve available methods:
$proxy | Get-Member 

关于c# - 可以通过浏览器访问 WCF 服务,但不能通过 PowerShell 中的 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769457/

相关文章:

c# - 更改事件字符串 c#

exception - 不可捕获的异常(PowerShell)

javascript - Angular js 应用程序无法处理多请求

c# - 在对 PowerShell 脚本的 AfterBuild 调用中引用自定义 msbuild 参数

c# - 调用 sync 和 async 方法有 await 区别?

powershell - 如何在新窗口中从 .bat 启动 PowerShell 并运行 .ps1 而不退出控制台?

sql-server - 如何使用 PowerShell 获取 SQL Server 中表的唯一键列表?

.net - ADO .Net - 如何在具有多个 IP 地址的系统上的 SqlConnection 中指定源 IP 地址

wcf - Silverlight 4 安全 LOB 应用程序 - WCF 与 WCF Ria 服务

c# - 正则表达式修改