SharpMap WMTS / TMS Server implementation - c#

Can anyone help me with the implementation of WMTS / TMS Server in SharpMap?
I've been trying out a lot from diff sources but I can't seem to come up with a working solution. Below is a handler I am using, it's just drawing the boxes instead of data from the database. I am using the same method I used for a WMS Server:
DatabaseUtil.SqlServer(ConnectionString(), Layers(), new Size(1, 1), bbox, "id");
to get data from SQL Server and it's working just fine for the WMS. The only difference is that the one below returns a specific layer instead of a list by using .FindAll(lyr => lyr.Table.Equals(layer)) query.
/// <summary>
/// Summary description for WMTS
/// </summary>
public class WMTS : IHttpHandler
{
/// <summary>
/// Defines the projection
/// </summary>
private readonly ICoordinateTransformation projection = ProjUtil.ToPseudoMercator();
/// <summary>
/// The ProcessRequest
/// wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=layer_id&STYLE=default&TILEMATRIXSET=matrix_id&TILEMATRIX=3&TILEROW=2&TILECOL=0&FORMAT=image%2Fjpeg
/// </summary>
/// <param name="context">The <see cref="HttpContext"/></param>
public void ProcessRequest(HttpContext context)
{
string layer = context.Request.Params["LAYER"];
int tilematrix = int.Parse(context.Request.Params["TILEMATRIX"]);
int tilerow = int.Parse(context.Request.Params["TILEROW"]);
int tilecol = int.Parse(context.Request.Params["TILECOL"]);
string service = context.Request.Params["SERVICE"];
string request = context.Request.Params["REQUEST"];
string version = context.Request.Params["VERSION"];
string style = context.Request.Params["STYLE"];
string tilematrixset = context.Request.Params["TILEMATRIXSET"];
string format = context.Request.Params["FORMAT"];
if (String.IsNullOrEmpty(layer))
throw new ArgumentNullException("layer");
Map map = Map(layer, tilecol, tilerow, tilematrix);
var map_image = map.GetMap();
//using (var memory_stream = new MemoryStream())
//{
// map_image.Save(memory_stream, ImageFormat.Png);
// var wms = memory_stream.ToArray();
// WriteResponseInChunks(wms, context);
//}
byte[] buffer;
using (var ms = new MemoryStream())
{
map_image.Save(ms, ImageFormat.Png);
map_image.Dispose();
buffer = ms.ToArray();
}
WriteResponseInChunks(buffer, context);
}
public static ImageCodecInfo GetEncoderInfo(String mimeType)
{
foreach (var encoder in ImageCodecInfo.GetImageEncoders())
if (encoder.MimeType == mimeType)
return encoder;
return null;
}
/// <summary>
/// The GetMap
/// </summary>
/// <returns>The <see cref="SharpMap.Map"/></returns>
protected Map Map(string layer, int x, int y, int z)
{
Envelope bbox = GetBoundingBoxInLatLngWithMargin(x, y, z);
return DatabaseUtil.SqlServer(ConnectionString(), Layers().FindAll(lyr => lyr.Table.Equals(layer)), new Size(1, 1), bbox, "id");
}
/// <summary>
/// The Layers
/// </summary>
/// <returns>The <see cref="List{VectorLayerModel}"/></returns>
private List<VectorLayerModel> Layers()
{
VectorLayerModel standsLayerModel = new VectorLayerModel()
{
Table = "cadastre",
Style = new VectorStyle { Line = new Pen(Color.DarkGray, 2) }
};
VectorLayerModel roadsLayerModel = new VectorLayerModel()
{
Table = "townships",
Style = new VectorStyle { Line = new Pen(Color.DarkRed, 2.5f) }
};
VectorLayerModel pipeLayerModel = new VectorLayerModel()
{
Table = "provinces",
Style = new VectorStyle { Line = new Pen(Color.DarkBlue, 1.5f) }
};
return new List<VectorLayerModel>() { standsLayerModel, roadsLayerModel, pipeLayerModel };
}
/// <summary>
/// The ConnectionString
/// </summary>
/// <returns>The <see cref="string"/></returns>
private string ConnectionString()
{
return "Data Source=******;Initial Catalog=GCCIGO_V2;Integrated Security=SSPI;";
}
/// <summary>
/// The GetBoundingBoxInLatLngWithMargin
/// </summary>
/// <param name="tileX">The <see cref="int"/></param>
/// <param name="tileY">The <see cref="int"/></param>
/// <param name="zoom">The <see cref="int"/></param>
/// <returns>The <see cref="Envelope"/></returns>
private Envelope GetBoundingBoxInLatLngWithMargin(int tileX, int tileY, int zoom)
{
Point px1 = new Point((tileX * 256), (tileY * 256));
Point px2 = new Point(((tileX + 1) * 256), ((tileY + 1) * 256));
PointF ll1 = TileSystemHelper.PixelXYToLatLong(px1, zoom);
PointF ll2 = TileSystemHelper.PixelXYToLatLong(px2, zoom);
double[] prj1 = projection.MathTransform.Transform(new double[] { ll1.X, ll1.Y });
double[] prj2 = projection.MathTransform.Transform(new double[] { ll2.X, ll2.Y });
Envelope bbox = new Envelope();
bbox.ExpandToInclude(prj1[0], prj1[1]);
bbox.ExpandToInclude(prj2[0], prj2[1]);
return bbox;
}
/// <summary>
/// The size of the chunks written to response.
/// </summary>
private const int ChunkSize = 2 * 8192;
/// <summary>
/// Method to write an array of bytes in chunks to a http response
/// </summary>
/// <remarks>
/// The code was adopted from http://support.microsoft.com/kb/812406/en-us
/// </remarks>
/// <param name="buffer">The array of bytes</param>
/// <param name="context">The response</param>
private static void WriteResponseInChunks(byte[] buffer, HttpContext context)
{
try
{
bool _continue;
context.Response.ClearContent();
context.Response.ContentType = "image/png";
using (var ms = new MemoryStream(buffer))
{
var dataToRead = buffer.Length;
while (dataToRead > 0)
{
if (context.Response.IsClientConnected)
{
{
var tmpBuffer = new byte[ChunkSize];
var length = ms.Read(tmpBuffer, 0, tmpBuffer.Length);
context.Response.OutputStream.Write(tmpBuffer, 0, length);
context.Response.Flush();
dataToRead -= length;
}
}
else
{
dataToRead = -1;
}
}
_continue = dataToRead > 0;
}
}
catch (Exception ex)
{
context.Response.ClearContent();
context.Response.ContentType = "text/plain";
context.Response.Write(string.Format("Error : {0}", ex.Message));
context.Response.Write(string.Format("Source : {0}", ex.Message));
context.Response.Write(string.Format("StackTrace: {0}", ex.StackTrace));
}
finally
{
context.Response.End();
}
}
/// <summary>
/// Gets a value indicating whether IsReusable
/// </summary>
public bool IsReusable
{
get
{
return true;
}
}
}
Maybe it will help if i just put up the code for creating the SharpMap.Map:
public static Map SqlServer(string conn, List<VectorLayerModel> layers, Size map_size, Envelope bbox, string id_column = "ID")
{
Map map = new Map(map_size);
foreach (var layer in layers)
{
VectorLayer lyr = CreateSqlServerLayer(conn, layer.Table, layer.Style, id_column);
lyr.IsQueryEnabled = true;
lyr.Enabled = true;
if (bbox != null)
{
var geometries = lyr.DataSource.GetGeometriesInView(bbox);
lyr.DataSource = new GeometryFeatureProvider(geometries);
}
map.Layers.Add(lyr);
}
return map;
}

