明确定义枚举值时,WCF客户端失败

时间:2016-02-01 17:46:11

标签: .net wcf enums datacontract

我的问题与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
}

3 个答案:

答案 0 :(得分:4)

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

枚举只需要默认值,一切都像魅力一样!不知道这是一个很好的bug还是未记录的功能。

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

我是怎么来的?我只是尝试了其他枚举,它甚至有明确的值,幸运的是差异在于它有默认值。

答案 1 :(得分:3)

在你链接的答案中,用[EnumMember]装饰的枚举和值集,如下所示:

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

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

<强> ADDED

虽然您不需要使用[EnumMember]属性(根据documentation),但适用于简单的枚举。根据&#34;简单枚举&#34;部分,它说:

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**.

答案 2 :(得分:0)

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

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