Umbau Pasttrack Service in Restful Webservice (JSON)

Achtung, hier musste ich serverseitig noch .NET Activation (4.7) freischalten. Da habe ich leider eine Weile suchen müssen..
This commit is contained in:
Daniel Schick 2020-09-14 15:51:10 +00:00
parent 658cd11977
commit c35ed3707e
5 changed files with 63 additions and 41 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.ServiceModel;
@ -8,11 +9,15 @@ namespace bsmd.pasttrack.service
[ServiceContract]
public interface IService
{
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "pasttrack/{mmsi}/{mins}")]
[OperationContract]
Pasttrack[] GetPasttrack(int mmsi, int mins);
Pasttrack[] GetPasttrack(string mmsi, string mins);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class Pasttrack
@ -23,8 +28,6 @@ And timestamp > DATEADD(MINUTE, -120, GETDATE())
order by timestamp desc
*/
[DataMember]
public double Latitude { get; set; }
[DataMember]

View File

@ -1 +1 @@
<%@ ServiceHost Language="C#" Debug="true" Service="bsmd.pasttrack.service.Service" CodeBehind="Service.svc.cs" %>
<%@ ServiceHost Service="bsmd.pasttrack.service.Service" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Language="C#" debug="true" %>

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using log4net;
@ -7,37 +8,45 @@ namespace bsmd.pasttrack.service
{
public class Service : IService
{
private static ILog _log = LogManager.GetLogger(typeof(Service));
private readonly static ILog _log = LogManager.GetLogger(typeof(Service));
public Pasttrack[] GetPasttrack(int mmsi, int mins)
public Pasttrack[] GetPasttrack(string mmsi, string mins)
{
List<Pasttrack> aList = new List<Pasttrack>();
// Query
if (Int32.TryParse(mmsi, out int mmsi_val) && Int32.TryParse(mins, out int mins_val))
{
try
{
SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
con.Open();
string cmdText = string.Format("select Latitude, Longitude, Heading, Timestamp from aisposreport where mmsi=@MMSI And timestamp > DATEADD(MINUTE, @MIN, GETDATE()) order by timestamp desc");
SqlCommand cmd = new SqlCommand(cmdText, con);
cmd.Parameters.AddWithValue("@MMSI", mmsi);
cmd.Parameters.AddWithValue("@MIN", -mins);
cmd.Parameters.AddWithValue("@MMSI", mmsi_val);
cmd.Parameters.AddWithValue("@MIN", -mins_val);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Pasttrack p = new Pasttrack();
p.Latitude = (double) reader.GetInt32(0) / 600000.0;
p.Longitude = (double) reader.GetInt32(1) / 600000.0;
p.Heading = reader.GetInt32(2);
p.Timestamp = reader.GetDateTime(3);
Pasttrack p = new Pasttrack
{
Latitude = (double)reader.GetInt32(0) / 600000.0,
Longitude = (double)reader.GetInt32(1) / 600000.0,
Heading = reader.GetInt32(2),
Timestamp = reader.GetDateTime(3)
};
aList.Add(p);
}
reader.Close();
con.Close();
} catch (SqlException ex)
}
catch (SqlException ex)
{
_log.ErrorFormat("Error on data query: {0}", ex.ToString());
}
} else
{
_log.WarnFormat("invalid parameters: mins {0} mmsi {1}", mins, mmsi);
}
_log.InfoFormat("Result pasttrack array contains {0} elems", aList.Count);
return aList.ToArray();
}

View File

@ -20,22 +20,32 @@
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="bsmd.pasttrack.service.Service" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="bsmd.pasttrack.service.IService" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--

Binary file not shown.