Archive
Stack overflow error while disposing object – C#
I have a class “ABC” which implements “IDisposable” interface and having below Dispose method.
Public Class ABC : IDisposable {
public void Dispose() {
this.Dispose(true);
}
}
When I run the application I was getting “Stack Overflow” error because the Dispose() method getting called recursively and went in to an infinite loop.
Fix :
- Prevent the Dispose() recursive call by using flag and the logic is as below
private bool disposed = false;
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (this.disposed) {
return;
}
this.disposed = true;
}
🙂
Make your WCF service’s Operation/Method Fire and Forget
There are cases when an operation has no returned values and the client does not care about the success or failure of the invocation.
To support this sort of fire-and-forget invocation, WCF offers one-way operations.
It’s simple to implement. We just need to add “IsOneWay” property to ‘true’ to the [OperationContract] property.
[ServiceContract]
interface IWCFContract
{
[OperationContract(IsOneWay = true)]
void HelloWorld()
}
Note : The Operation Contract must have a “void” return type without any outgoing parameters.
Refer this article for more.
🙂
HTTP Error 404.17 – hosting Service in IIS
Recently when I hosted a web service project in IIS and browse that, I got below error.
HTTP Error 404.17 – Not Found
The requested content appears to be script and will not be served by the static file handler.
Fix :
- In my case I missed a simple step, My Web service Application Pool set to .Net 2.0
- Since I built web application .Net 4.0 framework, changing Application Pool .Net Framework to v4.0 solved my problem
Note :
- Sometimes issue might caused by missing of ASP.Net framework.
- So install ASP.Net framework by running aspnet_regiis.exe –I command using Visual Studio Command Prompt.
🙂
Expecting element from namespace encountered with name namespace – Error DataContractSerializer
I have a REST enabled WCF service with Method GetFruits() which return list of Fruit objects serialized in XML format.
My ‘Fruit’ class defined as below
[DataContract]
public class Fruit {
string name = string.Empty;
[DataMember]
public string Name {
get { return name; }
set { name = value; }
}
}
I built a Client Web application and try to execute the ‘GetFruits’ and get the Fruit collection.
I got the result XML but when I try to DeSerialize XML to List<Fruit> I got below error
Reason –
- Namespace of ‘Fruit’ class at Service and Client are not matching
Fix –
- Add ‘Namespace’ tag to ‘Fruit’ class in both Client & Server.
[DataContract(Namespace = “ABC”)]
public class Fruit
{}
Check Entities attribute type – CRM Late Binding
Recently somebody posted a question in my blog, whether there is a way to determine the entities ‘Attribute Type’ instead of making Organization service call and read the metadata.
Assume you have a Plug-in you made a ‘Retrieve’ call using Late Binding and, you got Entity object.
Below is easy way to determine type of each attribute using ‘is’ operator.
Entity.Attributes[attributeName] is EntityReference
Or
Entity.Attributes[attributeName] is Money
Or
Entity.Attributes[attributeName] is OptionSetValue
🙂