Calling Method in .Net CF - compact-framework

I want to perform following operation in .NET Compact Framework. I am looking for Calling Method type.
http://www.csharp-examples.net/reflection-calling-method-name/
.NET Compact Framework doesn't support StackFrame class. Also, GetCurrentMethod() is not available in MethodBase class.

Try throwing an exception then check it's StackTrace property.
try
{
throw new Exception();
}
catch(Exception ex)
{
Console.Write(ex.StackTrace);
}
It will give something like
"at
NumericTextBoxControl.NumericalInput..ctor()\r\nat
Custom_Numeric_Input.frmTestApplication.InitializeComponent()\r\nat
Custom_Numeric_Input.frmTestApplication..ctor()\r\nat
Custom_Numeric_Input.Program.Main()\r\n"
which you can parse, split and use reflection on.

As you're finding, the Compact Framework is just that - compact. Not everything from the full framework is available, and this is an example of that. There simply is no way to determine the calling method unless you're in an Exception handler and can look at the call stack there (and even then getting the method name isn't as straightforward as one would like).

Related

How to fix 'System.ServiceModel.Channels.ReceivedFault' cannot be serialized

I have a workflow service. I also use workflow persistence in that service. But after I deployed workflow in IIS, from client I make a request to workflow service, in log file on server. I see a message
The execution of the InstancePersistenceCommand named {urn:schemas-microsoft-com:System.Activities.Persistence/command}SaveWorkflow was interrupted by an error.InnerException Message: Type 'System.ServiceModel.Channels.ReceivedFault' cannot be serialized.
Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
I tried research about this exception, but I didn't find anything.
How to fix this problem ? or let me know what is the reason about above exception ?
System.ServiceModel.Channels.ReceivedFault is a non-serializable internal .NET framework class, so unfortunately there is nothing you can do to correct the actual root cause (i.e. making said class serializable).
You are probably calling an external service via WCF which faults, i.e. a System.ServiceModel.FaultException is thrown. IIRC, somewhere deep down in that FaultException object is a reference to the culprit, a ReceivedFault instance.
Solution: Catch all FaultExceptions, transfer the information you need to know from the FaultException into a serializable exception, and throw the latter:
try
{
CallService();
}
catch (FaultException ex)
{
// Gather all info you need from the FaultException
// and transport it in a serializable exception type.
// I'm just using Exception and the message as an example.
throw new Exception(ex.Message);
}

WCF + Enterprise Library + ValidationFault

I am trying to catch the ValidationFault exceptions of my service and return a instance of MyClass with the property Message filled with the validation error provided by EntLib when my client calls one of my service methods without the correct parameters (I can't use complex types).
I tried to implement two interfaces to accomplish this task: IParameterInspector and IOperationInvoker. The problem is after the method BeforeCall is called (of the IParameterInspector interface), EntLib throws the ValidationFault exception but I can't catch it and my code doesn't reach the Invoke method of my IOperationInvoker class and because of that I can't replace the return value with a instance of MyClass.
Remember, my client is not based on .NET platform and there's no such thing as catch(FaultException<ValidationFault> ex) there. That's why I MUST work with a default object on my service responses.
I appreciate the help.
Can you use an IErrorHandler implementation similar to Enterprise Library's WCF ExceptionShielding? Basically catch all exceptions and if it's a FaultException<ValidationFault> then convert the ValidationFault to your custom message and return it.
It looks like the output of ProvideFault is a Message so you could return a message instead of a fault. This posting seems to give the approach.

MSMQ, WCF and Enterprise Library 5 Logging application block

Background: I'm using WCF and MSMQ to execute long running jobs on a server. General error logging is done by Enterprise Library 5 Logging Application Block.
My problem is this:
private LogWriter _writer = EnterpriseLibraryContainer.Current.GetInstance() is always null once the ExecutingMethod() has been pickup up from the queue and starts executing, resulting in not logging possible errors.
public class SerializedClass
{
private LogWriter _writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
public void ExecutingMethod()
{
try
{
.....
}
catch(Exception ex)
{
_writer.Write(ex, Category.General, Priority.Highest);
}
}
}
I've already checked that the logging config is visible to the executing assembly. My assumption is that there might be a problem with the serialization of the 'SerializedClass'?
Any help or input would be appreciated?
The LogWriter class is explicitly not serializable. It's not obvious what serialization you're doing from the code. Actually, it's not obvious from the code you're serializing at all, so I'm guessing here.
Anyway, I'm not sure what serializer you're using, but whatever it is, LogWriter will NOT be going through it cleanly. The .NET serializers typically don't re-run the constructors for types, so for fields that don't deserialize cleanly you'd probably get null, like you are.
The workaround is easy - don't grab the LogWriter until you actually need it. Grab it in the catch block instead of storing it in the constructor. In this case, it will probably be easier to use the older static facades instead, and just call Logger.Write() instead of going through a specific LogWriter instance.
-Chris

WCF Custom WSDL XmlSerializerOperationBehavior

I have code that builds a custom WCF wsdl on the fly. In one particular scenario, the WSDL exported should use the XmlSerializerOperationBehavior as the IWsdlExportExtension as opposed to the default DataContractSerializerOperationBehavior IWsdlExportExtension.
However, every time I try this from the WSDL generation code, I get a null reference exception from the ExportBody method of the XmlSerializerMessageContractExporter (which is used internally in System.ServiceModel by the XmlSerializerOperationBehavior ExportContract method). I've reflector'd it and I can't see anything obviously wrong. For some reason, .NET also doesn't want to work with source stepping in this scenario...
Simply, the most basic way I can reproduce this is
var c = ContractDescription.GetContract(typeof(IMyService));
foreach (var op in c.Operations)
{
op.Behaviors.Remove(typeof(DataContractSerializerOperationBehavior));
op.Behaviors.Insert(0, new XmlSerializerOperationBehavior(op));
}
new WsdlExporter().ExportContract(c); // throws NullReferenceException
Does anyone have any ideas on this?
Thanks very much.
have you check c is not null?
The only other thing i can think of is this statement
new WsdlExporter().ExportContract(c);
maybe the compilier doesn't like it, try this instead
WsdlExporter wsdlImporter = new WsdlExporter()
wsdlImporter.ExportContract(c);
We've had scenarios in the past with wcf and chaining operations and the solution has been to unchain the calls.
I figured it out. The problem is that XmlSerializerOperationBehavior will throw a null reference exception if XmlSerializerFormatAttribute is not present on the ServiceContract interface itself. Had to reflector the whole thing to figure it out...

Hibernate session / transaction design

I'm trying to figure out how to best use Sessions in (N)Hibernate. I have a C# remoting object (MarshalByRefObject) which gets consumed by an ASP.NET client.
Currently my remoting class opens up one Session instance in the constructor and uses that one for all transactions. Is this a good idea? And would I need a finalizer for the remote object where session.Dipose() gets called?
Each client request opens a new transaction. Now my database access looks generally like this:
ITransaction transaction = this.session.BeginTransaction();
try {
// perfrom nhibernate query
transaction.Commit();
}
catch (Exception ex) {
transaction.Rollback();
this.session.Flush();
}
and then I often return the retrieved database object to the client.
Is this a valid way to handle this? Should I use the transaction object in a using block or call Dispose() on it? Is the session.Flush() needed after the rollback?
Also sometimes I have a problem when binding a returned collection to a GridView. It throws a exception stating that a binding property of a certain object is not valid. Is this related to hibernate returning proxy objects? And why are the collected objects by hibernate within one query a mixture of "real" and proxy objects?
Thanks!
See my answer here: NH Request Per Session - “Session is closed!”