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;
using System.ServiceModel.Web;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.ServiceModel; using System.ServiceModel;
@ -8,11 +9,15 @@ namespace bsmd.pasttrack.service
[ServiceContract] [ServiceContract]
public interface IService public interface IService
{ {
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "pasttrack/{mmsi}/{mins}")]
[OperationContract] [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. // Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract] [DataContract]
public class Pasttrack public class Pasttrack
@ -23,8 +28,6 @@ And timestamp > DATEADD(MINUTE, -120, GETDATE())
order by timestamp desc order by timestamp desc
*/ */
[DataMember] [DataMember]
public double Latitude { get; set; } public double Latitude { get; set; }
[DataMember] [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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
using log4net; using log4net;
@ -7,36 +8,44 @@ namespace bsmd.pasttrack.service
{ {
public class Service : IService 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>(); 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); try
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);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{ {
Pasttrack p = new Pasttrack(); SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
p.Latitude = (double) reader.GetInt32(0) / 600000.0; con.Open();
p.Longitude = (double) reader.GetInt32(1) / 600000.0; string cmdText = string.Format("select Latitude, Longitude, Heading, Timestamp from aisposreport where mmsi=@MMSI And timestamp > DATEADD(MINUTE, @MIN, GETDATE()) order by timestamp desc");
p.Heading = reader.GetInt32(2); SqlCommand cmd = new SqlCommand(cmdText, con);
p.Timestamp = reader.GetDateTime(3); cmd.Parameters.AddWithValue("@MMSI", mmsi_val);
aList.Add(p); cmd.Parameters.AddWithValue("@MIN", -mins_val);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
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();
} }
reader.Close(); catch (SqlException ex)
con.Close(); {
} catch (SqlException ex) _log.ErrorFormat("Error on data query: {0}", ex.ToString());
}
} else
{ {
_log.ErrorFormat("Error on data query: {0}", ex.ToString()); _log.WarnFormat("invalid parameters: mins {0} mmsi {1}", mins, mmsi);
} }
_log.InfoFormat("Result pasttrack array contains {0} elems", aList.Count); _log.InfoFormat("Result pasttrack array contains {0} elems", aList.Count);
return aList.ToArray(); return aList.ToArray();

View File

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

Binary file not shown.