从powershell连接到WCF Rest Service应用程序不能按预期工作

时间:2017-10-13 14:08:52

标签: c# powershell wcf

我设置了一个WCF服务应用程序,所以当我调用该地址时,它只返回true。

IRemoteService.cs

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

RemoteService.svc.cs

namespace RemoteService
{    
    public class RemoteService : IRemoteService
    {
        public bool ValidationResult()
        {
            return true;
            //throw new NotImplementedException();
        }
    }
}

我在IIS上添加了一个应用程序,现在我可以通过以下网址访问该服务:

https://localhost/ValidationServiceApp/RemoteService.svc/validationresult/

返回:

{"ValidationResultResult":true}

效果很好。但是,当我运行以下powershell脚本时,我无法访问该服务:

$url = "https://localhost/ValidationServiceApp/SASRemoteService.svc/validationresult/"
$result = Invoke-RestMethod -Method GET -Uri $url 
Write-Host $result

我必须指出,我已经尝试过一个名为“我只是休息”的客户端应用程序。这会返回预期的结果。所以我认为它必须与powershell有关。要么我还没有在web.config文件中允许某些内容,要么在PowerShell中缺少某些设置。我还试图忽略powershell上的证书错误。这没有任何帮助。

来自powershell的错误:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:18 char:11
+ $result = Invoke-RestMethod -Method GET -Uri $url
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

以下是该服务的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)

管理以找到问题。

我需要在IIS中编辑托管服务的权限。所以我将用户({pcname}/Users)添加到允许用户列表中。

我会删除这个问题,但也许这可能有助于未来的人:)