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:
parent
658cd11977
commit
c35ed3707e
@ -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]
|
||||
|
||||
@ -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" %>
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using log4net;
|
||||
@ -7,36 +8,44 @@ 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
|
||||
try
|
||||
if (Int32.TryParse(mmsi, out int mmsi_val) && Int32.TryParse(mins, out int mins_val))
|
||||
{
|
||||
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);
|
||||
SqlDataReader reader = cmd.ExecuteReader();
|
||||
while(reader.Read())
|
||||
try
|
||||
{
|
||||
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);
|
||||
aList.Add(p);
|
||||
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_val);
|
||||
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();
|
||||
con.Close();
|
||||
} catch (SqlException ex)
|
||||
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);
|
||||
return aList.ToArray();
|
||||
|
||||
@ -20,22 +20,32 @@
|
||||
<compilation debug="true" targetFramework="4.5"/>
|
||||
<httpRuntime targetFramework="4.5"/>
|
||||
</system.web>
|
||||
|
||||
<system.serviceModel>
|
||||
<behaviors>
|
||||
<serviceBehaviors>
|
||||
<behavior>
|
||||
<serviceBehaviors >
|
||||
<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.
Loading…
Reference in New Issue
Block a user