Chr*_*ian 16 .net c# wcf prism
我目前正在c#中编写一个.Net应用程序,它有两个主要组件:
这两个组件目前是我的解决方案中的两个独立项目.此外,我正在使用PRISM 4.0框架,以便从这些组件中制作模块.
本质上,DataGenerator使用PRISM 的EventAggregator生成大量数据并发送事件,Viewer会订阅这些事件并显示为用户准备好的数据.
现在我的要求略有改变,现在两个组件将在他们自己的应用程序中运行(但在同一台计算机上).我仍然希望所有的通信事件驱动,我还想使用PRISM框架.
我的第一个想法是使用WCF进行这两个应用程序之间的通信.然而,有一件事让生活变得更难:
基本上所有这些事件所携带的数据都是非常简单的字符串,整数和布尔值.如果没有WCF,可以采用更轻量级的方式吗?
最后,如果DataGenerator可以发送这些事件并且可能有多个应用程序订阅它们(或者没有),那将是很好的.
任何建议和提示都非常感谢.
谢谢!基督教
编辑1
我现在正在创建两个简单的控制台应用程序(一个托管服务并发送消息,另一个接收消息)使用WCF和回调(如建议的那样).一旦我开始工作,我将添加工作代码.
编辑2
好的 - 管理一个简单的程序运行!:)谢谢你的帮助,伙计们!以下是代码和图片的位置:
让我们从发件人开始:
在我的应用程序中,发件人包含服务接口及其实现.
IMessageCallback是回调接口:
namespace WCFSender
{
interface IMessageCallback
{
[OperationContract(IsOneWay = true)]
void OnMessageAdded(string message, DateTime timestamp);
}
}
Run Code Online (Sandbox Code Playgroud)
ISimpleService是服务合同:
namespace WCFSender
{
[ServiceContract(CallbackContract = typeof(IMessageCallback))]
public interface ISimpleService
{
[OperationContract]
void SendMessage(string message);
[OperationContract]
bool Subscribe();
[OperationContract]
bool Unsubscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
SimpleService是ISimpleService的实现:
public class SimpleService : ISimpleService
{
private static readonly List<IMessageCallback> subscribers = new List<IMessageCallback>();
public void SendMessage(string message)
{
subscribers.ForEach(delegate(IMessageCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.OnMessageAdded(message, DateTime.Now);
}
else
{
subscribers.Remove(callback);
}
});
}
public bool Subscribe()
{
try
{
IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
if (!subscribers.Contains(callback))
subscribers.Add(callback);
return true;
}
catch
{
return false;
}
}
public bool Unsubscribe()
{
try
{
IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
if (!subscribers.Contains(callback))
subscribers.Remove(callback);
return true;
}
catch
{
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Program.cs中(在发送方),托管服务并发送消息:
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
class Program : SimpleServiceReference.ISimpleServiceCallback, IDisposable
{
private SimpleServiceClient client;
static void Main(string[] args)
{
ServiceHost myService = new ServiceHost(typeof(SimpleService));
myService.Open();
Program p = new Program();
p.start();
Console.ReadLine();
}
public void start()
{
InstanceContext context = new InstanceContext(this);
client = new SimpleServiceReference.SimpleServiceClient(context, "WSDualHttpBinding_ISimpleService");
for (int i = 0; i < 100; i++)
{
client.SendMessage("message " + i);
Console.WriteLine("sending message" + i);
Thread.Sleep(600);
}
}
public void OnMessageAdded(string message, DateTime timestamp)
{
throw new NotImplementedException();
}
public void Dispose()
{
client.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
此外,请注意服务引用已添加到Sender项目中!
让我们现在到达接收方:
正如在Sender中已经完成的那样,我将服务引用添加到项目中.
Program.cs只有一个类:
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
class Program : SimpleServiceReference.ISimpleServiceCallback, IDisposable
{
private SimpleServiceClient client;
static void Main(string[] args)
{
Program p = new Program();
p.start();
Console.ReadLine();
p.Dispose();
}
public void start()
{
InstanceContext context = new InstanceContext(this);
client = new SimpleServiceReference.SimpleServiceClient(context, "WSDualHttpBinding_ISimpleService");
client.Subscribe();
}
public void OnMessageAdded(string message, DateTime timestamp)
{
Console.WriteLine(message + " " + timestamp.ToString());
}
public void Dispose()
{
client.Unsubscribe();
client.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
剩下的最后一件事是app.config文件.在客户端,通过添加服务引用自动生成app.config.在服务器端,我略微更改了配置,但是通过添加服务引用也可以自动生成部分配置.请注意,您需要在添加服务引用之前进行更改:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_ISimpleService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" />
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" />
</security>
</binding>
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8732/Design_Time_Addresses/WCFSender/SimpleService/"
binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_ISimpleService"
contract="SimpleServiceReference.ISimpleService" name="WSDualHttpBinding_ISimpleService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior name="MessageBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCFSender.SimpleService" behaviorConfiguration="MessageBehavior">
<endpoint address="" binding="wsDualHttpBinding" contract="WCFSender.ISimpleService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFSender/SimpleService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)
重要提示:我设法使用教程实现了这两个非常简单的应用程序.上面的代码对我有用,希望能帮助其他人理解WCF回调.它不是编写得非常好的代码,不应该完全使用!它只是一个简单的示例应用程序.
不要担心性能,如果配置正确,wcf可以达到非常高的吞吐量.为您的活动使用回调:http://www.switchonthecode.com/tutorials/wcf-tutorial-events-and-callbacks