Is there a possibility to initialize a WebService.
I'm searching for a method that runs only during first call to WebService. Is there something like it in .Net ?
If you require "initialisation" the first time a given client connects to the Web Service:
Have an Initialise method that returns a token, such a a GUID, that's then required on every call made to the actual "does the work" method of your web service. You can then ensure that for that client the service is always initialised.
If you require it the first time the web service is ever called:
Add some code to your service, as a private method, that is called at the top of each public method. Within it check for the existance of something, such as a registry entry, file, database record or other persistant value. If it doesn't exist, perform your initialisation and then create the "something".
If you require it on the first call since IIS last recycled/started the Application Pool:
Have a static constructor for the class so that when it's first instantiated the static constructor runs and performs your initialisation.
If you are trying to initialize a resource that is used by the web service and you only want to initialize it once per application, you can use the Application_Start event on the Global.asax. Be aware that IIS will recycle the application pool whenever the application pool is eating up too many resources.
If you need to initialize class level variable, you can do it in the constructor of the web service. I would recommend against that, because your web service should be stateless. If you need to initialize a static resource once in your web service, you can use a static constructor.
If you need a single resource that is available once throughout your application, I would recommend you look into the singleton pattern.
When you create a WebService application in Visual Studio, by default a class named "Service" will be added. When you look at the code for this class (Service.cs), you'll see a constructor ("public Service()") with two commented-out lines. You can either put your initialization code here, or call a private method that you've defined inside the Service class. This constructor will be called only when your client makes its first call to any WebMethod in the Service class.
Well, there's no equivalent to the J2EE initialization if that's what you're after... However each web app is hosted in an application domain. Every now and then an application may be recycled and a new application domain may be created...
You can use Application_Start in global.asax, it will run once if any webservice inside your project is called
Related
I am new in .Net development..........
I want to call two web service methods simultaneously(parallel) using c# code from the application, web service is built as a separate application. In my application, one web service method should get called & it is not needed to wait for the response, then another service method should get called & it is not needed to wait for the response, but after calling both of these service methods, I need to wait for the respond for first method & as per the result I need to proceed ahead, what is the best way to perform this?
Have just completed developing a WebAPI.
Now, creating an client application for the same.
To do this, I had created a WPF Client application which needs the user to enter the parent URI of the service.
That is,
http://localhost:65620/VirtualDirectoryName (Just the base URL not the entire URL)
Now, how to check if the URL is correct or not using the client application?
EDIT 1:
HttpClient can be used: But I am asking about validating the base URL alone.
Any ideas how to do this ?
(If I put the base address in the Browser, it shows the contents of the Directory. Then I assume that the installation is correct (At this point I am not calling any methods of webAPI though))
EDIT 2:
URL is Correct or not: I have to ensure that the service has been installed correctly in the IIS and it is up and running before any client could access its methods.
So you have a WebAPI that cannot be modified, and a client application that cannot be modified. You need to call one of the API methods using something, to check the API has been deployed correctly.
Either use the client application to call one of the API's methods.
Or create a test application which calls one of the API's methods.
I may be missing something, but surely there must be a simple GET method in your API that you can call from your test application that would prove the API is running.
In a self hosted ASP.NET Web Api, how can I:
Detect from a class if there's an "ambient" web api context. This is needed to avoid passing in metadata information on every service call. I'm looking for the equivalent of
System.Web.HttpRequest.Current != null
How can I attach metadata information associated with the current request. Again some of this metadata is just so prevalent that including them on every method and calls is way too painful. Think transaction, multi-tenant architecture and credentialings. I need a way to make this sort of information flow through between requests without cluttering the code.
In another word I also need the equivalent of this as explained here:
HttpContext.Current.Items["user" + X.ToString()]
I think I can still access them, as long as the WebApi is hosted on IIS, but I have got these self hosted and I need a way to keep track of the ambient UoW information - how can I do so?
A few notes:
I have also contemplated using per request DI and injecting a
request context into the managers, there are however a ton of legacy
code that wasn't set up for that (some of which are static) and I
don't have the guts to blow up production by doing such a major
refactor.
I have also used a thread static, static variable - the problem with such is that the thread gets recycled, and the process hosts multiple services, some of which aren't even WebApi... so sometimes my managers thought it's handling a WebApi request when in fact it's serving a WCF one.
The HttpRequestMessage instance has Properties dictionary that is intended for holding arbitrary per-request context.
I have a webservice with a .asmx extension which points to a class in my web application. After adding some code to output a debug log on application startup, I can see a new debug log is being created every time a user visits the page.
I was hoping that I could configure this web service to only ever use a single instance.
Meaning, when the first user visits the site it creates an instance of my webservice, and then all requests after are routed through that same class, sharing the same state.
Is this even possible?
Web sites are stateless in nature. Meaning, each request is generally unrelated to any other request.
That said, you could set the log as a static variable in your global.asax file. Be aware of threading issues.
Alternatively you might look at Elmah.
You seem to have a number of problems.
First of all, the ASMX technology is all but obsolete, and should not be used for new development. WCF should be used instead.
Next, yes, you can have multiple calls to the service share the same instance of your class. However, you'll have to prevent simultaneous access to this class unless it is thread safe. You should expect multiple service calls to call into the same class instance at the same time.
Finally, ASMX does not support singleton web services. The best you can do is to have multiple service calls all share the same object (carefully locked).
I'm fairly new to WebService developement and have just set up my own webservice (ASP.Net 3.5, Visual Studio 2008 .asmx file).
I can not find a way of setting up my webservice to take parameters on the constructor.
If i create a constructor that takes parameters, it is not then shown when i hook up to the webservice from my application (it only shows a parameterless constructor).
Am i missing something blatently obvious, or is this not possible (and why not)?
Thanks.
When you say "constructor for my webservice", do you mean "constructor for the proxy that communicates with my webservice"?
You may need to write a wrapper for the proxy if you need to handle authentication tokens and the like (this is the approach that the Flickr.Net open source project took)
it is meaningless to have parameters of web service constructor. web service communicate with client only on requests. constructor is called by web server internals automatically