none
How to host WCF Service with AppInitialize on IIS? RRS feed

  • Question

  • Hy guys,

    I wrote a WCF Service which I want to host on the IIS. Because I want to log some stuff when the WCF service starts, I used the AppInitialize method within the App_Code folder for logging. That works great local but when I publish the service I don´t get any logs. 

    The service works but I don´t get any logs. I think the AppInitialize class doesn´t work after installing it on IIS.

    I am using VS 2012 with the local IIS Web Server on Windows 7.

    Can somebody help me? 


    Liebe Gr+ße Michael Web Entwickler

    Thursday, June 20, 2013 7:22 AM

Answers

  • Hi,

    Please try the workaround in this thread, hope it can help you.

    Make use of the Factory attribute of the svc file

    <%@ ServiceHost Language="C#" Debug="true" Service="*" CodeBehind="*.svc.cs" Factory="ServiceHostFactory" %>

    Implement your ServiceHostFactory by inheriting from System.ServiceModel.Activation.ServiceHostFactory

    and override the method CreateServiceHost.  In that method create instance of a ServiceHost class, which in turn is inherited from  System.ServiceModel.ServiceHost.

    Move code from App_Code/InitializeService.cs file to ur ServiceHost's overridden method InitializeRuntime ().

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 21, 2013 8:23 AM
    Moderator

All replies

  • Hi,

    Please try the workaround in this thread, hope it can help you.

    Make use of the Factory attribute of the svc file

    <%@ ServiceHost Language="C#" Debug="true" Service="*" CodeBehind="*.svc.cs" Factory="ServiceHostFactory" %>

    Implement your ServiceHostFactory by inheriting from System.ServiceModel.Activation.ServiceHostFactory

    and override the method CreateServiceHost.  In that method create instance of a ServiceHost class, which in turn is inherited from  System.ServiceModel.ServiceHost.

    Move code from App_Code/InitializeService.cs file to ur ServiceHost's overridden method InitializeRuntime ().

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 21, 2013 8:23 AM
    Moderator
  • Hi,

    thank you for your answer.

    I figured out that my IIS Applicationpool Identity had no read access to the Eventlogs. First I tried to use my local account (Administrator) for the Application pool which worked great, but I had to use a special domain user account for the application pool.

    So I tried this: http://www.christiano.ch/wordpress/2009/12/02/iis7-web-application-writing-to-event-log-generates-security-exception/

    and then it worked on Windows Server 2008R2 Datacenter with IIS 7. 

    The problem now is, that I have to use Windows Server 2012 with IIS 8. So I tried the same thing there. Give the application pool identity access to the Eventlog. But this time it doesn´t work. Maybe the App_Code/AppStart.cs/AppInitialize() code is not executing when I start my service.

    So what can I do?

    Best Regards

    Michael


    Liebe Gr+ße Michael Web Entwickler

    Thursday, July 4, 2013 8:34 AM
  • Ok it works now. 

    I called the service from the browser and since then I got logs.

    Regards

    Michael


    Liebe Gr+ße Michael Web Entwickler

    Thursday, July 4, 2013 9:05 AM
  • Haixia,

    Thanks for the information.  I'm trying this and I'm having some issues.  Here is my code:

    namespace Sample
    {
        public class ServiceHostFactory : System.ServiceModel.Activation.ServiceHostFactoryBase
        {
            public override System.ServiceModel.ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
            {
                var initialize = new KNServiceHost();
                return initialize;
            }
        }
    
        public class KNServiceHost: System.ServiceModel.ServiceHost
        {
            protected override void InitializeRuntime()
            {
                base.InitializeRuntime();
                var i = 0; //code to run on activation.
            }
        }
    }

    I must be doing something wrong since I'm getting back an error on a test that says:

         The requested service, 'https://<snip>Service.svc' could not be activated.

    When I take the Factory attribute back out it seems to be fine, but of course does not run my initialization code.


    Best Wishes, Keith Nicholson

    Tuesday, October 7, 2014 9:46 PM