UPDATE:因爲很清楚您使用的是MTOM,所以將其替換爲答案。
顯然這是WCF中的bug,根據this被標記爲「延遲」。所以很難說,什麼時候它會被修復。最後一個鏈接還提到了一些解決方法,我將在這裏重現,以便他們不會迷路。
- 不要使用MTOM
- 添加一些前綴字符發送者和接收者瞭解,並且然後接收器可以剝離(如報價等)。
- 使用消息檢查和緩衝消息
下面的代碼顯示了這個問題的第三個解決方法:
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。
您必須對XML名稱進行編碼。 http://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents –
從來沒有聽說過這樣的事情,並會驚訝,如果這是別的一個錯誤。客戶端和服務器都是使用WCF的.NET嗎? –
@KevinRaffay肯定不是。 (Net)DataContractSerializer(「WCF」)將爲您做到這一點。 –