Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF catches exception " The server did not provide a meaningful reply.."

Tags:

wcf

after a server call my client catches an exception with the following message

"The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."

Also, note I tried the configuration in WCF "The server did not provide a meaningful reply" but still didn't work.

Please note, that I debug the service to the end, and the data is successfully populated but at the client end when the data is supposed to appear it just crashes with the mentioned exception.

Any help appreciated.

like image 358
Amr Elsehemy Avatar asked Feb 25 '23 09:02

Amr Elsehemy


2 Answers

I figured out the reason behind this that the proxy was wrongly generated for an enum type, it was generated as a string so it failed and gave me out that exception

like image 125
Amr Elsehemy Avatar answered Mar 08 '23 22:03

Amr Elsehemy


If someone else comes across this same exception, with the same behavior of debugging to the end in the server, but getting the exception on the return to the client, another possible cause is an uninitialized enum in the return data contract where the enum has explicit values, but no explicit zero value.

[ServiceContract]
public interfact IMyService
{
    [OperationContract]
    MyResult DoSomething();
}

[DataContract]
public class MyResult
{
    [DataMember]
    public OperationStatus Status {get; set;}

    [DataMember]
    public string ErrorText {get; set;}
}

[DataContract]
public enum Operation Status
{
    [EnumMember]
    Success = 1,
    [EnumMember]
    Failure = 2
}

public class MyService : IMyService
{
    public MyResult DoSomething()
    {
        var result = new MyResult();

        // ... do work, but don't set any properties on result ...

        return result;

    }
}

The reason the error happens in this scenario is that result.Status defaults to zero, but the data contract does not provide any means to serialize it, since it is not one of the explicitly defined enum values.

The solution (assuming you really do need this enum with explicit integer values) is to either set the enum value in the return object, or provide a default (zero) value.

[DataContract]
public enum Operation Status
{
    [EnumMember]
    Unknown = 0,
    [EnumMember]
    Success = 1,
    [EnumMember]
    Failure = 2
}

--Bill

like image 28
Bill Avatar answered Mar 08 '23 23:03

Bill