gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:11:01 26 4
gpt4 key购买 nike

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

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

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
}

最佳答案

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

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

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

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

关于.net - 显式定义枚举值时 WCF 客户端失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35136989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com