After some debugging i found out that my
GetBoundingBoxInLatLngWithMargin(int tileX, int tileY, int zoom)
was returning bounds that was way out of my data bounds. I realized i was applying a coordinate transformation to my bbox giving me a bbox in pseudo mercator yet my layer is in wgs84. I changed the GetBoundingBoxInLatLngWithMargin to:
//code omited
Envelope bbox = new Envelope();
bbox.ExpandToInclude(ll1.X, ll1.Y );
bbox.ExpandToInclude(ll2.X, ll2.Y);
return bbox;
Please find the full conversation on GitHub

Related

NewtonSoft Deserialise from stream [duplicate]

I have heard that Json.NET is faster than DataContractJsonSerializer, and wanted to give it a try...
But I couldn't find any methods on JsonConvert that take a stream rather than a string.
For deserializing a file containing JSON on WinPhone, for example, I use the following code to read the file contents into a string, and then deserialize into JSON. It appears to be about 4 times slower in my (very ad-hoc) testing than using DataContractJsonSerializer to deserialize straight from the stream...
// DCJS
DataContractJsonSerializer dc = new DataContractJsonSerializer(typeof(Constants));
Constants constants = (Constants)dc.ReadObject(stream);
// JSON.NET
string json = new StreamReader(stream).ReadToEnd();
Constants constants = JsonConvert.DeserializeObject<Constants>(json);
Am I doing it wrong?
The current version of Json.net does not allow you to use the accepted answer code. A current alternative is:
public static object DeserializeFromStream(Stream stream)
{
var serializer = new JsonSerializer();
using (var sr = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(sr))
{
return serializer.Deserialize(jsonTextReader);
}
}
Documentation: Deserialize JSON from a file stream
public static void Serialize(object value, Stream s)
{
using (StreamWriter writer = new StreamWriter(s))
using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jsonWriter, value);
jsonWriter.Flush();
}
}
public static T Deserialize<T>(Stream s)
{
using (StreamReader reader = new StreamReader(s))
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}
}
UPDATE: This no longer works in the current version, see below for correct answer (no need to vote down, this is correct on older versions).
Use the JsonTextReader class with a StreamReader or use the JsonSerializer overload that takes a StreamReader directly:
var serializer = new JsonSerializer();
serializer.Deserialize(streamReader);
I've written an extension class to help me deserializing from JSON sources (string, stream, file).
public static class JsonHelpers
{
public static T CreateFromJsonStream<T>(this Stream stream)
{
JsonSerializer serializer = new JsonSerializer();
T data;
using (StreamReader streamReader = new StreamReader(stream))
{
data = (T)serializer.Deserialize(streamReader, typeof(T));
}
return data;
}
public static T CreateFromJsonString<T>(this String json)
{
T data;
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(json)))
{
data = CreateFromJsonStream<T>(stream);
}
return data;
}
public static T CreateFromJsonFile<T>(this String fileName)
{
T data;
using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
{
data = CreateFromJsonStream<T>(fileStream);
}
return data;
}
}
Deserializing is now as easy as writing:
MyType obj1 = aStream.CreateFromJsonStream<MyType>();
MyType obj2 = "{\"key\":\"value\"}".CreateFromJsonString<MyType>();
MyType obj3 = "data.json".CreateFromJsonFile<MyType>();
Hope it will help someone else.
I arrived at this question looking for a way to stream an open ended list of objects onto a System.IO.Stream and read them off the other end, without buffering the entire list before sending. (Specifically I'm streaming persisted objects from MongoDB over Web API.)
#Paul Tyng and #Rivers did an excellent job answering the original question, and I used their answers to build a proof of concept for my problem. I decided to post my test console app here in case anyone else is facing the same issue.
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TestJsonStream {
class Program {
static void Main(string[] args) {
using(var writeStream = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.None)) {
string pipeHandle = writeStream.GetClientHandleAsString();
var writeTask = Task.Run(() => {
using(var sw = new StreamWriter(writeStream))
using(var writer = new JsonTextWriter(sw)) {
var ser = new JsonSerializer();
writer.WriteStartArray();
for(int i = 0; i < 25; i++) {
ser.Serialize(writer, new DataItem { Item = i });
writer.Flush();
Thread.Sleep(500);
}
writer.WriteEnd();
writer.Flush();
}
});
var readTask = Task.Run(() => {
var sw = new Stopwatch();
sw.Start();
using(var readStream = new AnonymousPipeClientStream(pipeHandle))
using(var sr = new StreamReader(readStream))
using(var reader = new JsonTextReader(sr)) {
var ser = new JsonSerializer();
if(!reader.Read() || reader.TokenType != JsonToken.StartArray) {
throw new Exception("Expected start of array");
}
while(reader.Read()) {
if(reader.TokenType == JsonToken.EndArray) break;
var item = ser.Deserialize<DataItem>(reader);
Console.WriteLine("[{0}] Received item: {1}", sw.Elapsed, item);
}
}
});
Task.WaitAll(writeTask, readTask);
writeStream.DisposeLocalCopyOfClientHandle();
}
}
class DataItem {
public int Item { get; set; }
public override string ToString() {
return string.Format("{{ Item = {0} }}", Item);
}
}
}
}
Note that you may receive an exception when the AnonymousPipeServerStream is disposed, I ignored this as it isn't relevant to the problem at hand.
another option that is handy when you are running out of memory is to periodically flush
/// <summary>serialize the value in the stream.</summary>
/// <typeparam name="T">the type to serialize</typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
/// <param name="settings">The json settings to use.</param>
/// <param name="bufferSize"></param>
/// <param name="leaveOpen"></param>
public static void JsonSerialize<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false)
{
using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize,leaveOpen))
using (var jsonWriter = new JsonTextWriter(writer))
{
var ser = JsonSerializer.Create( settings );
ser.Serialize(jsonWriter, value);
jsonWriter.Flush();
}
}
/// <summary>serialize the value in the stream asynchronously.</summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
/// <param name="settings">The settings.</param>
/// <param name="bufferSize">The buffer size, in bytes, set -1 to not flush till done</param>
/// <param name="leaveOpen"> true to leave the stream open </param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
public static Task JsonSerializeAsync<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false, CancellationToken cancellationToken=default)
{
using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize: bufferSize,leaveOpen: leaveOpen))
using (var jsonWriter = new JsonTextWriter(writer))
{
var ser = JsonSerializer.Create( settings );
ser.Serialize(jsonWriter, value);
return jsonWriter.Flush();
}
//jsonWriter.FlushAsnc with my version gives an error on the stream
return Task.CompletedTask;
}
You can test/ use it like so:
[TestMethod()]
public void WriteFileIntoJsonTest()
{
var file = new FileInfo(Path.GetTempFileName());
try
{
var list = new HashSet<Guid>();
for (int i = 0; i < 100; i++)
{
list.Add(Guid.NewGuid());
}
file.JsonSerialize(list);
var sr = file.IsValidJson<List<Guid>>(out var result);
Assert.IsTrue(sr);
Assert.AreEqual<int>(list.Count, result.Count);
foreach (var item in result)
{
Assert.IsFalse(list.Add(item), $"The GUID {item} should already exist in the hash set");
}
}
finally
{
file.Refresh();
file.Delete();
}
}
you'd need to create the extension methods, here is the whole set:
public static class JsonStreamReaderExt
{
static JsonSerializerSettings _settings ;
static JsonStreamReaderExt()
{
_settings = JsonConvert.DefaultSettings?.Invoke() ?? new JsonSerializerSettings();
_settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
_settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
_settings.DateFormatHandling = DateFormatHandling.IsoDateFormat ;
}
/// <summary>
/// serialize the value in the stream.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
public static void JsonSerialize<T>(this Stream stream,[DisallowNull] T value)
{
stream.JsonSerialize(value,_settings);
}
/// <summary>
/// serialize the value in the file .
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="file">The file.</param>
/// <param name="value">The value.</param>
public static void JsonSerialize<T>(this FileInfo file,[DisallowNull] T value)
{
if (string.IsNullOrEmpty(file.DirectoryName)==true && Directory.Exists(file.DirectoryName) == false)
{
Directory.CreateDirectory(file.FullName);
}
using var s = file.OpenWrite();
s.JsonSerialize(value, _settings);
file.Refresh();
}
/// <summary>
/// serialize the value in the file .
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="file">The file.</param>
/// <param name="value">The value.</param>
/// <param name="settings">the json settings to use</param>
public static void JsonSerialize<T>(this FileInfo file, [DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings)
{
if (string.IsNullOrEmpty(file.DirectoryName) == true && Directory.Exists(file.DirectoryName) == false)
{
Directory.CreateDirectory(file.FullName);
}
using var s = file.OpenWrite();
s.JsonSerialize(value, settings);
file.Refresh();
}
/// <summary>
/// serialize the value in the file .
/// </summary>
/// <remarks>File will be refreshed to contain the new meta data</remarks>
/// <typeparam name="T">the type to serialize</typeparam>
/// <param name="file">The file.</param>
/// <param name="value">The value.</param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
public static async Task JsonSerializeAsync<T>(this FileInfo file, [DisallowNull] T value, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(file.DirectoryName) == true && Directory.Exists(file.DirectoryName) == false)
{
Directory.CreateDirectory(file.FullName);
}
using (var stream = file.OpenWrite())
{
await stream.JsonSerializeAsync(value, _settings,bufferSize:1024,leaveOpen:false, cancellationToken).ConfigureAwait(false);
}
file.Refresh();
}
/// <summary>
/// serialize the value in the file .
/// </summary>
/// <remarks>File will be refreshed to contain the new meta data</remarks>
/// <typeparam name="T">the type to serialize</typeparam>
/// <param name="file">The file to create or overwrite.</param>
/// <param name="value">The value.</param>
/// <param name="settings">the json settings to use</param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
public static async Task JsonSerializeAsync<T>(this FileInfo file, [DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, CancellationToken cancellationToken=default)
{
if (string.IsNullOrEmpty(file.DirectoryName) == true && Directory.Exists(file.DirectoryName) == false)
{
Directory.CreateDirectory(file.FullName);
}
using (var stream = file.OpenWrite())
{
await stream.JsonSerializeAsync(value, settings,bufferSize:1024,leaveOpen:false, cancellationToken).ConfigureAwait(false);
}
file.Refresh();
}
/// <summary>serialize the value in the stream.</summary>
/// <typeparam name="T">the type to serialize</typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
/// <param name="settings">The json settings to use.</param>
/// <param name="bufferSize">The buffer size, in bytes, set -1 to not flush till done</param>
/// <param name="leaveOpen"> true to leave the stream open </param>
public static void JsonSerialize<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false)
{
using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize,leaveOpen))
using (var jsonWriter = new JsonTextWriter(writer))
{
var ser = JsonSerializer.Create( settings );
ser.Serialize(jsonWriter, value);
jsonWriter.Flush();
}
}
/// <summary>serialize the value in the stream asynchronously.</summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <param name="value">The value.</param>
/// <param name="settings">The settings.</param>
/// <param name="bufferSize">The buffer size, in bytes, set -1 to not flush till done</param>
/// <param name="leaveOpen"> true to leave the stream open </param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
public static Task JsonSerializeAsync<T>(this Stream stream,[DisallowNull] T value, [DisallowNull] JsonSerializerSettings settings, int bufferSize=1024, bool leaveOpen=false, CancellationToken cancellationToken=default)
{
using (var writer = new StreamWriter(stream,encoding: System.Text.Encoding.UTF32,bufferSize: bufferSize,leaveOpen: leaveOpen))
using (var jsonWriter = new JsonTextWriter(writer))
{
var ser = JsonSerializer.Create( settings );
ser.Serialize(jsonWriter, value);
jsonWriter.Flush();
}
return Task.CompletedTask;
}
/// <summary>
/// Determines whether [is valid json] [the specified result].
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <param name="result">The result.</param>
/// <returns><c>true</c> if [is valid json] [the specified result]; otherwise, <c>false</c>.</returns>
public static bool IsValidJson<T>(this Stream stream, [NotNullWhen(true)] out T? result)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (stream.Position != 0)
{
stream.Seek(0, SeekOrigin.Begin);
}
JsonSerializerSettings settings = (JsonConvert.DefaultSettings?.Invoke()) ?? new JsonSerializerSettings() { DateTimeZoneHandling = DateTimeZoneHandling.Utc, DateFormatHandling = DateFormatHandling.IsoDateFormat };
settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
var ser = JsonSerializer.Create(settings);
try
{
result = ser.Deserialize<T>(jsonReader);
}
catch { result = default; }
}
return result is not null;
}
/// <summary>
/// Determines whether [is valid json] [the specified settings].
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="stream">The stream.</param>
/// <param name="settings">The settings.</param>
/// <param name="result">The result.</param>
/// <returns><c>true</c> if [is valid json] [the specified settings]; otherwise, <c>false</c>.</returns>
public static bool IsValidJson<T>(this Stream stream, JsonSerializerSettings settings, [NotNullWhen(true)] out T? result)
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
if (stream.Position != 0)
{
stream.Seek(0, SeekOrigin.Begin);
}
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
var ser = JsonSerializer.Create(settings);
try
{
result = ser.Deserialize<T>(jsonReader);
}
catch { result = default; }
}
return result is not null;
}
/// <summary>
/// Determines whether file contains valid json using the specified settings and reads it into the output.
/// </summary>
/// <typeparam name="T">Type to convert into</typeparam>
/// <param name="file">The file.</param>
/// <param name="settings">The settings.</param>
/// <param name="result">The result.</param>
/// <returns><c>true</c> if [is valid json] [the specified settings]; otherwise, <c>false</c>.</returns>
/// <exception cref="System.ArgumentNullException">file</exception>
/// <exception cref="System.ArgumentNullException"></exception>
/// <exception cref="System.ArgumentNullException">settings</exception>
/// <exception cref="System.IO.FileNotFoundException">File could not be accessed</exception>
public static bool IsValidJson<T>(this FileInfo file, JsonSerializerSettings settings, [NotNullWhen(true)] out T? result)
{
if (file is null)
{
throw new ArgumentNullException(nameof(file));
}
if (File.Exists(file.FullName) == false)
{
throw new FileNotFoundException("File could not be accessed",fileName: file.FullName);
}
using var stream = file.OpenRead();
if (stream is null)
{
throw new ArgumentNullException(message:"Could not open the file and access the underlying file stream",paramName: nameof(file));
}
if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
var ser = JsonSerializer.Create(settings);
try
{
result = ser.Deserialize<T>(jsonReader);
}
catch { result = default; }
}
return result is not null;
}
/// <summary>
/// Determines whether file contains valid json using the specified settings and reads it into the output.
/// </summary>
/// <typeparam name="T">Type to convert into</typeparam>
/// <param name="file">The file.</param>
/// <param name="result">The result.</param>
/// <returns><c>true</c> if [is valid json] [the specified result]; otherwise, <c>false</c>.</returns>
/// <exception cref="System.ArgumentNullException">file</exception>
/// <exception cref="System.IO.FileNotFoundException">File could not be accessed</exception>
public static bool IsValidJson<T>(this FileInfo file, [NotNullWhen(true)] out T? result)
{
if (file is null)
{
throw new ArgumentNullException(nameof(file));
}
if (File.Exists(file.FullName) == false)
{
throw new FileNotFoundException("File could not be accessed",fileName: file.FullName);
}
JsonSerializerSettings settings =( JsonConvert.DefaultSettings?.Invoke()) ?? new JsonSerializerSettings() { DateTimeZoneHandling= DateTimeZoneHandling.Utc, DateFormatHandling= DateFormatHandling.IsoDateFormat };
settings.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
return file.IsValidJson<T>(settings,out result);
}
}

