gpt4 book ai didi

c# - 在另一个进程中处理 WCF 事件

转载 作者:太空狗 更新时间:2023-10-30 00:20:50 24 4
gpt4 key购买 nike

我有一个不可序列化的对象,我想从一个单独的进程访问它。我环顾四周,似乎唯一可行的选择是使用 WCF,但我不确定如何执行此操作,因为我是 WCF 的新手。如果我创建一个 WCF 服务,我如何将 WinForm“ Hook ”到 WCF 服务中的各种事件中?例如,用户直接与 WCF 服务通信,我希望我的 WinForm 客户端收到通知。我如何才能知道用户何时使用 WCF 服务完成某些操作并让 WinForm 客户端接收到该信息?

最佳答案

实现您正在寻找的方法是在您的服务上实现回调契约(Contract)。然后您的 win-forms 应用程序将能够“订阅”服务上触发的事件(例如对您的对象的修改)。

为此,您需要使用回调契约(Contract)实现服务契约(Contract):

[ServiceContract]
public interface IMyService_Callback
{
[OperationContract(IsOneWay = true)]
void NotifyClients(string message);
}

[ServiceContract(CallbackContract = typeof(IMyService_Callback))]
public interface IMyService
{
[OperationContract]
bool Subscribe();
}

然后你实现你的服务:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MyService : IMyService
{
private List<IMyService_Callback> callbacks;

public MyService()
{
this.callbacks = new List<IMyService_Callback>();
}

private void CallClients(string message)
{
callbacks.ForEach(callback => callback.NotifyClients(message));
}

public bool Subscribe()
{
var callback = OperationContext.Current.GetCallbackChannel<IMyService_Callback>();

if (!this.callbacks.Contains(callback))
{
this.callbacks.Add(callback);
}

// send a message back to the client
CallClients("Added a new callback");

return true;
}
}

在你的winforms客户端你只需要实现回调方法:

[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
public partial class ServiceClient : Form, IMyService_Callback
{
// Sync context for enabling callbacks
SynchronizationContext uiSyncContext;

public ServiceClient()
{
InitializeComponent(); //etc.

uiSyncContext = SynchronizationContext.Current;

// Create proxy and subscribe to receive callbacks
var factory = new DuplexChannelFactory<IMyService>(typeof(ServiceClient), "NetTcpBinding_IMyService");
var proxy = factory.CreateChannel(new InstanceContext(this));
proxy.Subscribe();
}

// Implement callback method
public void NotifyClients(string message)
{
// Tell form thread to update the message text field
SendOrPostCallback callback = state => this.Log(message);

uiSyncContext.Post(callback, "Callback");
}

// Just updates a form text field
public void Log(string message)
{
this.txtLog.Text += Environment.NewLine + message;
}
}

服务配置:

<system.serviceModel>
<services>
<service name="TestService.MyService" behaviorConfiguration="Normal">
<endpoint
address="net.tcp://localhost:8000/MyService"
contract="TestService.IMyService"
binding="netTcpBinding" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Normal" >
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

客户端配置

<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:8000/MyService"
binding="netTcpBinding"
contract="TestService.IMyService"
name="NetTcpBinding_IMyService">
</endpoint>
</client>
</system.serviceModel>

关于c# - 在另一个进程中处理 WCF 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8889051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com