Powershell如何通过IIS使用WCF REST服务?

Gre*_*son 2 c# wcf iis-7.5 powershell-3.0

意图:我需要从自动服务器组向数据库提交一系列测试报告.测试脚本是python,而不是打开蠕虫,我想设置一个Powershell脚本来将完成的测试推送到WCF服务.

我有一个现有的ASP.NET网站来管理报告,并建立起来我想添加一个可以通过自动化访问的简单WCF服务.我似乎碰到了墙壁,觉得我非常接近这个工作.我已经在这个问题上工作了几天,我的眼睛开始流血阅读网站,同时试图找到相关的帮助.

服务代码:

namespace TrendDB.Web
{
    [ServiceContract]
    public interface ITrendRestService
    {
        [OperationContract]
        [WebGet]
        string GetTestMessage();
    }

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TrendRestService : ITrendRestService
    {
        public string GetTestMessage()
        {
            return "Success!";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Web.config(仅显示相关信息):

<system.web>
...
  <authentication mode="Windows" />
  <authorization>
    <deny users="?" />
  </authorization>
...
</system.web>
...
<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="restfulBehavior">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  <services>
    <service name="TrendDB.Web.TrendRestService">
      <endpoint address="" behaviorConfiguration="restfulBehavior"
                binding="webHttpBinding" bindingConfiguration="" contract="TrendDB.Web.ITrendRestService">
      </endpoint>
    </service>
  </services>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

我测试了这个在我的浏览器中工作,http://localhost/trenddb/TrendRestService.svc/GetTestMessage并且发出的消息是:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Success!</string>
Run Code Online (Sandbox Code Playgroud)

IIS 7.5是使用MS的BKM设置的,用于内部网的角色授权的MVC使用.该站点设置了特定的应用程序池,并使用Windows身份验证在域中设置服务器.

我第一次打电话给Powershell开始时:

$trend = New-WebServiceProxy -uri http://localhost/trenddb/TrendRestService.svc

New-WebServiceProxy : The request failed with HTTP status 401: Unauthorized.
Run Code Online (Sandbox Code Playgroud)

这很好,因为至少那是对匿名访问的预制响应,所以我添加-UseDefaultCredential到命令并得到:

$trend = New-WebServiceProxy -uri http://localhost/trenddb/TrendRestService.svc -UseDefaultCredential
New-WebServiceProxy : Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

我尝试使用GetTestMessageWebInvoke只是为了看看发生了什么:

$trend = New-WebServiceProxy -uri http://localhost/trenddb/TrendRestService.svc/GetTestMessage -UseDefaultCredential
New-WebServiceProxy : The document at the url 
http://localhost/trenddb/TrendRestService.svc/GetTestMessage was not recognized as a known document type.
The error message from each known type may help you fix the problem:
- Report from 'WSDL Document' is 'There is an error in XML document (1, 2).'.
  - <string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'> was not expected.
- Report from 'XML Schema' is 'The root element of a W3C XML Schema should be <schema> and its namespace should be 'http://www.w3.org/2001/XMLSchema'.'.
- Report from 'DISCO Document' is 'Discovery document at the URL http://localhost/trenddb/TrendRestService.svc/GetTestMessage could not be found.'.
  - The document format is not recognized.
Run Code Online (Sandbox Code Playgroud)

从我的浏览器调用到WCF,我知道这<string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'>正是我所期待的...所以我现在需要弄清楚连接到这个WCF REST服务所需的powershell.

编辑: 使用svcutil.exe实用程序创建代理,但跳过配置文件.该行是否serviceMetadata httpGetEnabled="true"意味着我正在生成可用于WCF客户端的元数据?这会影响PS吗?

Ric*_*ney 9

New-WebServiceProxy为Web服务(例如.NET中的.asmx)创建"Web服务"代理.这些服务是WS-I-Basic Profile 1.1服务.换句话说,有一种对WSDL的期望.

您的WCF服务只有一个端点,即WebHttpBinding端点.这种端点用于HTTP客户端(非SOAP),并且通常由REST客户端使用,因为它们仅使用Web协议(HTTP和适当的动词,如GET,POST,...).当您使用浏览器对此进行测试时,它的工作原理是因为浏览器只是HTTP客户端.当您使用来自New-WebServiceProxy的代理测试它时,它试图将响应解释为WSDL而不仅仅是字符串,因为这是New-WebServiceProxy创建的客户端类型.

要使WCF服务适用于Web服务客户端(例如New-WebServiceProxy),您需要使用他们理解的绑定为这些客户端添加另一个端点,即basicHttpBinding.

      <endpoint address="basic" binding="basicHttpBinding" contract="TrendDB.Web.ITrendRestService" />
Run Code Online (Sandbox Code Playgroud)

这至少会让它适用于两种类型的客户端(减去安全性).

为了安全起见,为basicHttpBinding创建一个BindingConfiguration,为webHttpBinding创建一个.在那里,您可以将客户端凭据类型指定为Windows.

  • @Brian:至于"Greg可能没有欣赏它",我认为你的意思是我可能不完全欣赏Rick所提供的内容,但我无知我正在克服而不是缺乏对答案的感激之情.我认为这个解决方案正是我所需要的.在我提出任何问题或提供回复之前,我没有足够的时间重新实现这一点并希望有时间使用它. (2认同)