How do I use a custom JsonConverter with NServiceBus? - json.net

I have a class that needs a custom JsonConverter to properly be deserialized. It is a value object (in DDD terminology), and I need to include it in a message being sent with NServiceBus when using Json serialization.
The problem is, since NServiceBus internalizes their copy of Json.Net, I have to use the JsonConverter base class that is included in NSB, but it has been marked "internal" during the merge.
This basically prevents you from hooking any custom serialization code into NSB. Is this by design? Is there a recommended workaround?

If you are having problems with the merged assemblies you can find the core only assemblies on the downloads page on github.

Related

Generate only $ref in JSON.NET JsonSchemaGenerator?

I have a goal of automating converting some of my .NET model classes into Java classes. The current method I am attempting is to use JSON.NET to generate a JSON Schema and then use jsonschema2pojo to produce the Java classes. I have a .NET backend and .NET, Android, and iOS clients. This particular piece is for the Android client to consume JSON RESTful resources from the .NET server.
My specific question is:
Is there a way to have JSON.NET's JsonSchemaGenerator not recurse into nested classes but instead to always specify a $ref? I'm not seeing any options for JsonSchemaGenerator and the JsonProperty attribute doesn't seem to contain anything that would help. I would like to have a schema file for each class and no duplicate definitions of a class when it's nested within another class. jsonschema2pojo seems to work well with a directory full of schema files with each file having the name of the class.

How Orchard CMS does the logging?

I'm working with Orchard CMS and it is better CMS for me. I want to understand how it does the logging and whether I can add my own logging or not. I saw that Orchard uses NullLogger class and it does no work. I've opened the App_Data.Logs folder and have seen that there are the log files. But how? I searched in code where is the trick that replaces NullLogger with log4net (I guess this is log4net, because the log format and the formatting for log4net.config are very similar) but I haven't found this.
Can somebody answer me:
How Orchard does the logging?
Whether I can add my own logger and if yes what best practices exist to do this?
Thanks, Andrey.
An Autofac module (Orchard.Logging.LoggerModule to be precise) handles that. Basically - it scans each dependency and fills all properties of type ILogger with a reference to appropriate logger instance. Each dependency gets its own logger with name equal to full type name (including namespace) of a containing class.
The NullLogger is just a placeholder so accessing the property would not throw NullReferenceExceptions before the property is being set by Autofac.
Extending the default logging is a rather complicated task as it would involve doing three things:
create a custom implementation of ILoggerFactory (just like the default Orchard.Logging.CastleLoggerFactory) and
create an Autofac module that registers that implementation in the container (like the mentioned LoggerModule does)
suppress the current default logging module by decorating your new one with [OrchardSuppressDependency("Orchard.Logging.LoggingModule")]
UPDATE
Just realized I haven't addressed the most important part of the question here:)
Yes, Orchard uses log4net so you may alter the default settings via Config/log4net.config file.
There is a great article on how Orchard Logging works. (I am not sure if it is ok to copy and paste the entire article). This is the link: Injection Logger in Orchard
So the trick is this:
Whenever a class requires a Logger instance, all it needs to do is to
declare a ILogger property, that’s it. And later, in your class, you
can use this property to Logging at anytime
And how is this done?
When Orchard web application startup, the OrchardStarter will be used
to do most of the registration work.
In a few words, it looks all the code in all projects, gets all the classes that use an ILogger property, and implements it for you (if not implemented), using Castle's logger factory.

ASP.NET with Unity 2.0

Can anyone help point me to some good resources for working with Unity 2.0 in an ASP.NET that doesn't talk about ASP.NET MVC?!?
We are not using MVC and I am struggling to get Unity to inject dependencies into my pages following the couple of examples I've read (which are all based on David Hayden's work so they are all presenting the same examples and code).
UPDATE
I tried to go the PageHandlerFactory route but the example (here) is incomplete and no source code is available to accompany the article.
So, I decided to try the custom HttpModule approach described here and here. Again, no source code is available beyond what is shown so it is difficult to troubleshoot issues.
The problem I have now is that all of the plumbing appears to be wiring up correctly but the Buildup method does nothing to my page. I can see all of the types are registered in the container when I set a break-point in the module code and the code is executing as expected. But a break-point in the Page_Load event handler shows that the dependencies are all null.
The property in question is public, with a setter and getter, and is marked with the Dependency attribute. I've tried with and without the attribute, with and without a mapping name, ... every combination I could think of and nothing works.
What am I missing???
It depends what you expect. Most exemples targeting MVC present custom controller factory which allows creating controllers with dependency injection. This is indeed also possible with web forms but instead of controller you must inject dependencies into pages. To do this you must replace PageHandlerFactory with custom implementation.
You can create your own implementation of PageHandlerFactory which will be able to resolve pages directly and inject dependencies defined in constructor or you can use one of these (here, here and here) which instead uses stantandard PageHandlerFactory and builds page instance (resolve property dependencies).

