繁体   English   中英

从 powershell 连接到 WCF Rest 服务应用程序未按预期工作

[英]Connecting to WCF Rest Service Application from powershell not working as expected

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

远程服务.cs

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

远程服务.svc.cs

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

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

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>

设法找到了问题。

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

我会删除这个问题,但也许这可能会在将来对某人有所帮助:)

暂无
暂无

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

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