2016-02-01 17 views
1

我的問題類似於this但有一個有趣的區別。我已經明確定義了所有的值,但它仍然無效。如果可能的話,我也嘗試避免契約屬性。WCF客戶端在明確定義枚舉值時失敗

所以它看起來像什麼。 我定義了一個具有枚舉類型屬性的合同。枚舉是

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

然後客戶端無法與errror:

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 
} 
+0

您是否按照鏈接問題裝飾了[EnumMember(Value =「1」)]'的枚舉? – Tim

+0

不,我試圖避開它 – neleus

+0

爲什麼?問題的根本原因很可能是序列化錯誤,那麼爲什麼要避免'[DataContract]'屬性系列? – Tim

回答

3

我終於找到一個非常簡單的解決方案。文檔沒有提到它似乎很奇怪。

該枚舉只需要有一個默認值和一切工作就像一個魅力!不知道這是一個不錯的bug還是沒有記錄的功能。

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

我是怎麼知道的?我只是嘗試其他枚舉,它甚至有明確的值,幸運的是它有默認值。

+0

有趣 - 感謝您發佈答案。 – Tim

0

我會假設它會做數據合同,你沒有在你的例子中顯示。

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

+0

見下面,有一節'Simple Enumerations'就是我的情況 – neleus

+0

簡單狀態下的子句 「當您不需要定製枚舉的數據協定名稱時可以使用簡單的枚舉,命名空間和枚舉成員值。「您正在嘗試調整這些值。 – DoomVroom

3

在回答您鏈接到,凡與[EnumMember]裝飾和設置的值,這樣的枚舉:出現

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

您發佈的代碼中缺少這些屬性。

ADDED

儘管你不需要使用[EnumMember]屬性(每documentation),適用簡單枚舉。 「簡單枚舉」部分,它說:

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

+0

根據msdn的屬性可以省略https://msdn.microsoft.com/en-us/library/aa347875%28v=vs.110%29.aspx – neleus

+0

你認爲有沒有辦法避免這些屬性?想象一下,有許多具有十六進制值的成員,在這種情況下,重複屬性中的值並不優雅。我儘量避免這種情況 – neleus

+0

@neleus - 請參閱我上面的答案。 – Tim