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

时间:2017-10-16 11:39:33

标签: 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/validationresult/"

以下是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();

RemoteService.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>

1 个答案:

答案 0 :(得分:1)

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

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