我需要我的WCF服務將事件提交給客戶端。我讀過一個碰巧經過回調通道,我已經實現了它以下列方式: 服務接口:如何在通過ChannelFactory創建的WCF服務上設置回調通道?
public interface IServiceCallback
{
[OperationContract(IsOneWay = true)]
void OnNewAlert(Alert a);
[OperationContract(IsOneWay = true)]
void OnProductEdited(Product p);
[OperationContract(IsOneWay = true)]
void OnHighlightChanged(Dictionary<User, List<Product>> highlighted);
[OperationContract(IsOneWay = true)]
void OnCatalogUpdated();
event EventHandler NewAlert;
event EventHandler ProductEdited;
event EventHandler HighlightChanged;
event EventHandler CatalogUpdated;
}
[ServiceContract(CallbackContract = typeof(IServiceCallback))]
public interface IService : IDisposable
{
[OperationContract]
List<Product> GetProducts(Predicate<Product> match = null, int limit = 0, string username = null);
[OperationContract]
Product GetProduct(Predicate<Product> match, string username = null);
[OperationContract]
Product GetRandomProduct(Predicate<Product> match = null, string username = null);
[OperationContract]
int GetFlagIndex(string flagName);
[OperationContract]
void SetFlag(string pid, string flagName, bool value);
[OperationContract]
List<Alert> GetAlerts(string username);
[OperationContract]
void DismissAlert(Alert alert, String username);
[OperationContract]
void HighlightProduct(List<string> pids, string user);
[OperationContract]
void EditProduct(string pid, Dictionary<string, object> fieldValues, string username = null);
[OperationContract]
void AttachModule(IModule m);
[OperationContract]
void Ping();
event EventHandler NewAlert;
event EventHandler ProductEdited;
event EventHandler HighlightChanged;
event EventHandler CatalogUpdated;
}
服務實現:
namespace Service
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class ServiceInstance : IService
{
List<IServiceCallback> callbackChannels = new List<IServiceCallback>();
//other vars
public ServiceInstance()
{
//lots of stuff here
}
private User SignalUser(string username)
{
if (username == null)
return null;
IServiceCallback channel = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
if (!callbackChannels.Contains(channel)) //if CallbackChannels not contain current one.
{
callbackChannels.Add(channel);
}
User user = knownUsers.Find(p => p.username == username);
if (user == null)
{
user = new User();
user.username = username;
user.highlighColor = Color.FromArgb(r.Next(0, 128), r.Next(0, 128), r.Next(0, 128));
knownUsers.Add(user);
foreach (KeyValuePair<Alert, List<User>> kvp in alerts)
{
kvp.Value.Add(user);
}
}
user.lastOnline = DateTime.Now;
if(!onlineUsers.Contains(user))
onlineUsers.Add(user);
return user;
}
//lots of other things here
}
}
上的客戶端回調實現:
class ServiceEventHandler : IServiceCallback
{
public event EventHandler NewAlert;
public event EventHandler ProductEdited;
public event EventHandler HighlightChanged;
public event EventHandler CatalogUpdated;
public void OnCatalogUpdated()
{
CatalogUpdated?.BeginInvoke(null, null, null, null);
}
public void OnHighlightChanged(Dictionary<User, List<Product>> highlighted)
{
HighlightChanged?.BeginInvoke(highlighted, EventArgs.Empty, null, null);
}
public void OnNewAlert(Alert a)
{
NewAlert?.BeginInvoke(a, EventArgs.Empty, null, null);
}
public void OnProductEdited(Product p)
{
ProductEdited?.BeginInvoke(p, EventArgs.Empty, null, null);
}
}
但這是我的問題: 在客戶端,我是應該將它傳遞給這樣的服務:根據這個StackOverflow的答案
EventHandler eventHandler = new EventHandler();
MyServiceClient client = new MyServiceClient(new InstanceContext(eventHandler));
: https://stackoverflow.com/a/1143777/2018696
但我不連接到我的服務就是這樣,因爲我的客戶不知道的實施的服務,它只知道兩個接口! 所以我連這樣的:
public static IService GetService(string serviceAddress)
{
Uri service_uri = new Uri(serviceAddress);
var endpoint = new EndpointAddress(service_uri, new[] { AddressHeader.CreateAddressHeader(settings["username"], "", "") });
IService service = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), endpoint);
return service;
}
那麼,如何讓回調的工作?
UPDATE:
好,從而提出一個意見,我更換的ChannelFactory與DuplexChannelFactory和basicHttpBinding的與WsDualHTTPBinding,我沒有得到來自服務器的響應。如果我抓取回調處理程序,我會得到BasicHTTPBinding的響應。所以基本上:
[ServiceContract]
BasicHttpBinding();
ChannelFactory<IService>.CreateChannel(binding, endpoint);
^這個作品
[ServiceContract(CallbackContract = typeof(IServiceCallback))]
WSDualHttpBinding(WSDualHttpSecurityMode.None);
DuplexChannelFactory<IService>.CreateChannel(new InstanceContext(handler), binding, endpoint);
^這個沒有。
它適用於localhost,但不適用於LAN或Internet。防火牆在服務器和客戶端都處於關閉狀態。當我嘗試聯繫服務器時,我收到了60秒的超時時間。
請查看[DuplexChannelFactory類](https://msdn.microsoft.com/en-us/library/ms576164(v = vs.110).aspx),它應該指向您繼續的方向。 – carlosfigueira
@carlosfigueira謝謝,我做了一些修改,但是現在我沒有得到服務的迴應,請在操作中查看「更新」。謝謝! – Daniel