none
C# windows service - web interface RRS feed

  • Question

  • I created a web service that gives web interface

    this is the service.cs code :

    using System.ServiceProcess;
    using NLog;
    using System.ComponentModel;
    using System.ServiceModel;
    using System.Configuration;
    using System.Configuration.Install;
    
    namespace WindowsServiceTemplate
    {
        public partial class Service : ServiceBase
        {
            private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
            public ServiceHost serviceHost = null;
    
            private readonly TestService s;
            public Service()
            {
               
                
                InitializeComponent();
                s = new TestService();
            }
    
            protected override void OnStart(string[] args)
            {
                Logger.Info("Start event");
                if (serviceHost != null)
                {
                    serviceHost.Close();
                }
    
                // Create a ServiceHost for the CalculatorService type and 
                // provide the base address.
                string baseAddress = "http://localhost:8000/Service";
                serviceHost = new ServiceHost(typeof(CalculatorService), new System.Uri(baseAddress));
                serviceHost.AddServiceEndpoint(typeof(WindowsServiceTemplate.Service.ICalculator),
                            new BasicHttpBinding(), baseAddress);
                // Open the ServiceHostBase to create listeners and start 
                // listening for messages.
                serviceHost.Open();
                s.Start();
            }
    
            protected override void OnStop()
            {
                
                Logger.Info("Stop event");
                if (serviceHost != null)
                {
                    serviceHost.Close();
                    serviceHost = null;
                }
                s.Stop();
            }
    
            protected override void OnShutdown()
            {
                Logger.Info("Windows is going shutdown");
                Stop();
            }
    
    
            public void Start()
            {
                OnStart(null);
            }
            /////////////////////////////////
            /// <summary>
            /// 
            // Define a service contract.
            [ServiceContract(Namespace = "http://WindowsServiceTemplate")]
            public interface ICalculator
            {
              
                
                [OperationContract]
                string test();
            }
            /// </summary>
            ///// Implement the ICalculator service contract in a service class.
            public class CalculatorService : ICalculator
            {
               
                public string test()
                {
                    return "111";
                }
            } //end of CalculatorService
    
        }
    }
    

    and this is the code from app.config

    <system.serviceModel>
    
        <services>
          <!-- Note: the service name must match the configuration name for the service implementation. -->
          <service name="WindowsServiceTemplate.myservice" behaviorConfiguration="MyServiceTypeBehaviors" >
            <!-- Add the following endpoint.  -->
            <!-- Note: your service must have an http base address to add this endpoint. -->
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mexHttpBinding" />
          </service>
        </services>
    
        <behaviors>
          <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
              <!-- Add the following element to your service behavior configuration. -->
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
    
      </system.serviceModel>

    I can browse http://localhost:8000/Service

    but I can configure out whats wrong (when I try to access the test() method 

     http://localhost:8000/Service/test - > I get 400

    and when I browse  http://localhost:8000/Service I get

    Service
    
    
    This is a Windows© Communication Foundation service.
    
    Metadata publishing for this service is currently disabled.
    
    If you have access to the service, you can enable metadata publishing by completing the following steps to modify your web or application configuration file:
    
    1. Create the following service behavior configuration, or add the <serviceMetadata> element to an existing service behavior configuration:
    
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    2. Add the behavior configuration to the service:
    
    <service name="MyNamespace.MyServiceType" behaviorConfiguration="MyServiceTypeBehaviors" >
    Note: the service name must match the configuration name for the service implementation.
    
    3. Add the following endpoint to your service configuration:
    
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
    Note: your service must have an http base address to add this endpoint.
    
    The following is an example service configuration file with metadata publishing enabled:
    
    <configuration>
        <system.serviceModel>
     
            <services>
                <!-- Note: the service name must match the configuration name for the service implementation. -->
                <service name="MyNamespace.MyServiceType" behaviorConfiguration="MyServiceTypeBehaviors" >
                    <!-- Add the following endpoint.  -->
                    <!-- Note: your service must have an http base address to add this endpoint. -->
                    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
                </service>
            </services>
     
            <behaviors>
                <serviceBehaviors>
                    <behavior name="MyServiceTypeBehaviors" >
                        <!-- Add the following element to your service behavior configuration. -->
                        <serviceMetadata httpGetEnabled="true" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
     
        </system.serviceModel>
    </configuration>
    For more information on publishing metadata please see the following documentation: http://go.microsoft.com/fwlink/?LinkId=65455.

    I assume I miss configure something but can't find it.

    any idea?

    Thursday, February 28, 2019 12:04 AM

All replies

  • The error message tells you exactly what is happening here.

    By default, HTTP GET verb is disabled for WCF services. If you want to access the services by typing URL in your web browser (which will generate GET request), you need to enable that but modifying the settings as described in the error message.

    Thursday, February 28, 2019 1:40 AM
  • I attached above my code from app.config

    where you can see

    <serviceMetadata httpGetEnabled="true" />

    Thursday, February 28, 2019 6:43 AM
  • Hi want 2 Learn,

    According to your question is more related to Web Service, you could post a new thread in WCF, ASMX and other Web Services forum.

    https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 28, 2019 7:32 AM
  • EndPoint Address="mex", not "mexHttpBinding".

    For more explanation of the config settings, see here.

    • Edited by cheong00 Thursday, February 28, 2019 8:07 AM
    Thursday, February 28, 2019 8:05 AM
  • I did still same notification
    Thursday, February 28, 2019 8:18 AM
  • Okay, I think you can ignore the config file changes as I see you did the equivalent on OnStart() method. (A number of things goes wrong too, say the EndPoint contract should have set to "ICalculator")

    Try add ServiceMetadataBehavior and set HttpGetEnabled to true as instructed in here, then binds to serviceHost.Description.Behaviour and so on.

    Thursday, February 28, 2019 8:42 AM
  • ok I made a progress by adding

      ServiceMetadataBehavior smb = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                // If not, add one
                if (smb == null)
                    smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                serviceHost.Description.Behaviors.Add(smb);

    I get now

    CalculatorService Service
    
    
    You have created a service.
    
    To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:
    
    
    svcutil.exe http://localhost:8000/Service?wsdl
    You can also access the service description as a single file:
    
    http://localhost:8000/Service?singleWsdl
    This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:
    
    C#
    
    class Test
    {
        static void Main()
        {
            CalculatorClient client = new CalculatorClient();
    
            // Use the 'client' variable to call operations on the service.
    
            // Always close the client.
            client.Close();
        }
    }
    
    Visual Basic
    
    Class Test
        Shared Sub Main()
            Dim client As CalculatorClient = New CalculatorClient()
            ' Use the 'client' variable to call operations on the service.
    
            ' Always close the client.
            client.Close()
        End Sub
    End Class

    but I don't want to access the test() method using a service reference

    i want to call it like 

    http://localhost:8000/Service/test

    and return to the bowser soe html code

    Thursday, February 28, 2019 9:06 AM