none
My Wcf service application only work in visual studio self-host environment(hosted by Wcfsvrhost.exe), not work in windows service host that I created. RRS feed

  • Question

  • My Wcf service application only work in visual studio self-host environment(hosted by Wcfsvrhost.exe), not work in windows service host that I created.

    below is my script.

    I create a Wcf library project and write below script:

    I define an interface class:

    using System.ServiceModel;
    namespace CBM
    {
        [ServiceContract (Namespace="Http://CBM.cbmService")]
        public interface IcbmService
        {

            [OperationContract]
            string GetData();
        }

    }

    I define an implement class

    namespace CBM
    {
        //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class cbmService : IcbmService
        {

            public string GetData()
            {
                return "aa";
            }
        }
    }

    I change App.config as below:

    <?xml version="1.0"?>
    <configuration>

      <system.web>
        <compilation debug="true"/>
        <trust level="Full"/>
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's
      app.config file. System.Configuration does not support config files for libraries -->
      <system.serviceModel>
      
        <bindings>
          <basicHttpBinding>
            <binding name="BasicBinding0" />
          </basicHttpBinding>
          <wsHttpBinding>
            <binding name="wsBinding0">
              <security>
                <transport clientCredentialType="None" />
              </security>
            </binding>
          </wsHttpBinding>
        </bindings>
        <services>
          <service behaviorConfiguration="cbmServerBehaviour" name="CBM.cbmService">
            <endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicBinding0"
              name="basic" contract="CBM.IcbmService" />
            <!--<endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="wsBinding0" name="ws" contract="CBM.IcbmService" />-->
            <host>
              <baseAddresses>
                <add baseAddress="Http://localhost:8000/cbmWcfService/cbmService" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webBehaviour">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="cbmServerBehaviour">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
              <serviceCredentials>
                <userNameAuthentication userNamePasswordValidationMode="Windows" />
              </serviceCredentials>
            </behavior>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <!--New configure-->
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
    <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

    after I finished all above, I create a install class as below:

    namespace cbmWcfService
    {
        using System;
        using System.Configuration.Install;
        using System.ComponentModel;
        using System.ServiceProcess;
        using System.ServiceModel;
        using System.Reflection;

        [RunInstaller(true)]
        public class ProjectInstaller : Installer
        {
            private readonly ServiceProcessInstaller process;
            private readonly ServiceInstaller service;

            public ProjectInstaller()
            {
                process = new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem};
                service = new ServiceInstaller { ServiceName = ServiceProperties.WindowServiceName };

                service.ServiceName = "cbmWcfService";
                service.DisplayName = "cbmWcfService";
                service.Description = "CBM WCF Service Hosted by Windows NT Service";
                service.StartType = ServiceStartMode.Automatic;

                Installers.Add(process);
                Installers.Add(service);
            }
        }

        partial class cbmWcfService : ServiceBase
        {
            public ServiceHost serviceHost = null;

            public static void Main(string[] args)
            {
                if (Environment.UserInteractive)
                {
                    string parameter = string.Concat(args);
                    switch (parameter)
                    {
                        case "install":
                            ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                            break;
                        case "uninstall":
                            ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                            break;
                    }
                }
                else
                {

                    Run(new cbmWcfService());
                }
            }

            public cbmWcfService()
            {
                ServiceName = ServiceProperties.WindowServiceName;

            }

            //Start the Windows service.

            protected override void OnStart(string[] args)
            {

                if (serviceHost != null)
                {
                    serviceHost.Close();
                }

                // Create a ServiceHost for the WcfCalculatorService type and provide the base address.
                serviceHost = new ServiceHost(typeof(CBM.cbmService));

                // Open the ServiceHostBase to create listeners and start listening for messages.
                serviceHost.Open();


            }


            // Stop the Windows service.
            protected override void OnStop()
            {
                if (serviceHost != null)
                {
                    serviceHost.Close();
                    serviceHost = null;
                }


            }
        }

        public static class ServiceProperties { public static readonly string WindowServiceName = "cbmWcfService";}
    }

    after that, I bulid and test it in visual studio, it work fine.

    the I create a windows  form application , and add service reference as "Http://localhost:8000/cbmWcfService/cbmService",

    below is my test application script:

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using FixDataSystems;
    using WindowsFormsApplication1.ServiceReference1;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            IcbmServiceClient aa = new IcbmServiceClient();
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show(aa.GetData());
            }
        }
    }

    Test it in visual studio, it work fine.

    meanwhile I notice that "Wcfsvrhost.exe" startup while I test my program.


    then, I "Exit"  "Wcfsvrhost.exe". and install mp it service and startup it by command:

    bin\Debug\cbmWcfService.exe install
    net start "cbmWcfService"

    my windows service startup and work fine, the my test application get error message and can not work.

    below is detailed error message:

    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http://localhost:8000/cbmWcfService/cbmService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException:
    由于目标计算机积极拒绝,无法连接。 127.0.0.1:8000
       at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
       at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
       --- End of inner exception stack trace ---
       at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
       at System.Net.HttpWebRequest.GetRequestStream()
       at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
       --- End of inner exception stack trace ---

    Server stack trace:
       at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream()
       at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at WindowsFormsApplication1.ServiceReference1.IcbmService.GetData()
       at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\sa\Documents\Visual Studio 2010\Projects\cbmWcfService_03\WindowsFormsApplication1\Form1.cs:line 25
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
    ----------------------------------------
    WindowsFormsApplication1
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/Users/sa/Documents/Visual%20Studio%202010/Projects/cbmWcfService_03/WindowsFormsApplication1/bin/Release/WindowsFormsApplication1.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.ServiceModel
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel/v4.0_4.0.0.0__b77a5c561934e089/System.ServiceModel.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.ServiceModel.Internals
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Internals/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Internals.dll
    ----------------------------------------
    SMDiagnostics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/SMDiagnostics/v4.0_4.0.0.0__b77a5c561934e089/SMDiagnostics.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.ServiceModel.Web
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Web/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Web.dll
    ----------------------------------------
    System.Runtime.Serialization
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
    ----------------------------------------
    System.IdentityModel
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.IdentityModel/v4.0_4.0.0.0__b77a5c561934e089/System.IdentityModel.dll
    ----------------------------------------
    Microsoft.VisualStudio.Diagnostics.ServiceModelSink
        Assembly Version: 4.0.0.0
        Win32 Version: 14.0.23107.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Diagnostics.ServiceModelSink/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Diagnostics.ServiceModelSink.dll
    ----------------------------------------
    System.Web
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll
    ----------------------------------------
    System.Xaml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.6.1055.0 built by: NETFXREL2
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.

    For example:

    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>

    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.

    My currnt summary:

    when I do testing, visual studio and my compiled application run as administrator.

    I assume my window service isn't host wcf library nicely. then I can see the wcf library can work with wcfsvrhost.exe in normal, but abnormal while it work with my windows background service.

    but my windows background service configuration and script is so simple, with can get expert advise how to fix this problem. THX.

    Tuesday, May 3, 2016 2:09 AM

Answers