我正在尝试创建一个WCF服务,它具有webHttpBinding
端点(用于Java客户端)和netTcpBinding
端点(用于。net客户端)。
对于netTcpBinding
端点,我希望能够使用回调,以便对事件发出警报,但是当我尝试配置它时,WCF抱怨,因为服务也有webHttpBinding
端点,不支持回调。
是否有一种方法可以让一个端点使用回调,而不是另一个端点?
不,绑定将验证它可以履行合同;如果合约是一个双工合约(即,它指定了一个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();
}
}