1

我想創建一個WCF服務,需要有一個webHttpBinding端點(用於Java客戶端)和netTcpBinding端點(對於.NET客戶端)。有WCF回調只爲綁定,支持它

對於netTcpBinding端點,我希望能夠使用回調來提醒事件,但是當我嘗試配置此事件時,WCF會抱怨,因爲該服務也有端點webHttpBinding,該端點不支持回調。

是否有具有一個端點使用的回調而不是另一種方式?

回答

4

號,綁定將驗證它能夠信守合同;如果合同是雙面合同(即,它指定了CallbackContract),但綁定不能執行雙面打印,那麼它將在驗證期間拋出。

你可以做的是有它使用的webHttpBinding端點的基本合同,而另一個合同(此時雙面之一),從第一,用於由netTcpBinding終點的。

下面的代碼示出了這種合同安排的一個例子。

public class StackOverflow_7341463 
{ 
    [ServiceContract] 
    public interface ICalc 
    { 
     [OperationContract, WebGet] 
     int Add(int x, int y); 
     [OperationContract, WebGet] 
     int Subtract(int x, int y); 
     [OperationContract, WebGet] 
     int Multiply(int x, int y); 
     [OperationContract, WebGet] 
     int Divide(int x, int y); 
    } 
    [ServiceContract(CallbackContract = typeof(ICalcNotifications))] 
    public interface INotifyingCalc : ICalc 
    { 
     [OperationContract] 
     void Connect(); 
     [OperationContract] 
     void Disconnect(); 
    } 
    [ServiceContract] 
    public interface ICalcNotifications 
    { 
     [OperationContract(IsOneWay = true)] 
     void OperationPerformed(string text); 
    } 
    public class Service : INotifyingCalc 
    { 
     static List<ICalcNotifications> clients = new List<ICalcNotifications>(); 

     #region ICalc Members 

     public int Add(int x, int y) 
     { 
      this.NotifyOperation("Add", x, y); 
      return x + y; 
     } 

     public int Subtract(int x, int y) 
     { 
      this.NotifyOperation("Subtract", x, y); 
      return x - y; 
     } 

     public int Multiply(int x, int y) 
     { 
      this.NotifyOperation("Multiply", x, y); 
      return x * y; 
     } 

     public int Divide(int x, int y) 
     { 
      this.NotifyOperation("Divide", x, y); 
      return x/y; 
     } 

     #endregion 

     #region INotifyingCalc Members 

     public void Connect() 
     { 
      var callback = OperationContext.Current.GetCallbackChannel<ICalcNotifications>(); 
      clients.Add(callback); 
     } 

     public void Disconnect() 
     { 
      var callback = OperationContext.Current.GetCallbackChannel<ICalcNotifications>(); 
      clients.Remove(callback); 
     } 

     #endregion 

     private void NotifyOperation(string operationName, int x, int y) 
     { 
      foreach (var client in clients) 
      { 
       client.OperationPerformed(string.Format("{0}({1}, {2})", operationName, x, y)); 
      } 
     } 
    } 
    class MyCallback : ICalcNotifications 
    { 
     public void OperationPerformed(string text) 
     { 
      Console.WriteLine("Operation performed: {0}", text); 
     } 
    } 
    public static void Test() 
    { 
     string baseAddressTcp = "net.tcp://" + Environment.MachineName + ":8008/Service"; 
     string baseAddressHttp = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddressHttp), new Uri(baseAddressTcp)); 
     host.AddServiceEndpoint(typeof(ICalc), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); 
     host.AddServiceEndpoint(typeof(INotifyingCalc), new NetTcpBinding(SecurityMode.None), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     var factory = new DuplexChannelFactory<INotifyingCalc>(
      new InstanceContext(new MyCallback()), 
      new NetTcpBinding(SecurityMode.None), 
      new EndpointAddress(baseAddressTcp)); 
     var proxy = factory.CreateChannel(); 
     proxy.Connect(); 
     Console.WriteLine("Proxy connected"); 

     Console.WriteLine(new WebClient().DownloadString(baseAddressHttp + "/Add?x=4&y=7")); 
     Console.WriteLine(new WebClient().DownloadString(baseAddressHttp + "/Multiply?x=44&y=57")); 
     Console.WriteLine(new WebClient().DownloadString(baseAddressHttp + "/Divide?x=432&y=16")); 

     proxy.Disconnect(); 

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

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 
     host.Close(); 
    } 
} 
+0

我希望得到的所有答覆都和這一樣一樣清晰有效:D – Satal

+0

我也使用這種模式。 –

相關問題