How to get email.Body without html tags in C# visual studio?

Im able to get the body of the email via imap in c#, but its inclusive html tags like this...
<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>asd</div></div></body></html>
I would only want "asd" from this...
This is what I use to get it with the html tags:
foreach (var a in messages)
{
//MessageBox.Show($"{a.Body}");
rec_body.AppendText( a.Body);
}
Im using S22.Imap
Install package from Nuget: HTML AgilityPack
You can try;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
string s = doc.DocumentNode.SelectSingleNode("//body").InnerText;
You don't mention which IMAP library you are using, but if you are using MailKit, you could start by more-or-less cloning MimeKit's HtmlTextPreviewer class.
For example (untested):
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using MimeKit.Text;
namespace MyNamespace {
/// <summary>
/// A text extractor for HTML content.
/// </summary>
/// <remarks>
/// A text extractor for HTML content.
/// </remarks>
public class HtmlTextExtractor
{
/// <summary>
/// Initialize a new instance of the <see cref="HtmlTextExtractor"/> class.
/// </summary>
/// <remarks>
/// Creates a new text extractor for HTML.
/// </remarks>
public HtmlTextExtractor ()
{
}
static bool IsWhiteSpace (char c)
{
return char.IsWhiteSpace (c) || (c >= 0x200B && c <= 0x200D);
}
static bool Append (StringBuilder extracted, string value, ref bool lwsp)
{
int i;
for (i = 0; i < value.Length; i++) {
if (IsWhiteSpace (value[i])) {
if (!lwsp) {
extracted.Append (' ');
lwsp = true;
}
} else {
extracted.Append (value[i]);
lwsp = false;
}
}
return false;
}
sealed class HtmlTagContext
{
public HtmlTagContext (HtmlTagId id)
{
TagId = id;
}
public HtmlTagId TagId {
get;
}
public int ListIndex {
get; set;
}
public bool SuppressInnerContent {
get; set;
}
}
static void Pop (IList<HtmlTagContext> stack, HtmlTagId id)
{
for (int i = stack.Count; i > 0; i--) {
if (stack[i - 1].TagId == id) {
stack.RemoveAt (i - 1);
break;
}
}
}
static bool ShouldSuppressInnerContent (HtmlTagId id)
{
switch (id) {
case HtmlTagId.OL:
case HtmlTagId.Script:
case HtmlTagId.Style:
case HtmlTagId.Table:
case HtmlTagId.TBody:
case HtmlTagId.THead:
case HtmlTagId.TR:
case HtmlTagId.UL:
return true;
default:
return false;
}
}
static bool SuppressContent (IList<HtmlTagContext> stack)
{
int lastIndex = stack.Count - 1;
return lastIndex >= 0 && stack[lastIndex].SuppressInnerContent;
}
HtmlTagContext GetListItemContext (IList<HtmlTagContext> stack)
{
for (int i = stack.Count; i > 0; i--) {
var ctx = stack[i - 1];
if (ctx.TagId == HtmlTagId.OL || ctx.TagId == HtmlTagId.UL)
return ctx;
}
return null;
}
/// <summary>
/// Extract text from HTML input.
/// </summary>
/// <remarks>
/// Gets the extracted text from HTML input.
/// </remarks>
/// <param name="reader">A reader for HTML text.</param>
/// <returns>A string representing the extracted text.</returns>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="reader"/> is <c>null</c>.
/// </exception>
public string Extract (TextReader reader)
{
if (reader == null)
throw new ArgumentNullException (nameof (reader));
var tokenizer = new HtmlTokenizer (reader) { IgnoreTruncatedTags = true };
var stack = new List<HtmlTagContext> ();
var extracted = new StringBuilder ();
var prefix = string.Empty;
HtmlTagContext ctx;
HtmlAttribute attr;
bool body = false;
bool full = false;
bool lwsp = true;
HtmlToken token;
while (!full && tokenizer.ReadNextToken (out token)) {
switch (token.Kind) {
case HtmlTokenKind.Tag:
var tag = (HtmlTagToken) token;
if (!tag.IsEndTag) {
if (body) {
switch (tag.Id) {
case HtmlTagId.Image:
if ((attr = tag.Attributes.FirstOrDefault (x => x.Id == HtmlAttributeId.Alt)) != null) {
Append (extracted, prefix + attr.Value, ref lwsp);
prefix = string.Empty;
}
break;
case HtmlTagId.LI:
if ((ctx = GetListItemContext (stack)) != null) {
if (ctx.TagId == HtmlTagId.OL) {
Append (extracted, $" {++ctx.ListIndex}. ", ref lwsp);
prefix = string.Empty;
} else {
//Append (extracted, " \u2022 ", ref lwsp);
prefix = " ";
}
}
break;
case HtmlTagId.Br:
case HtmlTagId.P:
extracted.Append ('\n');
prefix = string.Empty;
lwsp = true;
break;
}
if (!tag.IsEmptyElement) {
ctx = new HtmlTagContext (tag.Id) {
SuppressInnerContent = ShouldSuppressInnerContent (tag.Id)
};
stack.Add (ctx);
}
} else if (tag.Id == HtmlTagId.Body && !tag.IsEmptyElement) {
body = true;
}
} else if (tag.Id == HtmlTagId.Body) {
stack.Clear ();
body = false;
} else {
Pop (stack, tag.Id);
}
break;
case HtmlTokenKind.Data:
if (body && !SuppressContent (stack)) {
var data = (HtmlDataToken) token;
Append (extracted, prefix + data.Data, ref lwsp);
prefix = string.Empty;
}
break;
}
}
if (lwsp && extracted.Length > 0)
extracted.Length--;
return extracted.ToString ();
}
/// <summary>
/// Extract text from HTML input.
/// </summary>
/// <remarks>
/// Gets the extracted text from HTML input.
/// </remarks>
/// <param name="reader">A reader for HTML text.</param>
/// <returns>A string representing the extracted text.</returns>
/// <exception cref="System.ArgumentNullException">
/// <paramref name="reader"/> is <c>null</c>.
/// </exception>
public string Extract (string input)
{
if (input == null)
throw new ArgumentNullException (nameof (input));
using (var reader = new StringReader (input))
return Extract (reader);
}
}
}
To use this class:
var extractor = new HtmlTextExtractor ();
var text = extractor.Extract ("<html><body>This is some text.</body></html>");
The class I've written above also tries to handle HTML list tags as well, so you may find that nice :)
You can try the following code to get email body without html via IMAP.
First, please install nuget-package ImapX.
Second, you can try the following code to read the email body information from the gmail inbox.
var client = new ImapX.ImapClient("imap.gmail.com", 993, true);
bool t=client.Connect();
bool m=client.Login("Username#gmail.com", "userpassword");
var inbox= client.Folders.Inbox;
var messages = inbox.Search("ALL", ImapX.Enums.MessageFetchMode.Full);
foreach (var item in messages)
{
Console.WriteLine(item.Body.Text);
Console.WriteLine("*****************");
}
First email in gmail website:
First email in console:

