user4593252
user4593252

Reputation: 3506

WCF Service reference in WCF not getting method type correct

WCF service library specifies:

[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:

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:

Argument 1: cannot convert from 'System.Collections.IDictionary' to '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.

So what gives?

Upvotes: 5

Views: 2068

Answers (1)

ta.speot.is
ta.speot.is

Reputation: 27214

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.

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.

Upvotes: 1

Related Questions