小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

[WCF 學(xué)習(xí)筆記] 4. 消息操作

 隱形的翅膀 2007-11-23
WCF 的一切都是圍繞著 Message 進(jìn)行,那么 Message 究竟是什么樣子?
[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  double Add(double a, double b);
}

public class CalculateService : ICalculate
{
  public double Add(double a, double b)
  {
    Message msg = OperationContext.Current.RequestContext.RequestMessage;
    Console.WriteLine(msg);

    return a + b;
  }
}

public class WcfTest
{
  public static void Test()
  {
    AppDomain.CreateDomain("Server").DoCallBack(delegate
    {
      ServiceHost host = new ServiceHost(typeof(CalculateService));
      host.AddServiceEndpoint(typeof(ICalculate), new BasicHttpBinding(),
        "http://localhost:8080/calc");

      host.Open();
    });

    ChannelFactory<ICalculate> factory = new ChannelFactory<ICalculate>(new BasicHttpBinding(),
      "http://localhost:8080/calc");
    ICalculate o = factory.CreateChannel();
    Console.WriteLine(o.Add(1, 2));
  }
}

輸出
<s:Envelope xmlns:s="http://schemas./soap/envelope/">
  <s:Header>
    <To s:mustUnderstand="1" xmlns="http://...">http://localhost:8080/calc</To>
    <Action s:mustUnderstand="1" xmlns="http://...">http:///ICalculate/Add</Action>
  </s:Header>
  <s:Body>
    <Add xmlns="http:///">
      <a>1</a>
      <b>2</b>
    </Add>
  </s:Body>
</s:Envelope>

事實(shí)上我們可以直接基于 Message Layer 進(jìn)行編程,利用 OperationContract.Action 捕獲特定 Action 的消息。
[ServiceContract]
public interface ICalculate
{
  [OperationContract(Action = "Add", ReplyAction="Add")]
  Message ProcessMessage(Message m);
}

public class CalculateService : ICalculate
{
  public Message ProcessMessage(Message m)
  {
    Data d = m.GetBody<Data>();
    Console.WriteLine(d.I);
    
    return Message.CreateMessage(MessageVersion.Soap11, "Add", new Data(9999));
  }
}

[DataContract]
public class Data
{
  [DataMember]
  public int I;

  public Data(int i)
  {
    this.I = i;
  }
}

public class WcfTest
{
  public static void Test()
  {
    AppDomain.CreateDomain("Server").DoCallBack(delegate
    {
      ServiceHost host = new ServiceHost(typeof(CalculateService));
      host.AddServiceEndpoint(typeof(ICalculate), new BasicHttpBinding(),
        "http://localhost:8080/calc");

      host.Open();
    });

    ChannelFactory<IRequestChannel> factory = new ChannelFactory<IRequestChannel>(
      new BasicHttpBinding(), "http://localhost:8080/calc");
    IRequestChannel channel = factory.CreateChannel();
    channel.Open();

    Message request = Message.CreateMessage(MessageVersion.Soap11, "Add", new Data(1234));
    Message reply = channel.Request(request);
    Console.WriteLine("-------------------");
    Console.WriteLine(reply);

    channel.Close();
    factory.Close();
  }
}    

輸出:
1234
-------------------
<s:Envelope xmlns:s="http://schemas./soap/envelope/">
  <s:Header />
  <s:Body>
    <Data xmlns="http://..." xmlns:i="http://...">
      <I>9999</I>
    </Data>
  </s:Body>
</s:Envelope>

正如上面所看到的,所有的調(diào)用都被轉(zhuǎn)換成消息后發(fā)送。這也符合 SOA 的規(guī)范,完全隔離,清晰的邊界。(調(diào)用 "m.GetBody<Data>()" 后,會(huì)導(dǎo)致 Message.State 變更,再次訪問會(huì)出錯(cuò),有關(guān)詳細(xì)信息請(qǐng)參考 MSDN 文檔。)

我們還可以使用 MessageContractAttribute / MessageHeaderAttribute 來控制消息格式,這比 DataContractAttribute 要更加靈活。我們可以設(shè)置消息標(biāo)頭、消息體,包括是否對(duì)其中某些進(jìn)行簽名和加密處理。
[ServiceContract]
public interface ICalculate
{
  [OperationContract]
  void Add(Data d);
}

public class CalculateService : ICalculate
{
  public void Add(Data d)
  {
    Console.WriteLine(OperationContext.Current.RequestContext.RequestMessage);
    Console.WriteLine("----------------");
    Console.WriteLine("{0}/{1}", d.a, d.b);
  }
}

[MessageContract]
public class Data
{
  [MessageHeader]
  public double a = 1;

  [MessageBodyMember]
  public double b = 2;
}

public class WcfTest
{
  public static void Test()
  {
    AppDomain.CreateDomain("Server").DoCallBack(delegate
    {
      ServiceHost host = new ServiceHost(typeof(CalculateService));
      host.AddServiceEndpoint(typeof(ICalculate), new BasicHttpBinding(),
        "http://localhost:8080/calc");
      
      host.Open();
    });

    ChannelFactory<ICalculate> factory = new ChannelFactory<ICalculate>(new BasicHttpBinding(),
      "http://localhost:8080/calc");
    ICalculate o = factory.CreateChannel();

    Data d = new Data();
    d.a = 1234;
    d.b = 5678;
    o.Add(d);
  }
}

輸出:
<s:Envelope xmlns:s="http://schemas./soap/envelope/">
  <s:Header>
    <h:a xmlns:h="http:///">1</h:a>
    <To s:mustUnderstand="1" xmlns="http://s...">http://localhost:8080/calc</To>
    <Action s:mustUnderstand="1" xmlns="http://...">http:///ICalculate/Add</Action>
  </s:Header>
  <s:Body>
    <Data xmlns="http:///">
      <b>2</b>
    </Data>
  </s:Body>
</s:Envelope>
----------------
1234/5678

有關(guān) Message 更詳細(xì)的信息,可以參考 MSDN (Microsoft Windows SDK) 文檔。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多