简体   繁体   English

WCF中的WCF服务引用未使方法类型正确

[英]WCF Service reference in WCF not getting method type correct

WCF service library specifies: WCF服务库指定:

[OperationContract]
void SaveData(IDictionary visitorData);

Adding a service reference in the consuming MVC project to the wcf library (must uncheck "Reuse types in referenced assemblies" to make it generate code) causes it to generate the following in Reference.cs: 在使用MVC项目中将服务引用添加到wcf库(必须取消选中“在引用的程序集中重用类型”以使其生成代码)会导致它在Reference.cs中生成以下内容:

public void SaveData(System.Collections.Generic.Dictionary<object, object> visitorData) {
    base.Channel.SaveData(visitorData);
}

As a result, when I call 结果,当我打电话

visitorActions.SaveData(requestInfo);

(with requestInfo of type IDictionary ) I get, to my utter lack of surprise, the following compiler error: (使用IDictionary类型的requestInfo )我完全没有意外地得到以下编译器错误:

Argument 1: cannot convert from 'System.Collections.IDictionary' to 'System.Collections.Generic.Dictionary'... 参数1:无法从'System.Collections.IDictionary'转换为'System.Collections.Generic.Dictionary'...

Yes, I can go in and change the code to explicitly say IDictionary but Reference.cs is autogenerated and the next time I update from the service, my changes will be gone. 是的,我可以进入并更改代码以明确说出IDictionary但是Reference.cs是自动生成的,下次我从服务更新时,我的更改将会消失。

So what gives? 什么给出了什么?

The Configure Service Reference dialog box enables you to configure the behavior of generated proxies. 使用“ 配置服务引用”对话框可以配置生成的代理的行为。 This includes options for configuring what types are used for collections and dictionaries. 这包括用于配置用于集合和词典的类型的选项。 If IDictionary isn't in there you might not be able to generate a proxy that uses it from within Visual Studio. 如果IDictionary不在那里,您可能无法生成在Visual Studio中使用它的代理。

But remember that the generated clients simply exchange data in an agreed upon format. 但请记住,生成的客户端只是以商定的格式交换数据。 If you can get your hands on an assembly containing your service contract (or a service contract in the right format) you can use a channel factory to generate a client, instead. 如果您可以使用包含服务合同(或正确格式的服务合同)的程序集,则可以使用渠道工厂生成客户端。 The format of data sent over-the-wire will be the same and therefore compatible with your service. 通过线路发送的数据格式将相同,因此与您的服务兼容。

You can see a simple example of channel factories here . 您可以在此处查看渠道工厂的简单示例。

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

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