access to class methods

Topics: Web Api
Sep 27, 2011 at 7:57 AM

I am using WebAPi aacording to below:

var host = new HttpServiceHost( typeof(DirectClientService), "http://localhost:8080/servicename" );




How I can access public methods of DirectClientService object?

When I did


DirectClientService varDC = (DirectClientService)host.SingletonInstance

 It always return null


Sep 27, 2011 at 8:07 AM

vovan, what are you trying to do exactly? You service is accessible over HTTP. To access it you will send HTTP requests to 'http://localhost:8080/servicename' either from a browser or using our HttpClient.

Check out out quick starts here for more on how to use web api. The quick start will show you how to access your api using our built in test client, but more will come in the future to show how to use HttpClient. Also if you check my recent build talk, I cover our http client in depth. 

The singleton instance property is only for hosts that are constructed with an instance. In general you never need to touch it.

Sep 27, 2011 at 8:16 AM

I would like to use it in server side application and not from HTTP.

After starting host.Open(); I would to call some method in my own instance class.

Is it possible? it always null


Sep 27, 2011 at 9:04 AM

Web API's sole purpose is to allow exposing over HTTP. The methods on your class are designed (via web api) to be exposed via HTTP Methods and uris, not to be invoked directly in an application. We do have an in-memory hosting model which allows you to send HTTP messages within process, but it doesn't sound like that is what you want.


Sep 27, 2011 at 9:12 AM

Yes exatly, I would like to do some init process, and this method does not exposed via HTTP. I hust want to get reference to my hosted in-memory object and call some method.

As I understand the SingletonInstance is exatly variable which are hosting my object but is always null. In debug seccion exist more host.base object , but how I can access them.




Nov 7, 2011 at 4:20 PM

I writing WCF application which are hosted in windows service using WCF WEB API. The problem is that I would like to pass to service class reference to my hosting application.

Below the Host code:

Dim t1 As System.Type = GetType(MOMRESTService.Service)
' Create a new ServiceHost to start and stop the service 
Dim serviceHost1 As New System.ServiceModel.ServiceHost(t1, New System.Uri(addressRoot)) ' for MEX inquiries
' Create the REST binding
Dim restBinding As New System.ServiceModel.WebHttpBinding()
' Create the REST endpoint. We need to do this explicitly in order to add a custom behavior to it. 
Dim restEndPoint = New System.ServiceModel.Description.ServiceEndpoint(System.ServiceModel.Description.ContractDescription.GetContract(GetType(MOMRESTService.IService)), restBinding, New System.ServiceModel.EndpointAddress(addressRoot))
' Add a customm behavior to the REST endpoint
restEndPoint.Behaviors.Add(New MOMRESTService.FaultingWebHttpBehavior())
' Add the endpoint to the service
' Open the ServiceHost to create listeners and start listening for messages

Below Service code:

 Public Function EchoWithGet(s As String) As String Implements IService.EchoWithGet
    Return "You said " & s
  End Function

So I would like in service method "EchoWithGet"  raise event to Hosted application. In another words I need to have reference from service to hosted application or to an object which can be passed from hosted application to service.

We tried to use singleton and created constructor in service, but it use sigle thread for all incoming request and we need multithreaded application.

With Rest API it also only using  InstanceContextMode.Single:

 var instance = new TodoResource(new ToDoMemoryRepository());

06         using (var host = new HttpServiceHost(instance, "http://localhost:8080/todo"))
07         {
08             host.Open();
09             ShowEndpointsOf(host);
10             WaitForKey();
11         }
Is exist any way to solve my problem?