多个WCF服务控制 [英] Multiple WCF service contrct

查看:57
本文介绍了多个WCF服务控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决一下在WCF服务中使用多个服务合同的情况吗?



我尝试了什么:



我是WCF的新宠,在一项服务中看到了多服务合同的一个例子,但却无法弄清楚应该使用哪个服务器

can someone please help me with scenarios in which one would use multiple service contract in WCF service??

What I have tried:

I am new bee to WCF and seen the one example of multiple service contract in one service but not able to figure out the senarios in which one should use

multiple service contract in one service

推荐答案

如果您在逻辑上不同的操作集之间共享数据,则在单个服务上实现多个合同通常要容易得多,而不是实现支持依赖注入的自定义 ServiceHost 。特别是如果你是WCF的新手。一个常见的模式是使用这样的部分类:



If you have shared data between logically distinct sets of operations it is generally much easier to implement multiple contracts on a single service rather than implementing a custom ServiceHost which supports dependency injection. Especially if you are new to WCF. A common pattern is the use of partial classes like this:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall)]
partial class DownloadService : IDownload
{
  //Various download function like Add, Remove, Start, Stop, etc. go here
}

partial class DownloadService : ISettings
{
  //Settings functions related to download functionality go here
}

[ServiceContract(SessionMode = SessionMode.Allowed)]
interface IDownload
{
  [OperationContract(IsOneWay = true)]
  void AddDownload(Download download);
  //Then whatever other functions to download you use
}

[ServiceContract(SessionMode = SessionMode.Allowed)]
interface ISettings
{
  [OperationContract(IsOneWay = true)]
  void SaveSettings();
  //Other functions for settings like Add, Remove, Load, etc.
}

[DataContract]
struct Download
{
  [DataMember]
  public string Name {get; private set;}
  [DataMember]
  public int Packet {get; private set;}
  public Download(string name, int packet)
  {
    Name = name;
    Packet = packet;
  }
}





所以在这个例子中,这个服务用于从某个地方下载。您还需要各种设置,以便控制 DownloadService 的功能。好吧,这显然需要设置和 DownloadService 之间的一些共享数据。一种快速简便的方法就是将两种功能整合到一个服务中并公开多个合同 - 一个用于下载,一个用于设置。更高级的解决方案更符合 SOLID [ ^ ]原则是派生出你自己的 ServiceHost 将共享依赖项注入服务实例的类。我有一个提示/技巧这里 [ ^ ]可以告诉你如何完成这个但是我坚持简单学习时的解决方案:)



希望这有帮助!



So in this example this service is used to download from somewhere. You also want various settings that let you control how the DownloadService functions. Well, this is obviously going to require some shared data between the settings and DownloadService. A quick and easy way to go about that is simply to wrap up both functionalities into a single service and expose multiple contracts - one for downloading and one for settings. A more advanced solution that's more in line with SOLID[^] principles would be to derive your own ServiceHost class which injects the shared dependency into service instances. I have a tip/trick here[^] that can show you how to accomplish this but I'd stick to simple solutions while learning :)

Hope this helped!


这篇关于多个WCF服务控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