1

I have a WCF service with the following method:

[OperationContract]
TernaryWebServiceResponse<long> ModifyObservations(...);

Response object is as following:

[DataContract]
public class TernaryWebServiceResponse<T>
{
    [DataMember]
    public TernaryProcessingResultStatus ProcessingSuccessStatus { get; set; }

    [DataMember]
    public Dictionary<T, bool> ProcessingSuccessDetails { get; set; } 
}

This class is defined in a shared assembly and WCF reference is configured to reuse it.

But when I generate the proxy, I get something like this:

Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Referenced type 'TernaryWebServiceResponse`1, General, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' with data contract name 'TernaryWebServiceResponseOflong' in namespace 'http://schemas.datacontract.org/2004/Genaral.SoapCommunication' cannot be used since it does not match imported DataContract. Need to exclude this type from referenced types.

Can somebody plz explain me, what is exactly the problem here? I know that generics can be used in WCF contracts as long as they are resolved, e.g. not MyObj<T> but MyObj<long>. So what's wrong with this class? Is it that T defined on the class cannot be somehow resolved on the dictionary?

Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
Maxim Zabolotskikh
  • 3,091
  • 20
  • 21

2 Answers2

0

Maybe defining a new contract will help. Something like:

[DataContract] 
public class LongTernaryWebServiceResponse : TernaryWebServiceResponse<long> 
{ 
}

And then in your service contract something like:

[OperationContract]
LongTernaryWebServiceResponse ModifyObservations(...);
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
  • I marked it as an answer coz I actually used this in the end, although it does not explain why this happens. The problem was in fact with dictionary serialization. In some cases writing a serializable dictionary wrapper (http://stackoverflow.com/questions/1124597/why-isnt-there-an-xml-serializable-dictionary-in-net) would work, just in case smb else stubles over this question. – Maxim Zabolotskikh Nov 19 '12 at 08:31
0

If the generic type for the class is only needed for the Dictionary key, you may be able to do something along the lines of this (untested) ...

[DataContract]
public class TernaryWebServiceResponse
{

    [DataMember]
    public TernaryProcessingResultStatus ProcessingSuccessStatus { get; set; }

    [DataMember]
    private Dictionary<object, bool> mProcessingSuccessDetails;

    static public TernaryWebServiceResponse Create<T>(Dictionary<T, bool> list)
    {
        var dc = new TernaryWebServiceResponse();
        dc.mProcessingSuccessDetails = new Dictionary<object, bool>();
        foreach (var pair in list)
        {
            dc.mProcessingSuccessDetails.Add((object)pair.Key, pair.Value);
        }
        return dc;
    }

    public Dictionary<T, bool> ProcessingSuccessDetails<T>()
    {
        return mProcessingSuccessDetails.ToDictionary(x => ((T)x.Key), x => x.Value);
    }

}
Llwyd
  • 106
  • 1
  • 3