WCF: Should all datacontracts use the same namespace?

I have several service contracts exposed over WCF, which use multiple datacontracts. The service is to be consumed by Adobe Flex. I've run into many problems getting Flex to consume the WSDL produced by the server. I used the procedure outlined here to squeeze the WSDL into a single file (I used FlatWsdl and not WcfExtras, if it matters).
Now I'm getting a bunch of errors when using VS2010's own wsdl.exe tool to extract my metadata. Specifically, I'm getting the following errors:
1) Schema validation warning: Schema item 'complexType' named
'ArrayOfKeyValueOfSomeKeySomeValuep1alXzIb' from namespace
'http://schemas.microsoft.com/2003/10/Serialization/Arrays' is invalid.
Namespace 'http://My/Wcf/Namespace' is not available to be referenced
in this schema.
2) Schema validation warning: Schema item 'complexType' named 'MyComplexType'
from namespace 'http://My/Wcf/Namespace' is invalid. Namespace
'http://schemas.datacontract.org/2004/07/My.Real.Namespace' is not available
to be referenced in this schema.
Now, the Dictionary ("ArrayOfKeyValueOf...") type is apparently defined in an external schema, which is not imported by my WSDL. Please note that SomeKey and SomeValue have a datacontract namespace of My/Wcf/Namespace.
As for MyComplexType, the weird thing is that if I change its Datacontract-namespace to the same namespace used by my services, servers, and bindings, then error (2) goes away. I can't figure out why.
What I'm asking boils down to two questions:
1) How can I add an import directive of an external schema to my WSDL? I saw some solutions on-line that suggested writing a wrapper class around the containers. This is something I would really like to avoid.
2) Do I really have to put all my datacontracts into a single namespace? I don't have any technical objection to doing it, but it's going to be very cumbersome to modify the namespace of all datacontracts. Perhaps there's an automated way of achieving this?
TIA!
So just in case anyone else runs into the same problem with accessing a WCF (BasicHttpBinding) server from a Flex client:
1) No, all DataContracts need not be in the same namespace (but all ServiceContracts, service implementations, and bindings do have to be in the same namespace!).
2) The crux of the problem is that Flex expects each xsd:schema to have an "xsd:import namespace" for all the namespaces it references. This import should be of the form:
<xsd:import namespace="http://schemas.datacontract.org/2004/07/SomeNamespace" />
There doesn't seem to be a way to do this with WCF, since it automatically adds an auto-generated schemaLocation attribute to this tag, which makes Flex try to fetch this schemaLocation. The only workaround appears to be manually manipulating the WSDL at the XML level (e.g. by exposing a REST endpoint for retrieving the metadata).
Thanks to Yaron Naveh in the MSDN WCF forums for his help. The discussion can be found at http://social.msdn.microsoft.com/Forums/en/wcf/thread/b9429e30-e4d5-454f-9fbd-bae39990ff33.

How can I list the properties of an object programmatically in VB6?

A friend of mine is working on a legacy VB6 project. I haven't touched that language in ten years, so I'm pretty rusty. Anyway, is there any kind of reflection API for VB6? Specifically, he needs a way to iterate the properties (and types) of a user-created Class. (In other words, not an external COM object, but an internal "Class Module" as it's called.)
How can this be done?
Jay's answer is the way to go if your project is an ActiveX (ActiveX EXE, DLL, or OCX, as opposed to Standard EXE), and if the classes are public.
However, you mentioned that your friend wants to do this with "internal class modules". I'm not sure if you are referring to private .cls files (classes), or .bas files (modules), but either way, you can only use the TypeLib Information Object Library to reflect on public classes, user-defined types, constants, or enumerations.
You cannot use the library to reflect modules, private classes, or anything else that is declared private.
As a general rule of thumb, you can only use reflection on the things that you can see in the Object Browser when viewing your project's contents from another project. That is, if you compile your project, create a new project, add the first project as a reference, and then view the first project's contents in the Object Browser, anything you can see in the Object Browser can be accessed via the TypeLib Information Object Library. If something is not listed in the Object Browser, then you won't be able to use the TypeLib Information Object Library to reflect it.
VB6 doesn't have any built-in support for run-time reflection or introspection. Using the TypeLib Information Object Library for reflection works for ActiveX VB6 projects because ActiveX projects are compiled in COM components with embedded type libraries, but as mentioned you can only access the data types that are publicly exposed in the compiled component's type library. Using the Object Browser is a quick way to determine what is in the type library because the Object Browser actually inspects the component's type library to populate what you see in the Object Browser, as long as you are viewing the component's contents from a separate project (if you view the component from its own VB6 project, it will display public and private data structures, i.e. everything that is visible in the IDE).
Is this the sort of thing you're looking for:
Visual Basic: Inspect COM Components Using the TypeLib Information Object Library
It's discussed on this thread here on SO: Self Inspection of VB6 UDTs
I've never tried this stuff myself.

Resources