【发布时间】:2012-01-05 18:59:50
【问题描述】:
也许我在混合,如果是这种情况,请告诉我。
我想通过 WCF 提供一组关于消息的服务(这是一个示例)。在这个集合中,我将有一个“sendMessage”服务和一个“receiveMessage”服务。
对于 sendMessage,我想使用 MSMQ,所以用户可以将消息发送给 100.000 个其他用户,这将在后台处理。
对于 receiveMessage,我不想使用 MSMQ,因为我希望用户在发出请求并获得响应时得到服务(在这种情况下,MSMQ 是单向的)。
我正在使用此代码在示例应用程序中启动我的主机
static void Main(string[] args)
{
using (ServiceHost host =
new ServiceHost(typeof(Service), new Uri[] {
new Uri("net.msmq://localhost/private/loadqueue"),
new Uri("http://localhost:8383/") }))
{
host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
NetMsmqBinding binding = new NetMsmqBinding();
binding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;
binding.Security.Transport.MsmqProtectionLevel = System.Net.Security.ProtectionLevel.None;
binding.ExactlyOnce = false;
host.AddServiceEndpoint(
typeof(IContract),
binding,
string.Empty);
host.AddServiceEndpoint(
typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex");
host.Open();
Console.WriteLine("service running");
Console.ReadLine();
}
}
合同是这样的
[ServiceContract(SessionMode=SessionMode.Allowed)]
[DeliveryRequirements(QueuedDeliveryRequirements = QueuedDeliveryRequirementsMode.Allowed)]
public interface IContract
{
[OperationContract(IsOneWay = true)]
void SendData(string msg);
}
当客户端调用 SendData 服务时,消息被发送到队列,然后被服务消费。
我想创建第二个服务,它直接从客户端接收消息(中间没有队列)。
客户端将只有一个 Web 引用,如果他调用 service.SendData(),则消息将发送到队列,如果客户端调用 service.NetMethod(),则服务会直接接收消息。这样,无论服务是否使用队列,对于客户端开发人员来说都是透明的,并且我可以根据服务的功能而不是机制对服务进行分组。能做到吗?
谢谢, 奥斯卡
【问题讨论】: