C# windows service - web interface

  • Thread starter Thread starter want 2 Learn
  • Start date Start date
W

want 2 Learn

Guest
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: Publishing Metadata.

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

any idea?

Continue reading...
 
Back
Top