Skip to content

Commit

Permalink
Simplified custom handling example usage #11
Browse files Browse the repository at this point in the history
  • Loading branch information
capslocky committed Sep 30, 2018
1 parent 3662289 commit 56bb71d
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 152 deletions.
30 changes: 17 additions & 13 deletions src/Library/WcfSoapLogger/HandlerCustom/HandlerCustom.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Text;
using WcfSoapLogger.EncodingExtension;
using WcfSoapLogger.Exceptions;
using WcfSoapLogger.EncodingExtension;

namespace WcfSoapLogger.HandlerCustom
{
Expand All @@ -25,38 +22,45 @@ public override void HandleBody(byte[] body, bool request)
this.HandleBodyOnClient(body, request);
}
}


// see stages
// https://github.com/capslocky/WcfSoapLogger/blob/master/docs/GeneralFlow.md

private void HandleBodyOnService(byte[] body, bool request)
{
if (request)
{
// service request [stage 3. logging]

SoapLoggerService.SetSettings(_settings);
SoapLoggerService.SetRequestBody(body);
return;
}
else
{
// service response [stage 5. logging ]

// service response [5. logging ]
var requestException = SoapLoggerService.GetRequestException();

var requestException = SoapLoggerService.GetRequestException();
if (requestException != null)
{
throw requestException;
}

if (requestException != null)
{
throw requestException;
SoapLoggerService.CallResponseCallback(body, _settings);
}

SoapLoggerService.CallResponseCallback(body, _settings);
}


private void HandleBodyOnClient(byte[] body, bool request)
{
if (request)
{
// client request [stage 2. logging]
SoapLoggerClient.CallRequestCallback(body, _settings);
}
else
{
// client response [stage 6. logging ]
SoapLoggerClient.CallResponseCallback(body, _settings);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static void CallRequestCallback(byte[] requestBody, SoapLoggerSettings
if (_client == null)
{
const string methodName = "SoapLoggerClient.SetCustomHandlerCallbacks";
throw new LoggerException("You have enabled 'useCustomHandler' for client class of given service. So method '" + methodName+ "' should be called before making any request. Make sure to call it everywhere.");
throw new LoggerException("You have enabled 'useCustomHandler' for client class of given service. So method '" + methodName+ "' should be called before each request (typically in constructor of inhereted client class).");
}

_client.HandleRequestBodyCallback(requestBody, settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal static void CallResponseCallback(byte[] responseBody, SoapLoggerSetting

//TODO add logging error message to file

throw new LoggerException("something went wrong, either pipeline execution didn't reach web-service method or web-service method didn't call 'ReadRequestSetResponseCallback'");
throw new LoggerException("something went wrong, either pipeline execution didn't reach web-service method or web-service method didn't execute 'SoapLoggerService.CallCustomHandlers'");
}

try
Expand Down
2 changes: 1 addition & 1 deletion src/UsageExamples/ExampleAlpha/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private static void Main()
Console.WriteLine();

var serviceClient = new WeatherServiceClient();
var randomDataClient = new RandomDataClient(serviceClient);
var randomDataClient = new RandomDataClient(() => serviceClient);
randomDataClient.StartThreads();

Console.ReadLine();
Expand Down
4 changes: 3 additions & 1 deletion src/UsageExamples/ExampleBeta/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ private static void Main()
Console.ReadKey();
Console.WriteLine();

// if no custom handling - it's ok to reuse client class object

var serviceClient = new WeatherServiceClient();
var randomDataClient = new RandomDataClient(serviceClient);
var randomDataClient = new RandomDataClient(() => serviceClient);
randomDataClient.StartThreads();

Task.Delay(500).ContinueWith(_ => Process.Start("explorer.exe", @"C:\SoapLog\Beta"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CustomHandling\RandomDataClientCustomHandler.cs" />
<Compile Include="CustomHandling\WeatherServiceClientCustomHandler.cs" />
<Compile Include="RandomDataClient.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WeatherServiceClient.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,17 @@

namespace CommonClient.CustomHandling
{
public class RandomDataClientCustomHandler : RandomDataClient, ISoapLoggerHandlerClient
public class WeatherServiceClientCustomHandler : WeatherServiceClient, ISoapLoggerHandlerClient
{
public RandomDataClientCustomHandler(WeatherServiceClient client) : base(client)
public WeatherServiceClientCustomHandler()
{
}

protected override void SendRandomReport(string location)
{
SoapLoggerClient.SetCustomHandlerCallbacks(this);
base.SendRandomReport(location);
}

protected override void GetLastReport(string location) {
SoapLoggerClient.SetCustomHandlerCallbacks(this);
base.GetLastReport(location);
}

protected override void GetForecast(string location, int days) {
// you need just this line in constructor of inherited client class to apply your custom handler to all requests
// NOTE: don't reuse this custom handling client class object twice, it should be instantiated for every new request
SoapLoggerClient.SetCustomHandlerCallbacks(this);
base.GetForecast(location, days);
}





public void HandleRequestBodyCallback(byte[] requestBody, SoapLoggerSettings settings)
{
WriteFileCustom(requestBody, true, settings.LogPath);
Expand All @@ -50,8 +35,7 @@ public void CustomHandlersDisabledCallback(SoapLoggerSettings settings)





// this custom handling method looks for 'GetLastReportByLocation' only
private void WriteFileCustom(byte[] body, bool request, string logPath)
{
const string operationNameToLog = "GetLastReportByLocation";
Expand All @@ -70,12 +54,8 @@ private void WriteFileCustom(byte[] body, bool request, string logPath)
}

fileNameFactory.AddSegment(operationName);

fileNameFactory.AddSegment(message.GetNodeValue("Body", operationNameToLog, "Location"));

// fileNameFactory.AddSegment(message.GetNodeValue("Body", "SendReport", "report", "location"));
// fileNameFactory.AddSegment(message.GetNodeValue("Body", "GetForecastByLocation", "location"));

fileNameFactory.AddDirection(request);
string indentedXml = message.GetIndentedXml();

Expand All @@ -95,5 +75,6 @@ private void WriteFileCustom(byte[] body, bool request, string logPath)
}
}


}
}
35 changes: 18 additions & 17 deletions src/UsageExamples/ExampleCommon/CommonClient/RandomDataClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -10,12 +7,12 @@ namespace CommonClient
public class RandomDataClient
{
private readonly Random _random;
private readonly IWeatherService _client;
private readonly Func<IWeatherService> _getClient;

public RandomDataClient(IWeatherService client)
public RandomDataClient(Func<IWeatherService> getClient)
{
_random = new Random();
_client = client;
_getClient = getClient;
}

public void StartThreads()
Expand Down Expand Up @@ -46,7 +43,7 @@ private void ThreadSleep(int minValue, int maxValue)
Thread.Sleep(TimeSpan.FromMilliseconds(_random.Next(minValue, maxValue)));
}

protected virtual void SendRandomReport(string location) {
protected void SendRandomReport(string location) {
WeatherReport newReport = new WeatherReport();

newReport.DateTime = DateTime.Now;
Expand All @@ -61,7 +58,7 @@ protected virtual void SendRandomReport(string location) {

try
{
long id = _client.SendReport(newReport);
long id = _getClient().SendReport(newReport);
Console.WriteLine("Report for " + location + ": Report ID = " + id);
}
catch (Exception ex)
Expand All @@ -70,10 +67,14 @@ protected virtual void SendRandomReport(string location) {
}
}

private float GetRandomValue(float minimum, float maximum)
{
return (float)_random.NextDouble() * (maximum - minimum) + minimum;
}
private float GetRandomValue(float minimum, float maximum)
{
double value = minimum + _random.NextDouble() * (maximum - minimum);
value = value * 100;
value = Math.Round(value, MidpointRounding.AwayFromZero);
value = value / 100;
return (float) value;
}


private void WatchLastReport(string location)
Expand All @@ -85,11 +86,11 @@ private void WatchLastReport(string location)
}
}

protected virtual void GetLastReport(string location)
protected void GetLastReport(string location)
{
try
{
var report = _client.GetLastReportByLocation(location);
var report = _getClient().GetLastReportByLocation(location);

if (report == null)
{
Expand All @@ -110,16 +111,16 @@ private void WatchForecast(string location, int days)
{
while (true)
{
ThreadSleep(4000, 7000);
ThreadSleep(3000, 6000);
GetForecast(location, days);
}
}

protected virtual void GetForecast(string location, int days)
protected void GetForecast(string location, int days)
{
try
{
var forecastArray = _client.GetForecastByLocation(location, days);
var forecastArray = _getClient().GetForecastByLocation(location, days);
Console.WriteLine("Forecast for " + location + " for " + forecastArray.Length + " days received.");
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
<Compile Include="IWeatherService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReportRepository.cs" />
<Compile Include="CustomHandling\CustomHandler_GetForecastByLocation.cs" />
<Compile Include="WeatherServiceEurope.cs" />
<Compile Include="CustomHandling\WeatherServiceEuropeCustomHandler.cs" />
</ItemGroup>
Expand Down

This file was deleted.

Loading

0 comments on commit 56bb71d

Please sign in to comment.