显式定义枚举值时 WCF 客户端失败

WCF Client fails when defining enum values explicitly

我的问题与 this 类似,但有一个有趣的区别。我已经明确定义了所有值,但它仍然不起作用。如果可能的话,我也会尽量避免合同属性。

所以这一切看起来像什么。 我定义了一个具有 属性 枚举类型的合约。枚举是

public enum ErrorCodes
{
    GeneralError = 1,
    ValidationError = 2,
    AuthenticationError = 3
}

然后客户端失败并出现错误:

System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP response to http://localhost/MyTestAppService/SomeService.svc/soap. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at MyTestApp.ISomeService.TestMethod(TestRequest request)

当我删除所有显式值时,它起作用了:

public enum ErrorCodes
{
    GeneralError,
    ValidationError,
    AuthenticationError
}

我认为这与您未在示例中显示的数据合同有关。

这是我在 MSDN 文档中找到的内容。 https://msdn.microsoft.com/en-us/library/aa347875(v=vs.110).aspx

在您链接到的答案中,用 [EnumMember] 装饰的枚举和设置的值,如下所示:

[DataContract]
public enum ErrorCodes
{
    [EnumMember(Value = "1")]
    GeneralError = 1,
    [EnumMember(Value = "2")]
    ValidationError = 2,
    [EnumMember(Value = "3")]
    AuthenticationError = 3
}

您发布的代码似乎缺少这些属性。

已添加

虽然您不需要使用 [EnumMember] 属性(根据 documentation),但它 应用于简单枚举。在 "Simple Enumerations" 部分下,它说:

You can use simple enumerations when you *do not need to customize* the enumeration's data contract name and namespace and the **enumeration member values**.

我终于找到了一个非常简单的解决方案。文档中没有提到它似乎很奇怪。

枚举只需要有一个默认值,一切都很顺利!不知道这是一个很好的错误还是未记录的功能。

public enum ErrorCodes
{
    Default = 0, // without this member WCF fails
    GeneralError = 1,
    ValidationError = 2,
    AuthenticationError = 3
}

我是怎么想到的?我只是尝试了其他枚举,它甚至在具有显式值的情况下也能正常工作,幸运的是,不同之处在于它具有默认值。