Powershell如何通过IIS使用WCF REST服务
本文关键字:WCF REST 服务 使用 IIS 何通过 Powershell | 更新日期: 2023-09-27 18:27:30
意图:我需要从自动化服务器组向数据库提交一系列测试报告。测试脚本在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!";
}
}
}
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>
我将其作为http://localhost/trenddb/TrendRestService.svc/GetTestMessage
在浏览器中进行了测试,得到的消息是:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Success!</string>
IIS 7.5是使用MS中的BKM设置的,用于intranet角色授权的MVC使用。该站点设置了特定的应用程序池,并且服务器是使用Windows身份验证在域中设置的。
我对Powershell的第一次调用开始时为:
$trend = New-WebServiceProxy -uri http://localhost/trenddb/TrendRestService.svc
New-WebServiceProxy : The request failed with HTTP status 401: Unauthorized.
这很好,因为至少这是对匿名访问的固定响应,所以我在命令中添加了-UseDefaultCredential
,得到:
$trend = New-WebServiceProxy -uri http://localhost/trenddb/TrendRestService.svc -UseDefaultCredential
New-WebServiceProxy : Object reference not set to an instance of an object.
我试着使用GetTestMessage
WebInvoke来查看发生了什么:
$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.
从我对WCF的浏览器调用中,我知道<string xmlns='http://schemas.microsoft.com/2003/10/Serialization/'>
正是我所期望的。。。所以我现在需要弄清楚连接到这个WCF REST服务所需的powershell。
编辑:使用svcutil.exe实用程序创建一个代理,但跳过配置文件。行serviceMetadata httpGetEnabled="true"
是否意味着我正在生成可用于WCF客户端的元数据?这会影响PS吗?
New WebServiceProxy为web服务(如.NET中的.asmx)创建一个"web服务"代理。这些类型的服务是WS-I-Basic Profile 1.1服务。换句话说,这是对WSDL的期望。
您的WCF服务只有一个端点,即WebHttpBinding端点。这种端点用于HTTP客户端(非soap),通常由REST客户端使用,因为它们只使用web协议(HTTP和适当的动词,如GET、POST…)。当您使用浏览器进行测试时,它有效,因为浏览器只是HTTP客户端。当您使用NewWebServiceProxy中的代理进行测试时,它试图将响应解释为WSDL,而不仅仅是字符串,因为这是NewWebServiceProxy创建的客户端类型。
要使WCF服务适用于Web服务client(如NewWebServiceProxy),您需要使用他们理解的绑定为这些client添加另一个端点,即basicHttpBinding。
<endpoint address="basic" binding="basicHttpBinding" contract="TrendDB.Web.ITrendRestService" />
这将至少使它适用于两种类型的客户端(减去安全性)。
为了安全起见,为basicHttpBinding和webHttpBinding分别创建一个BindingConfiguration。在那里,您可以将客户端凭据类型指定为Windows。