Skip to content

Commit

Permalink
Minor changes to support Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
mfidemraizer committed Nov 19, 2014
1 parent 929fe2f commit 3d94400
Show file tree
Hide file tree
Showing 59 changed files with 205 additions and 79,384 deletions.
9 changes: 8 additions & 1 deletion App.Api/App.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<Reference Include="Owin">
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference>
<Reference Include="StackExchange.Redis">
<HintPath>..\packages\StackExchange.Redis.1.0.371\lib\net45\StackExchange.Redis.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand All @@ -72,13 +75,17 @@
<Compile Include="Configuration.cs" />
<Compile Include="Filters\CultureFilterAttribute.cs" />
<Compile Include="PathHelper.cs" />
<Compile Include="ProductController.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Redis\RedisClientFactory.cs" />
<Compile Include="Startup.cs" />
<Compile Include="StaticFileController.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
7 changes: 6 additions & 1 deletion App.Api/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using App.Api.Filters;
using App.Api.Formatting;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;

namespace App.Api
{
public sealed class Configuration : HttpConfiguration
{
public Configuration()
{
this.Filters.Add(new CultureFilterAttribute());
//Services.Add(typeof(IExceptionHandler), null);

//Formatters.Insert(0, new JsonNetMediaTypeFormatter());
Filters.Add(new CultureFilterAttribute());
this.MapHttpAttributeRoutes();
}
}
Expand Down
111 changes: 111 additions & 0 deletions App.Api/Formatting/JsonMediaTypeFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Dynamic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace App.Api.Formatting
{
public class JsonNetMediaTypeFormatter : MediaTypeFormatter
{
public JsonNetMediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
}

public override bool CanReadType(Type type)
{
if (type == typeof(JValue) || type == typeof(JObject) || type == typeof(JArray))
return false;

return true;
}

public override bool CanWriteType(Type type)
{
//if (type == typeof(IKeyValueModel))

if (type.Name == "IKeyValueModel")
return false;

return true;
}

public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
TaskCompletionSource<object> completionSource = new TaskCompletionSource<object>();

var settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
};

string jsonText;

using (StreamReader reader = new StreamReader(readStream))
{
jsonText = reader.ReadToEnd();
}

if (jsonText.Trim().StartsWith("{"))
{
completionSource.SetResult(JsonConvert.DeserializeObject<ExpandoObject>(jsonText, new ExpandoObjectConverter()));
}
else
{
object deserializedObject = JsonConvert.DeserializeObject(jsonText);
JToken token;

if ((token = deserializedObject as JToken) != null)
{
Type clrType;

switch (token.Type)
{
case JTokenType.Array:
clrType = typeof(object[]);
break;

default:
throw new NotSupportedException("Source type not supported");
}

completionSource.SetResult(token.ToObject(clrType));
}
else
{
completionSource.SetResult(deserializedObject);
}
}

return completionSource.Task;
}

public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
TaskCompletionSource<object> completionSource = new TaskCompletionSource<object>();
completionSource.SetResult(null);

var settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};

string json = JsonConvert.SerializeObject(value);

byte[] buf = Encoding.Default.GetBytes(json);
writeStream.Write(buf, 0, buf.Length);
writeStream.Flush();

return completionSource.Task;
}
}
}
31 changes: 31 additions & 0 deletions App.Api/ProductController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using App.Api.Redis;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace App.Api
{
[RoutePrefix("v1/products")]
public sealed class ProductController : ApiController
{
[HttpGet]
public async Task<HttpResponseMessage> GetProductsAsync()
{
HttpResponseMessage response = null;
IDatabase db = RedisClientFactory.GetDatabase();

if(await db.KeyExistsAsync("users"))
{

}

return response;

}
}
}
44 changes: 44 additions & 0 deletions App.Api/Redis/RedisClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace App.Api.Redis
{
public static class RedisClientFactory
{
private readonly static AutoResetEvent _multiplexerResetEvent = new AutoResetEvent(true);
private readonly static ConnectionMultiplexer _multiplexer;

static RedisClientFactory()
{
_multiplexerResetEvent.WaitOne();

try
{
ConfigurationOptions options = new ConfigurationOptions();
options.EndPoints.Add("localhost", 6379);
options.AbortOnConnectFail = false;

_multiplexer = ConnectionMultiplexer.Connect(options);
}
finally
{
_multiplexerResetEvent.Set();
}
}

public static IDatabase GetDatabase()
{
return _multiplexer.GetDatabase();
}

public static ITransaction GetTransaction()
{
return GetDatabase().CreateTransaction();
}
}
}
Binary file modified App.Api/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
Binary file not shown.
1 change: 1 addition & 0 deletions App.Api/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
<package id="StackExchange.Redis" version="1.0.371" targetFramework="net45" />
</packages>
Binary file modified app.v12.suo
Binary file not shown.
1 change: 1 addition & 0 deletions compiledTemplates/ProductList.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiledTemplates/home.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiledTemplates/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions compiledTemplates/master.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 3d94400

Please sign in to comment.