Get all Shapes with Charts from a Slide

I am working with a Powerpoint slide and try to extract the names of all shapes with charts from the slide. I can find out if the Slide has charts at all, but I am stuck as to how to find the correspondending Slide.
code:
// suppose that a presentation is correctly loaded
private List<PPChart> GetChartsfromSlide(SlidePart slidepart)
{
var chartList = new List<PPChart>();
if (slidepart.ChartParts.Any())
{
foreach (var chart in slidepart.ChartParts)
{
// how to get the ID of the Chart and get the corespondending slide?
}
}
return chartList;
}
I found a solution, but the Solution is rather involved:
Hopefully, it helps somebody in the future...
/// <summary>
/// Gets a List of all Charts on this Slide
/// </summary>
/// <param name="slidepart">The SlidePart.</param>
/// <returns>A List of all Charts on this Slide</returns>
private List<PPChart> GetChartsfromSlide(SlidePart slidepart)
{
var chartList = new List<PPChart>();
if (slidepart.ChartParts.Any())
{
foreach (var chart in slidepart.ChartParts)
{
//// get the ID of the Chart-Part
var id = slidepart.GetIdOfPart(chart);
//// Get a list of all Shapes(Graphicframes) which contain Charts
var gshapes = from shapeDesc in slidepart.Slide.Descendants<GraphicFrame>() select shapeDesc;
var tempgshapes = gshapes.ToList();
//// Select all possible Shapes which have Graphics
var thisShape = from Gshape in tempgshapes where this.HasThisChart(id, Gshape) select Gshape;
var result = thisShape.ToList();
this.logger.Debug("Found Chart with ID:{0} Name:{1}", result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Id, result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name);
var ppchart = new PPChart(result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Id);
ppchart.ShapeName = result[0].NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;
chartList.Add(ppchart);
}
}
return chartList;
}
/// <summary>
/// Determines whether the Slider has this Chart or not.
/// </summary>
/// <param name="id">The id.</param>
/// <param name="gframe">The gframe.</param>
/// <returns>
/// <c>true</c> if the Slide has the chart; otherwise, <c>false</c>.
/// </returns>
private bool HasThisChart(string id, GraphicFrame gframe)
{
var returnValue = false;
if (!(gframe == null) && this.HasGraphic(gframe))
{
if (!(gframe.Graphic.GraphicData == null))
{
var graphicData = gframe.Graphic.GraphicData;
var drawChartsRef = graphicData.Descendants<DrawCharts.ChartReference>();
if (!(drawChartsRef == null))
{
foreach (var drawChart in drawChartsRef)
{
if (drawChart.Id == id)
{
returnValue = true;
}
}
}
}
}
return returnValue;
}
/// <summary>
/// Determines whether the specified GraphicFrame has a graphic (A graphic is a chart!).
/// </summary>
/// <param name="gframe">The gframe.</param>
/// <returns>
/// <c>true</c> if the specified gframe has a graphic; otherwise, <c>false</c>.
/// </returns>
private bool HasGraphic(GraphicFrame gframe)
{
var returnValue = false;
if (!(gframe == null))
{
var graphicDescendants = gframe.Descendants<Draw.Graphic>();
if (graphicDescendants.Count() > 0)
{
returnValue = true;
}
}
return returnValue;
}

