2013-08-20 7 views
1

나는 여러 가지 방법으로 WCF 서비스를 가지고 있습니다. 어떻게 보내 졌는지에 관계없이 클라이언트에서받은 원시 요청을 기록하고 싶습니다. 아래의 메소드를 사용하고 있습니다.문자열 헤더가없는 XML 본문에서

XML은 결과와 문자열에 있지만 본문에는 있지만 문자열은 본문에 있지만 헤더는 전혀 필요하지 않습니다. 이 점에서 아무도 나를 도울 수 있습니까?

+0

이 사람이 어떤 제안을 가질 수 ... – Sherry

답변

1

마지막으로 정렬하여 Body Writer Class를 덮어 씁니다. 아래의 자세한 솔루션을 찾으십시오.

namespace WcfServiceRaw 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 

     [OperationContract(ReplyAction = "ResponseToGetDataRequest")] 
     Message GetData(); 
    } 

} 
namespace WcfServiceRaw 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    public class Service1 : IService1 
    { 

     public Message GetData() 
     { 
      // Create body 
      TestDataWriter body = new TestDataWriter("<test>data</test>"); 

      // Create messatge 
      MessageVersion ver = OperationContext.Current.IncomingMessageVersion; 
      Message msg = Message.CreateMessage(ver, "ResponseToGetDataRequest", body); 

      //Debug.WriteLine(msg.ToString()); 
      return msg; 

     } 

    } 

    class TestDataWriter : BodyWriter 
    { 
     string _data; 

     public TestDataWriter(string data) 
      : base(false) 
     { 
      _data = data; 
     } 

     protected override void OnWriteBodyContents(XmlDictionaryWriter writer) 
     { 
      writer.WriteRaw(_data); 
     } 
    } 

그리고 클라이언트는 다음과 같습니다

namespace WcfClientRaw 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //IMyContract proxy = ChannelFactory<IMyContract>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(address)); 
      WcfServiceRaw.Service1Client proxy = new WcfServiceRaw.Service1Client(); 
      using (proxy as IDisposable) 
      { 
       Message msg = proxy.GetData(); 
       Console.WriteLine(msg.ToString()); 
       Console.WriteLine(); 

       XmlDictionaryReader xdr = msg.GetReaderAtBodyContents(); 
       //string exp = "<test>data</test>"; 
       string act = xdr.ReadOuterXml(); 
       //Debug.Assert(exp == act); 
       Console.WriteLine(act); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 
+0

같은 문제를 가진 다른 사람들을 돕기위한 해결책으로 표시해주세요. 감사 –

0

GetReaderAtBodyContents은 System.ServiceModel.Channel.Message 클래스에서 사용할 수 있습니다.

var reader = message.GetReaderAtBodyContents(); 
Console.WriteLine(reader.ReadOuterXml()); 
+0

나는 아직도 몸에 여분의 태그를 얻을. 사실 아래에 언급 된 서비스 계약 문자열 인 Greet (string name)를 사용하고 있습니다. 및 공용 문자열 Greet (문자열 이름) { return "Hello,"+ name; } – Sherry

+0

"추가 태그"는 무엇입니까? –

+0

실제로 클라이언트 쪽에서 원시 데이터를 전달하는 중이고 xml body 태그에있는 그대로 원시 데이터를 쓰려고하지만 원시 데이터가 전달 된 코드가 내 코드에 태그를 쓸 때 Mr.Wasif 이것은 원시 데이터 메시지 교환을위한 테스트 응용 프로그램입니다. 이번에는 필요한 결과를 얻길 바랍니다. 실제로 저는 태그 [Mr.Wasif]에서 다음 데이터가 필요합니다. 이것은 원시 데이터 메시지 교환을위한 테스트 응용 프로그램입니다. 이번에는 필요한 결과를 얻으시기 바랍니다.] – Sherry

관련 문제