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
{
}
partial class DownloadService : ISettings
{
}
[ServiceContract(SessionMode = SessionMode.Allowed)]
interface IDownload
{
[OperationContract(IsOneWay = true)]
void AddDownload(Download download);
}
[ServiceContract(SessionMode = SessionMode.Allowed)]
interface ISettings
{
[OperationContract(IsOneWay = true)]
void SaveSettings();
}
[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;
}
}
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!