WMI Remote Process Starting

Scenario
I've written a WMI Wrapper that seems to be quite sufficient, however whenever I run the code to start a remote process on a server, I see the process name appear in the task manager but the process itself does not start like it should (as in, I don't see the command line log window of the process that prints out what it's doing etc.)
The process I am trying to start is just a C# application executable that I have written.
Below is my WMI Wrapper Code and the code I am using to start running the process.
Question
Is the process actually running? - Even if it is only displaying the process name in the task manager and not actually launching the application to the users window?
Code To Start The Process
IPHostEntry hostEntry = Dns.GetHostEntry("InsertServerName");
WMIWrapper wrapper = new WMIWrapper("Insert User Name", "Insert Password", hostEntry.HostName);
List<Process> processes = wrapper.GetProcesses();
foreach (Process process in processes)
{
if (process.Caption.Equals("MyAppName.exe"))
{
Console.WriteLine(process.Caption);
Console.WriteLine(process.CommandLine);
int processId;
wrapper.StartProcess("E:\\MyData\\Data\\MyAppName.exe", out processId);
Console.WriteLine(processId.ToString());
}
}
Console.ReadLine();
WMI Wrapper Code
using System;
using System.Collections.Generic;
using System.Management;
using System.Runtime.InteropServices;
using Common.WMI.Objects;
using System.Net;
namespace Common.WMIWrapper
{
public class WMIWrapper : IDisposable
{
#region Constructor
/// <summary>
/// Creates a new instance of the wrapper
/// </summary>
/// <param jobName="username"></param>
/// <param jobName="password"></param>
/// <param jobName="server"></param>
public WMIWrapper(string server)
{
Initialise(server);
}
/// <summary>
/// Creates a new instance of the wrapper
/// </summary>
/// <param jobName="username"></param>
/// <param jobName="password"></param>
/// <param jobName="server"></param>
public WMIWrapper(string username, string password, string server)
{
Initialise(username, password, server);
}
#endregion
#region Destructor
/// <summary>
/// Clean up unmanaged references
/// </summary>
~WMIWrapper()
{
Dispose(false);
}
#endregion
#region Initialise
/// <summary>
/// Initialise the WMI Connection (local machine)
/// </summary>
/// <param name="server"></param>
private void Initialise(string server)
{
m_server = server;
// set connection options
m_connectOptions = new ConnectionOptions();
IPHostEntry host = Dns.GetHostEntry(Environment.MachineName);
}
/// <summary>
/// Initialise the WMI connection
/// </summary>
/// <param jobName="username">Username to connect to server with</param>
/// <param jobName="password">Password to connect to server with</param>
/// <param jobName="server">Server to connect to</param>
private void Initialise(string username, string password, string server)
{
m_server = server;
// set connection options
m_connectOptions = new ConnectionOptions();
IPHostEntry host = Dns.GetHostEntry(Environment.MachineName);
if (host.HostName.Equals(server, StringComparison.OrdinalIgnoreCase))
return;
m_connectOptions.Username = username;
m_connectOptions.Password = password;
m_connectOptions.Impersonation = ImpersonationLevel.Impersonate;
m_connectOptions.EnablePrivileges = true;
}
#endregion
/// <summary>
/// Return a list of available wmi namespaces
/// </summary>
/// <returns></returns>
public List<String> GetWMINamespaces()
{
ManagementScope wmiScope = new ManagementScope(String.Format("\\\\{0}\\root", this.Server), this.ConnectionOptions);
List<String> wmiNamespaceList = new List<String>();
ManagementClass wmiNamespaces = new ManagementClass(wmiScope, new ManagementPath("__namespace"), null); ;
foreach (ManagementObject ns in wmiNamespaces.GetInstances())
wmiNamespaceList.Add(ns["Name"].ToString());
return wmiNamespaceList;
}
/// <summary>
/// Return a list of available classes in a namespace
/// </summary>
/// <param jobName="wmiNameSpace">Namespace to get wmi classes for</param>
/// <returns>List of classes in the requested namespace</returns>
public List<String> GetWMIClassList(string wmiNameSpace)
{
ManagementScope wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\{1}", this.Server, wmiNameSpace), this.ConnectionOptions);
List<String> wmiClasses = new List<String>();
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher(wmiScope, new WqlObjectQuery("SELECT * FROM meta_Class"), null);
foreach (ManagementClass wmiClass in wmiSearcher.Get())
wmiClasses.Add(wmiClass["__CLASS"].ToString());
return wmiClasses;
}
/// <summary>
/// Get a list of wmi properties for the specified class
/// </summary>
/// <param jobName="wmiNameSpace">WMI Namespace</param>
/// <param jobName="wmiClass">WMI Class</param>
/// <returns>List of properties for the class</returns>
public List<String> GetWMIClassPropertyList(string wmiNameSpace, string wmiClass)
{
List<String> wmiClassProperties = new List<string>();
ManagementClass managementClass = GetWMIClass(wmiNameSpace, wmiClass);
foreach (PropertyData property in managementClass.Properties)
wmiClassProperties.Add(property.Name);
return wmiClassProperties;
}
/// <summary>
/// Returns a list of methods for the class
/// </summary>
/// <param jobName="wmiNameSpace"></param>
/// <param jobName="wmiClass"></param>
/// <returns></returns>
public List<String> GetWMIClassMethodList(string wmiNameSpace, string wmiClass)
{
List<String> wmiClassMethods = new List<string>();
ManagementClass managementClass = GetWMIClass(wmiNameSpace, wmiClass);
foreach (MethodData method in managementClass.Methods)
wmiClassMethods.Add(method.Name);
return wmiClassMethods;
}
/// <summary>
/// Retrieve the specified management class
/// </summary>
/// <param jobName="wmiNameSpace">Namespace of the class</param>
/// <param jobName="wmiClass">Type of the class</param>
/// <returns></returns>
public ManagementClass GetWMIClass(string wmiNameSpace, string wmiClass)
{
ManagementScope wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\{1}", this.Server, wmiNameSpace), this.ConnectionOptions);
ManagementClass managementClass = null;
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher(wmiScope, new WqlObjectQuery(String.Format("SELECT * FROM meta_Class WHERE __CLASS = '{0}'", wmiClass)), null);
foreach (ManagementClass wmiObject in wmiSearcher.Get())
managementClass = wmiObject;
return managementClass;
}
/// <summary>
/// Get an instance of the specficied class
/// </summary>
/// <param jobName="wmiNameSpace">Namespace of the classes</param>
/// <param jobName="wmiClass">Type of the classes</param>
/// <returns>Array of management classes</returns>
public ManagementObject[] GetWMIClassObjects(string wmiNameSpace, string wmiClass)
{
ManagementScope wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\{1}", this.Server, wmiNameSpace), this.ConnectionOptions);
List<ManagementObject> wmiClasses = new List<ManagementObject>();
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher(wmiScope, new WqlObjectQuery(String.Format("SELECT * FROM {0}", wmiClass)), null);
foreach (ManagementObject wmiObject in wmiSearcher.Get())
wmiClasses.Add(wmiObject);
return wmiClasses.ToArray();
}
/// <summary>
/// Get a full list of services
/// </summary>
/// <returns></returns>
public List<Service> GetServices()
{
return GetService(null);
}
/// <summary>
/// Get a list of services
/// </summary>
/// <returns></returns>
public List<Service> GetService(string name)
{
ManagementObject[] services = GetWMIClassObjects("CIMV2", "WIN32_Service");
List<Service> serviceList = new List<Service>();
for (int i = 0; i < services.Length; i++)
{
ManagementObject managementObject = services[i];
Service service = new Service(managementObject);
service.Status = (string)managementObject["Status"];
service.Name = (string)managementObject["Name"];
service.DisplayName = (string)managementObject["DisplayName"];
service.PathName = (string)managementObject["PathName"];
service.ProcessId = (uint)managementObject["ProcessId"];
service.Started = (bool)managementObject["Started"];
service.StartMode = (string)managementObject["StartMode"];
service.ServiceType = (string)managementObject["ServiceType"];
service.InstallDate = (string)managementObject["InstallDate"];
service.Description = (string)managementObject["Description"];
service.Caption = (string)managementObject["Caption"];
if (String.IsNullOrEmpty(name) || name.Equals(service.Name, StringComparison.OrdinalIgnoreCase))
serviceList.Add(service);
}
return serviceList;
}
/// <summary>
/// Get a list of processes
/// </summary>
/// <returns></returns>
public List<Process> GetProcesses()
{
return GetProcess(null);
}
/// <summary>
/// Get a list of processes
/// </summary>
/// <returns></returns>
public List<Process> GetProcess(uint? processId)
{
ManagementObject[] processes = GetWMIClassObjects("CIMV2", "WIN32_Process");
List<Process> processList = new List<Process>();
for (int i = 0; i < processes.Length; i++)
{
ManagementObject managementObject = processes[i];
Process process = new Process(managementObject);
process.Priority = (uint)managementObject["Priority"];
process.ProcessId = (uint)managementObject["ProcessId"];
process.Status = (string)managementObject["Status"];
DateTime createDate;
if (ConvertFromWmiDate((string)managementObject["CreationDate"], out createDate))
process.CreationDate = createDate.ToString("dd-MMM-yyyy HH:mm:ss");
process.Caption = (string)managementObject["Caption"];
process.CommandLine = (string)managementObject["CommandLine"];
process.Description = (string)managementObject["Description"];
process.ExecutablePath = (string)managementObject["ExecutablePath"];
process.ExecutionState = (string)managementObject["ExecutionState"];
process.MaximumWorkingSetSize = (UInt32?)managementObject ["MaximumWorkingSetSize"];
process.MinimumWorkingSetSize = (UInt32?)managementObject["MinimumWorkingSetSize"];
process.KernelModeTime = (UInt64)managementObject["KernelModeTime"];
process.ThreadCount = (UInt32)managementObject["ThreadCount"];
process.UserModeTime = (UInt64)managementObject["UserModeTime"];
process.VirtualSize = (UInt64)managementObject["VirtualSize"];
process.WorkingSetSize = (UInt64)managementObject["WorkingSetSize"];
if (processId == null || process.ProcessId == processId.Value)
processList.Add(process);
}
return processList;
}
/// <summary>
/// Start the specified process
/// </summary>
/// <param jobName="commandLine"></param>
/// <returns></returns>
public bool StartProcess(string command, out int processId)
{
processId = int.MaxValue;
ManagementClass processClass = GetWMIClass("CIMV2", "WIN32_Process");
object[] objectsIn = new object[4];
objectsIn[0] = command;
processClass.InvokeMethod("Create", objectsIn);
if (objectsIn[3] == null)
return false;
processId = int.Parse(objectsIn[3].ToString());
return true;
}
/// <summary>
/// Schedule a process on the remote machine
/// </summary>
/// <param name="command"></param>
/// <param name="scheduleTime"></param>
/// <param name="jobName"></param>
/// <returns></returns>
public bool ScheduleProcess(string command, DateTime scheduleTime, out string jobName)
{
jobName = String.Empty;
ManagementClass scheduleClass = GetWMIClass("CIMV2", "Win32_ScheduledJob");
object[] objectsIn = new object[7];
objectsIn[0] = command;
objectsIn[1] = String.Format("********{0:00}{1:00}{2:00}.000000+060", scheduleTime.Hour, scheduleTime.Minute, scheduleTime.Second);
objectsIn[5] = true;
scheduleClass.InvokeMethod("Create", objectsIn);
if (objectsIn[6] == null)
return false;
UInt32 scheduleid = (uint)objectsIn[6];
jobName = scheduleid.ToString();
return true;
}
/// <summary>
/// Returns the current time on the remote server
/// </summary>
/// <returns></returns>
public DateTime Now()
{
ManagementScope wmiScope = new ManagementScope(String.Format("\\\\{0}\\root\\{1}", this.Server, "CIMV2"), this.ConnectionOptions);
ManagementClass managementClass = null;
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher(wmiScope, new WqlObjectQuery(String.Format("SELECT * FROM Win32_LocalTime")), null);
DateTime localTime = DateTime.MinValue;
foreach (ManagementObject time in wmiSearcher.Get())
{
UInt32 day = (UInt32)time["Day"];
UInt32 month = (UInt32)time["Month"];
UInt32 year = (UInt32)time["Year"];
UInt32 hour = (UInt32)time["Hour"];
UInt32 minute = (UInt32)time["Minute"];
UInt32 second = (UInt32)time["Second"];
localTime = new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second);
};
return localTime;
}
/// <summary>
/// Converts a wmi date into a proper date
/// </summary>
/// <param jobName="wmiDate">Wmi formatted date</param>
/// <returns>Date time object</returns>
private static bool ConvertFromWmiDate(string wmiDate, out DateTime properDate)
{
properDate = DateTime.MinValue;
string properDateString;
// check if string is populated
if (String.IsNullOrEmpty(wmiDate))
return false;
wmiDate = wmiDate.Trim().ToLower().Replace("*", "0");
string[] months = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
try
{
properDateString = String.Format("{0}-{1}-{2} {3}:{4}:{5}.{6}",
wmiDate.Substring(6, 2), months[int.Parse(wmiDate.Substring(4, 2)) - 1], wmiDate.Substring(0, 4), wmiDate.Substring(8, 2), wmiDate.Substring(10, 2), wmiDate.Substring(12, 2), wmiDate.Substring(15, 6));
}
catch (InvalidCastException)
{
return false;
}
catch (ArgumentOutOfRangeException)
{
return false;
}
// try and parse the new date
if (!DateTime.TryParse(properDateString, out properDate))
return false;
// true if conversion successful
return true;
}
private bool m_disposed;
#region IDisposable Members
/// <summary>
/// Managed dispose
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose of managed and unmanaged objects
/// </summary>
/// <param jobName="disposing"></param>
public void Dispose(bool disposing)
{
if (disposing)
{
m_connectOptions = null;
}
}
#endregion
#region Properties
private ConnectionOptions m_connectOptions;
/// <summary>
/// Gets or sets the management scope
/// </summary>
private ConnectionOptions ConnectionOptions
{
get
{
return m_connectOptions;
}
set
{
m_connectOptions = value;
}
}
private String m_server;
/// <summary>
/// Gets or sets the server to connect to
/// </summary>
public String Server
{
get
{
return m_server;
}
set
{
m_server = value;
}
}
#endregion
}
}
It is running but it's not running interactive. If you're running a process remotely you can't be sure that someone is even logged in to that pc so you can't assume that it can have a GUI that a user can interact with.
I'd suggest, instead of writing progress to the console window, write it to a file or the Event log.

