2017-04-26 28 views
0

消失我有屬性Name我在WCF用來傳送一個模型(通信)的對象A空間上通過WCF轉印(XML)

[DataMember(IsRequired = false, EmitDefaultValue = false, Name = "p0", Order = 0)] 
public string Name { get; set; } 

我discoverd當Name開始與當時在另一邊它已經失去了空間,併成爲「123」 deserialisation後空間「123」。

的WCF服務使用MTOM消息編碼。

這是在一般的XML或WCF已知的效果呢?

的答案的幫助下提供我發現,領先的空格是由於MTOM編碼中刪除。事實上,當我刪除Mtom時,領先的空白區域被正確轉移。

的安全配置並沒有我的情況下起到任何作用。

有沒有辦法避免它?

+0

您必須對XML名稱進行編碼。 http://stackoverflow.com/questions/1091945/what-c​​haracters-do-i-need-to-escape-in​​-xml-documents –

+0

從來沒有聽說過這樣的事情,並會驚訝,如果這是別的一個錯誤。客戶端和服務器都是使用WCF的.NET嗎? –

+0

@KevinRaffay肯定不是。 (Net)DataContractSerializer(「WCF」)將爲您做到這一點。 –

回答

1

UPDATE:因爲很清楚您使用的是MTOM,所以將其替換爲答案。

顯然這是WCF中的bug,根據this被標記爲「延遲」。所以很難說,什麼時候它會被修復。最後一個鏈接還提到了一些解決方法,我將在這裏重現,以便他們不會迷路。

  1. 不要使用MTOM
  2. 添加一些前綴字符發送者和接收者瞭解,並且然後接收器可以剝離(如報價等)。
  3. 使用消息檢查和緩衝消息

下面的代碼顯示了這個問題的第三個解決方法:

public class Post_4cfd1cd6_a038_420d_8cb5_ec5a2628df1a 
{ 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     string Echo(string text); 
    } 
    public class Service : ITest 
    { 
     public string Echo(string text) 
     { 
      Console.WriteLine("In service, text = {0}", ReplaceControl(text)); 
      return text; 
     } 
    } 
    static Binding GetBinding() 
    { 
     //var result = new WSHttpBinding(SecurityMode.None) { MessageEncoding = WSMessageEncoding.Text }; 
     var result = new BasicHttpBinding() { MessageEncoding = WSMessageEncoding.Mtom }; 
     return result; 
    } 
    static string ReplaceControl(string text) 
    { 
     StringBuilder sb = new StringBuilder(); 
     foreach (var c in text) 
     { 
      if ((' ' <= c && c <= '~') && c != '\\') 
      { 
       sb.Append(c); 
      } 
      else 
      { 
       sb.AppendFormat("\\u{0:X4}", (int)c); 
      } 
     } 

     return sb.ToString(); 
    } 
    public class MyInspector : IEndpointBehavior, IDispatchMessageInspector, IClientMessageInspector 
    { 
     public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) 
     { 
     } 

     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) 
     { 
      clientRuntime.MessageInspectors.Add(this); 
     } 

     public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) 
     { 
      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this); 
     } 

     public void Validate(ServiceEndpoint endpoint) 
     { 
     } 

     public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) 
     { 
      request = request.CreateBufferedCopy(int.MaxValue).CreateMessage(); 
      return null; 
     } 

     public void BeforeSendReply(ref Message reply, object correlationState) 
     { 
     } 

     public void AfterReceiveReply(ref Message reply, object correlationState) 
     { 
      reply = reply.CreateBufferedCopy(int.MaxValue).CreateMessage(); 
     } 

     public object BeforeSendRequest(ref Message request, IClientChannel channel) 
     { 
      return null; 
     } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     var endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), ""); 
     endpoint.Behaviors.Add(new MyInspector()); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress)); 
     factory.Endpoint.Behaviors.Add(new MyInspector()); 
     ITest proxy = factory.CreateChannel(); 

     string input = "\t\tDoc1\tCase1\tActive"; 
     string output = proxy.Echo(input); 
     Console.WriteLine("Input = {0}, Output = {1}", ReplaceControl(input), ReplaceControl(output)); 
     Console.WriteLine("input == output: {0}", input == output); 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 

再次,真正的答案和代碼是從here

+0

這是HTTP與MTOM綁定名稱=「BasicHttpBinding」closeTimeout =「00:01:00」openTimeout =「00:01:00」receiveTimeout =「00:01:00」sendTimeout =「00:01:00」maxBufferPoolSize =「2147483647」maxBufferSize =「2147483647」maxReceivedMessageSize =「2147483647」transferMode =「Streamed」useDefaultWebProxy =「true」messageEncoding =「Mtom」' – Gerard

+0

還有'security mode =「None」'這是您的第二個鏈接中的mentiod。 – Gerard

+0

所以,你有它;-) –