【发布时间】:2011-08-26 11:09:29
【问题描述】:
我正在使用 WCF 订阅 SQL Server 2008 SSRS Web 服务 (.../reportserver/ReportService2005.asmx?wsdl),据我所知,默认 WCF 配置选项。
虽然它在生成本地代理类时会做一些奇怪的事情。
我以 ListChildren 方法为例:
在客户端,WCF 会生成这样的界面,正如您所期望的那样:
public interface ReportingService2005Soap {
ListChildrenResponse ListChildren(ListChildrenRequest request);
}
它还会生成一个实现该接口的“客户端”代理:
public partial class ReportingService2005SoapClient :
System.ServiceModel.ClientBase<ReportingService2005Soap>, ReportingService2005Soap
{
[EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
ListChildrenResponse ReportingService2005Soap.ListChildren(ListChildrenRequest request)
{
return base.Channel.ListChildren(request);
}
public ServerInfoHeader ListChildren(string Item, bool Recursive, out CatalogItem[] CatalogItems) {
ListChildrenRequest inValue = new ListChildrenRequest();
inValue.Item = Item;
inValue.Recursive = Recursive;
ListChildrenResponse retVal = ((ReportingService2005Soap)(this)).ListChildren(inValue);
CatalogItems = retVal.CatalogItems;
return retVal.ServerInfoHeader;
}
}
如您所见,客户端代理实现了接口,然后通过显式实现接口(因此您必须强制转换以获取接口方法)并另外使用 EditorBrowsableState.Advanced 属性来“隐藏”它以使其不被使用。
然后它添加了一个使用“out”参数的额外包装方法。
如果不这样做,有没有办法停止,直接实现接口?
它在这里所做的事情会引导您使用带有“out”参数的包装器方法,然后您会发现您不能很容易地模拟服务,因为包装器方法不是虚拟的,也没有定义在任何界面中。
注意:我在这里使用 SSRS Web 服务作为示例,但我已经看到 WCF 在其他服务上也这样做了。
【问题讨论】:
-
如果您进入添加服务参考的高级设置并检查
Always generate message contracts会发生什么? -
@Ladislav :是的,这似乎解决了它。如果您发布答案,我可以投票给您,谢谢。
-
我的问题获得了“热门问题”徽章(1000 次浏览),但没有一个支持。我应该为此获得反徽章:S