我想修改我的json.NET序列化程序,只将$ type属性添加到实现给定接口但不对任何属性或嵌套对象的对象.
使用TypeNameHandling.Auto(默认)
{
"PropertyA": 123,
"PropertyB": "foo",
"PropertyC": [1, 2, 3, 4]
}
Run Code Online (Sandbox Code Playgroud)
使用TypeNameHandling.All
{
"$type": "JsonNetTypeNameHandling.TestEvent, jsonNetTypeNameHandling",
"PropertyA": 123,
"PropertyB": "foo",
"PropertyC": {
"$type": "System.Collections.Generic.List`1[[System.Int32, mscorlib]], mscorlib",
"$values": [1, 2, 3, 4 ]
}
}
Run Code Online (Sandbox Code Playgroud)
我想要的是
{
"$type": "JsonNetTypeNameHandling.TestEvent, jsonNetTypeNameHandling",
"PropertyA": 123,
"PropertyB": "foo",
"PropertyC": [1, 2, 3, 4]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用自定义的ContractResolver,但我没有让它工作:
class Program
{
static void Main(string[] args)
{
var serializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
ContractResolver = new EnableTypeNameHandlingAllOnlyForEvents(), …Run Code Online (Sandbox Code Playgroud) 您是否曾尝试在具有完整IIS和多个角色实例的Windows Azure模拟器中运行托管服务?几天前,我注意到一次只有一个Web角色的多个实例在IIS中启动.以下屏幕截图说明了行为,屏幕截图前面的消息框显示了此行为的原因.尝试在IIS管理器中启动其中一个已停止的网站时出现消息框.
示例云应用程序包含两个Web角色:MvcWebRole1和WCFServiceWebRole1,每个都配置为使用三个实例.我的第一个想法是:"当然!在真正的天蓝色世界中不会发生端口冲突,因为每个角色实例都是一个自己的虚拟机.它无法在模拟器中运行!" 但经过一些研究和分析azure计算模拟器的许多部分后,我发现计算模拟器为每个角色实例创建了一个唯一的IP(在我的例子中从127.255.0.0到127.255.0.5).这篇MSDN博客文章(http://blogs.msdn.com/b/avkashchauhan/archive/2011/09/16/whats-new-in-windows-azure-sdk-1-5-each-instance-in-any微软员工Avkash Chauhan的-role-gets-its-own-ip-address-to-match-compute-emulator-close-the-cloud-environment.aspx)也描述了这种行为.在得出结论后,我提出了以下问题:为什么计算模拟器(更准确地说是DevFC.exe)没有将相应角色的IP添加到每个网站的绑定信息中?
我手动和tadaaaaa为每个网站添加了IP:每个网站都可以在没有任何冲突的情况下启动.下一个屏幕截图显示了突出显示已更改的绑定信息.
再一次:为什么模拟器不能为我做这件事?我写了一个小的静态助手方法,在每个角色开始时为我做绑定扩展.也许有人想要使用它:
public static class Emulator
{
public static void RepairBinding(string siteNameFromServiceModel, string endpointName)
{
// Use a mutex to mutually exclude the manipulation of the iis configuration.
// Otherwise server.CommitChanges() will throw an exeption!
using (var mutex = new System.Threading.Mutex(false, "AzureTools.Emulator.RepairBinding"))
{
mutex.WaitOne();
using (var server = new Microsoft.Web.Administration.ServerManager())
{
var siteName = string.Format("{0}_{1}", Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
var site = server.Sites[siteName];
// Add the IP of the role to the binding information …Run Code Online (Sandbox Code Playgroud)