Any ideas?
Regards;
/jb
My name is Allen Chen. It's my pleasure to work with you on this issue.
Quote from Jonny ==================================================
Problem is that I need to know
when IIS stops or recycles the application so I can call Dispose() on
internal objects.
==================================================
To clean up the unmanaged resources we can call the Dispose method in the
destructor method.
For example:
public class Service1 : IService1
{
~Service1()
{
//Clean up the unmanaged resources here
}
public void DoWork()
{
}
}
Any instance of this class is a managed object. Thus it's managed by the
GC. When GC knows there's no reference pointing to this object it will call
its destructor(actually the Finalize method. See
http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx). For more details
please check out:
http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
So we can clean up the unmanaged resources in the destructor.
Please have a try and let me know if you have further questions.
Regards,
Allen Chen
Microsoft Online Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msd...@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
When does this happen? I haven't seen any documentation that describes the
life time of a web service object.
1. Server machine starts, and IIS initializes.
2. A client calls the web service - this instantiates a Service1 instance
3. My code creates a separate thread to host a singleton instance of an
object.
4. A result is sent back to the client (result originates from the singleton
client)
5. Some time elapses
6. Another client calls the web service
7. A result is sent back to the client.
I thought that 2 and 6 above results in that new instances of Service1 is
created, and that 4 and 7 above results in that those instances is released
and reclaimed by the GC. In that case the finalizer is of no use, since the
singleton object would be destroyed at 4 and 7, thus defeating what I need
to accomplish.
Basically, what I need to do is to Dispose of my singleton when IIS shuts
down the application.
Regards;
/jb
Thanks for your clarification. I think there's a misunderstanding in my
previous post. In my previous post I thought you want to clean up some
unmanaged code that the Service1 class uses and it turns out that you want
to clean up a singleton object only once.
If you want to do that in Application_End you can create a global.asax file
in the virtual directory where the WCF resides.
<%@ Application Language="C#" %>
<script runat=server>
protected void Application_End(object src, EventArgs e)
{
//clean up the singleton object here
}
</script>
The behavior should be the same as ASP.NET. Could you test it to see if it
works?
Regards;
/jb
"Allen Chen [MSFT]" <v-al...@online.microsoft.com> skrev i meddelandet
news:vzfAp$DXJHA...@TK2MSFTNGHUB02.phx.gbl...
Thanks for your update.
Since the WCF is hosted in IIS the global.asax can also work for it.
Actually, the WCF is running in the same AppDomain of the ASP.NET
application. You can create a new ASP.NET Web Application to test:
1. Add following code in Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(AppDomain.CurrentDomain.Id);
}
2. Right click the project node on the explorer window, select Add->New
item->WCF service to add a WCF service.
3. Replace the interface in the IService1.cs with:
public interface IService1
{
[OperationContract]
int GetAppDomainID();
}
4. Replace the class Service1 in the Service1.svc.cs with:
public class Service1 : IService1
{
public int GetAppDomainID()
{
return AppDomain.CurrentDomain.Id;
}
}
5. Build the application and publish it on IIS.
6. Close the current project, create another ASP.NET Web Application and
add the previous WCF service as a service reference.
7. Add following code in Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Service1Client proxy = new Service1Client();
Response.Write(proxy.GetAppDomainID());
}
8. View the Default.aspx of the first ASP.NET Web Application from IIS.
9. View the Default.aspx of the second ASP.NET Web Application from Visual
Studio.
10. You'll see the same integer on the two pages.
11. Update the Web.config file of the first ASP.NET Web Application (in
C:\Inetpub\wwwroot\YourVirtualDirectoryName) to force calling
Application_End. (You can insert some white spaces) Refresh these two
pages. You may see the integer changed but still it's still same on these
two pages.
Please feel free to ask if you have further questions.
Regards;
/jb
"Allen Chen [MSFT]" <v-al...@online.microsoft.com> skrev i meddelandet
news:3LrMgGy...@TK2MSFTNGHUB02.phx.gbl...
You're welcome. Thank you for using our Newsgroup Support Service. Have a
nice day!