Understanding WCF Self-Hosting approach using ServiceHost class and its constructors

I have some doubts regarding service hosting in WCF.

I want to host a service using the self-hosting approach. ServiceHost class comes to the rescue. By using it, it is possible to host a service having a direct access to the Windows Hosting Framework. Well, consider the following approaches:

0) COMMON ASSUMPTIONS: All cases assume that configuration file App.config is used in order to set endpoints' ABC. So in the following codes no mention about endpoint, just do not bother about it. We will consider this services too:

[ServiceContract]
public interface IMyService {
   [OperationContract]
   string MyOp1(int myint);
   [OperationContract]
   string MyOp2(int myint);
}
public class MyService : IMyService {
   // This service needs to be constructed providing at least a string or an integer, if an attempt to construct it wothout passing any of these is done, the service should raise an error.
   MyService(string strparam) { ... }
   MyService(int intparam) { ... }
   MyService(string strparam, int intparam) { ... }
   public string MyOp1(int myint) { ... }
   public string MyOp2(int myint) { ... }
}
public class MyStandaloneService : IMyService {
   // This service does not need to be constructed.
   MyStandaloneService() { ... }
   public string MyOp1(int myint) { ... }
   public string MyOp2(int myint) { ... }
}

1) CASE 1: It is possible to host a service using this overload of the ServiceHost class:

public ServiceHost(
   Type serviceType,
   params Uri[] baseAddresses
)

By using it, it is possible to let the framework manage the service instantiation because simply the service type is required. Of course the construction is the base contruction if the service. The no parameter constructor will be called. This overload is good when handling services that do not need special construction... some kind of standalone services:

using (ServiceHost host = new ServiceHost(typeof(MyStandaloneService))) {
   host.Open();
   ...
   host.Close();
}

2) CASE 2: It is possible to host a service using this overload of the ServiceHost class:

public ServiceHost(
   Object singletonInstance,
   params Uri[] baseAddresses
)

By using it, it is possible to instantiate a service and then host it without letting the framework handle this... this approach is good when dealing with services that need special treatment and are not completely standalone:

MyService MS = new MyService("the string");
using (ServiceHost host = new ServiceHost(MS)) {
   host.Open();
   ...
   host.Close();
}

Well I would like to understand the following:

A) In CASE 1 it is possible to host a service automatically by providing the type. If I attempt to create another service of the same type (MyStandaloneService), does it result in an error because trying to create two same services? Probably I should hardcode, in this case, endpoint configurations because using the config file will result in two same services hosted n the same address.

B) In CASE 2 the MSDN documentation says this creates a singleton instance of the service. So If I attempt to host another service in this way:

MyService MS = new MyService("the string");
MyService MS2 = new MyService(23);
ServiceHost host = new ServiceHost(MS));
ServiceHost host2 = new ServiceHost(MS2));
host.Open();
host2.Open();
...
host.Close();
host2.Close();

Would I get an error?

C) If I wanted to avoid singleton instantiation what should I do?

Thanks

Answers


First of all, you probably need to check out the ServiceBehaviorAttribute MSDN article.

I don't go into details here, but you can either create one instance of the service object to handle ALL requests (sequentially, that is, one after the other), or let the ServiceHost object create one service object per request and handle them simultaneously in different threads.

Once you decide what approach suits you best in your application, you will understand which one of the ServiceHost constructor overloads to use. Your CASE 1 corresponds to multiple-instances simultaneous handling approach, and CASE 2 corresponds to 'one instance to handle them all' approach.

The ServiceHost overloads must go hand-to-hand with [ServiceBehavior] attribute on your MyService class. So, please check out the link I gave above.

EDIT: now responding to your questions:

A) If I attempt to create another service of the same type (MyStandaloneService), does it result in an error because trying to create two same services?

No, it is what will be done by ServiceHost: it will create one instance of the service per request (in fact, per session, but again read MSDN)

B) In CASE 2 the MSDN documentation says this creates a singleton instance of the service. So If I attempt to host another service in this way (...) Would I get an error?

You can't host two services with the same ABC at once, so yes. If you host them on different endpoints, it's OK. The 'singleton' here means that one single service instance will handle all requests.

C) If I wanted to avoid singleton instantiation what should I do?

Use CASE 1 approach :)


Need Your Help

Restoring instance variables in Android upon activity recreation

android android-activity tabs fragment recreate

I have an Android project for which I am creating a custom tab view. I have structured the main Activity (FragmentActivity) in such a way that it has a tab bar at the bottom of the screen with a

Writing to an IO stream in Ruby instead of sanitizing user input and sending to the shell?

ruby bash

I was just reading a blog post about sanitizing user input in Ruby before sending it to the command line. The author's conclusion was: don't send user input it to the command line in the first plac...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.