Translate Service Call Object Back to the Original Object

I have an object exposed through a web service that is consumed by another system. That system also uses that same WSDL to return back an object on demand to us. I was wondering if it was possible to take that same object and relay it back to a the original object?
I tried to do the following and it wouldn't actually cast it back with the object populated... Any help would be great!
Thank you!
Note The method code listed below was based off of http://www.dotnetjohn.com/articles.aspx?articleid=173
public void ConvertBack()
{
ThirdParty.Animal animal;
using (var svc = new ThirdPartySoapClient())
thirdPartyAnimal = svc.GetAnimal("Identifier");
var xml = SerializeObject(thirdPartyAnimal, typeof(ThirdParty.Animal));
var originalAnimal = (OriginalNamespace.Animal)DeserializeObject(xml, typeof(OrginalNamespace.Animal));
Assert.AreEqual(originalAnimal.Name, animal.Name);
}
/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <param name="typeOfObject">typeof() object that is being passed to be serialized to XML</param>
/// <returns>XML string</returns>
public string SerializeObject(object pObject, Type typeOfObject)
{
try
{
var memoryStream = new MemoryStream();
var xs = new XmlSerializer(typeOfObject);
var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, pObject);
memoryStream = (MemoryStream) xmlTextWriter.BaseStream;
var xmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return xmlizedString;
}
catch (Exception e)
{
Console.WriteLine(e);
return null;
}
}
/// <summary>
/// Method to reconstruct an Object from XML string
/// </summary>
/// <param name="pXmlizedString">XML To Be Converted to an Object</param>
/// <param name="typeOfObject">typeof() object that is being passed to be serialized to XML</param>
/// <returns></returns>
public object DeserializeObject(string pXmlizedString, Type typeOfObject)
{
var xs = new XmlSerializer(typeOfObject);
var memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}
/// <summary>
/// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
/// </summary>
/// <param name="characters">Unicode Byte Array to be converted to String</param>
/// <returns>String converted from Unicode Byte Array</returns>
private static string UTF8ByteArrayToString(Byte[] characters)
{
var encoding = new UTF8Encoding();
var constructedString = encoding.GetString(characters);
return (constructedString);
}
/// <summary>
/// Converts the String to UTF8 Byte array and is used in De serialization
/// </summary>
/// <param name="pXmlString"></param>
/// <returns></returns>
private static Byte[] StringToUTF8ByteArray(string pXmlString)
{
var encoding = new UTF8Encoding();
var byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
When generating the proxy from WSDL there is an option called "/sharetypes" which should solve this problem
You can use it from the command line tools or in the options area of the "Add Web Service" dialogs

Categories

Resources