Archive

Archive for the ‘Misc’ Category

HTTP Error 404.17 – hosting Service in IIS

September 6, 2014 Leave a comment

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.

HTTP Error 404.17

HTTP Error 404.17

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
Application Pool

Application Pool

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

September 2, 2014 Leave a comment

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

{}

XML to strongly typed object in C#

 

Assume you have composite XML and you want read the XML field values in your C# project, the simplest way is to convert in to Strongly type object and read as parameters.

It involves two steps as mentioned below


Generate XML Wrapper Class 

  • “Fruits.xml” is your XML file.
Composite XML

Composite XML

  • Copy the XML file content.
  • Open a new Class file “FruitsWrapper.cs” in Visual Studio 2012
  • Select “EDIT -> Paste Special -> Paste XML As Classes”
Paste Special XML As Class

Paste Special XML As Class

  • Now you get XML wrapper class on your Class file
Generated XML Wrapper Class

Generated XML Wrapper Class

  • Save the file

Convert XML to Class

  • To test the Conversion and read the XML data
  • Take a console application and paste below code

static void Main(string[] args){

const string fruitsXmlPath = @”c:\Fruits.xml”;

// Set your Root Element Name (i.e., <Fruits> is the root node of our XML)

var xRoot = new XmlRootAttribute { ElementName = “Fruits”, IsNullable = true };

var xmlSerializer = new XmlSerializer(typeof(Fruits), xRoot);

var fruits = (Fruits)xmlSerializer.Deserialize(new FileStream(fruitsXmlPath, FileMode.Open));

// Read XML node values as Object Properties

Console.WriteLine(“Fruit Name” + fruits.CitrusFruits.Fruit.Name);

Console.WriteLine(“Fruit Price” + fruits.CitrusFruits.Fruit.Price);

}


 

This approach is useful when you have your application’s configuration details provided in an XML file and if the file is complex and big.

Note – If you don’t have “Paste Special -> Paste XML As Classes” feature in your Visual Studio, you can use XSD.exe tool to generate wrapper class from XML.

Categories: Misc Tags: , , ,

Unable to uninstall windows service; Service status is disabled

I was trying to uninstall my windows service using cmd>installutil \u ‘sevice.exe’.

But the service is not uninstalling and showing status as ‘Disabled’ in ‘Service Manager’ (Run -> Services.msc).

Windows Service Status Disabled

Windows Service Status Disabled

Also when I try to install the same service again, I was getting “The specified service has been marked for deletion” message.

Fix

  • Make sure you close “Services Manager” (Services.msc) before Install or Uninstall your windows service.

🙂

Reflection with method overloading C#

July 16, 2014 1 comment

I have a class with 3 overloaded methods and when try to invoke method’s using Reflection, I was gettingAvoiding an ambiguous match exception” exception.

Below is the way to invoke overload methods using Reflection, which solved my exception.

Let’s take a class with 3 overload methods.

Class Structure

namespace MyNamespace{

public class CallMe{

public string MyName(){

return “You don’t have name”;

}

public string MyName(string firstName){

return “Your name is ” + firstName;

}

public string MyName(string firstName, string lastName){

return “Your name is ” + firstName + lastName;

}

}

Here is the way to invoke methods using Reflection

C# Code to call the Generic Method

// Load .dll

Assembly assembly = Assembly.LoadFile(“{Physical path of }MyNamespace.dll”);

// Set the Class type as “Namespace.Classname”

Type classType = assembly.GetType(“ReflectionClass.CallMe”);

// One of my methods expects 1 string hence creating MethodInfo object with 1 Type[] parameter

MethodInfo methodWithOneParameter = classType.GetMethod(“MyName”, new Type[] { typeof(string) });

// One of my methods expect 2 string parameters hence creating MethodInfo object with 2 Type[] parameters.

MethodInfo methodWithTwoParameter = classType.GetMethod(“MyName”, new Type[] { typeof(string), typeof(string) });

// To invoke overload with no parameters, provide an empty Type array to GetMethod’s second parameter

MethodInfo methodWithNoParameter = classType.GetMethod(“MyName”, new Type[0]);

// Invoke Methods

var resultMethodWithOneParameter = InvokeMethod(classType, methodWithOneParameter, new string[] { “Rajeev” });

var resultMethodWithTwoParameter = InvokeMethod(classType, methodWithTwoParameter, new string[] { “Rajeev”, “Pentyala” });

var resultMethodWithNoParameter = InvokeMethod(classType, methodWithNoParameter, null);

//  Display Results

Console.WriteLine(“ResultMethodWithOneParameter – ” + resultMethodWithOneParameter.ToString());

Console.WriteLine(“ResultMethodWithTwoParameter – ” + resultMethodWithTwoParameter.ToString());

Console.WriteLine(“ResultMethodWithNoParameter – ” + resultMethodWithNoParameter.ToString());

 C# Generic Method to Invoke methods

       // Generic method Invokes methods and return Result as Object

public static object InvokeMethod(Type classType, MethodInfo methodInfo, object[] parametersArray){

object result = null;

if (classType != null) {

if (methodInfo != null) {

ParameterInfo[] parameters = methodInfo.GetParameters();

object classInstance = Activator.CreateInstance(classType, null);

if (parameters.Length == 0) {

//This works fine

result = methodInfo.Invoke(classInstance, null);

}

else {

//The invoke does NOT work it throws “Object does not match target type”

result = methodInfo.Invoke(classInstance, parametersArray);

}

}

}

return result;

}

We get the response as below

Reflection Overload Result

Reflection Overload Result

🙂

Separating alphabets and digits from Alphanumeric string– C#

We got a requirement to separate alphabets and digits from alphanumeric string as groups.

Let’s say my alphanumeric string is “Hel00Wor11DD” and I need to get

  • Alphabet group as “Hel,Wor,DD
  •  Digit group as “00,11”.

Below is the C# code which use Regular expressions and achieve the same

var digitGroup = newList<string>();

var alphabetGroup = newList<string>();

Match regexMatch = null;

string myString = “Hel00Wor11DD”;

while (myString.Length > 0){

if ((regexMatch = Regex.Match(myString, “\\d”)).Success){

// If myString is not starting with digit

if (regexMatch.Index > 0) {

alphabetGroup.Add(myString.Substring(0, regexMatch.Index));

}

// If myString is starting with digits but has subsequent alphabets

elseif ((regexMatch = Regex.Match(myString, “\\D”)).Success) {

digitGroup.Add(myString.Substring(0, regexMatch.Index));

}

// If myString only has digits, no more alphabets

else{

digitGroup.Add(myString.Substring(0));

// No more alphabets

break;

}

myString = myString.Substring(regexMatch.Index);

}

// There are no digits in myString

else{

alphabetGroup.Add(myString);

// No more digits

break;

}

}

When you run above code, you would get Alphabets & Digits separated as Lists.

🙂

GUID generator website

How many times you were in a situation, where you need a GUID to test your .Net application or fill a ‘uniqueidentifier’ column of SQL table.

I found this GUID generator site very helpful.

You just need to refresh the web page to get a new GUID every time.

GUID Generator

GUID Generator

Not only this, this site will give you as many as GUID’s you want at a time and also has GUID validation option.

GUID Generator In 1000's

GUID Generator In 1000’s

Try it your self 🙂

Categories: Misc Tags: , ,

Can only generate one of classes or data sets – Error with Xsd.exe tool

I was getting below exception when I try to generate a class file from my XML schema using command (xsd “mySchema.xsd”)

XSD tool - Error

XSD tool – Error

Reason & Fix –

  • Reason is, Xsd tool unable to determine whether to generate Dataset or Class
  • Fix is, pass argument “/c” to generate Class or “/d” to generate Dataset
  • In my case I wanted to get a class so I prepared command (xsd “mySchema.xsd” /c)
XSD tool - generate class

XSD tool – generate class

🙂

How to debug XAML Workflows when invoked remotely

Assume you have a XAML workflow created using WF designer and you trigger this workflow from a console application which is in another server.

Below are the steps to debug

  • Install “Workflow Manager Tools”
  • After installation you can find a folder under “C:\Program Files (x86)\Workflow Manager Tools\1.0”
  • Open “Microsoft.Workflow.TestServiceHost.exe.config” using Visual Studio
  • Make sure all ‘connectionStrings’ pointing to proper Data base.
  • Open “Command Prompt” as Administrator.
  • Point to “C:\Program Files (x86)\Workflow Manager Tools\1.0”, Run the “Microsoft.Workflow.TestServiceHost.exe”
WF Test Service Host

WF Test Service Host

  • Keep the Command Prompt open (i.e., Minimize it)
  • Open the workflow you want debug using Visual Studio
  • Attach below Processes
Attach Processes

Attach Processes

  • Put a break point on the Workflow file
Break Point on WF file

Break Point on WF file

  • Invoke the Workflow from console and you get the breakpoint hit on Workflow.

🙂

Categories: Misc Tags: , ,

My blog report for the year 2013

December 31, 2013 Leave a comment

First of all thanks to all my blog visitors who posted valuable comments and suggestions.

I wish you all a great year ahead that starts with happiness and ends with that too. Happy New year 🙂

WordPress.com prepared a 2013 annual report for my blog.

Here’s an excerpt:

The Louvre Museum has 8.5 million visitors per year. This blog was viewed about 130,000 times in 2013. If it were an exhibit at the Louvre Museum, it would take about 6 days for that many people to see it.

Click here to see the complete report.

🙂

Categories: Misc