Converting UTF8 Encoded String to Bytes[]

I think every developer faced this problem of converting Unicode encoded string to Bytes[] . But .Net Framework has a very rich support for converting Encoded String to Bytes[].  .Net Framework support following 4 Encoding which is inherited from Base class Encoding (For Details – http://msdn2.microsoft.com/en-us/library/system.text.encoding(VS.71).aspx )

1.       System.Text.ASCIIEncoding – Encodes characters as 7Bit ASCII character

2.       System.Text.UnicodeEncoding – Encode characters in 2 consecutive Bytes enabling support for Big Endean or Little Endean.

3.       System.Text.UTF7Encoding – Encode characters in UTF7

4.       System.Text.UTF8Encoding-– Encode characters in UTF8

Now, let’s consider our text to be UTF8 encoded like

StringBuilder
_TextBuilder = new StringBuilder(223);
           
_TextBuilder.AppendFormat(@”<?xml version=””1.0″” encoding=””UTF-8″”?>{0}”, Environment.NewLine);
            _TextBuilder.AppendFormat(@”<Contents Type=””string””><![CDATA[• das sfas fdasfs afdasfasd fasd hg kjh klhhjn “);           
_TextBuilder.AppendFormat(@”fdhæfdhj fdh.lfjnhfjk.lnh fdæjlf hlæfjhnf læhfj hglæjælfdh{0}”, Environment.NewLine);
           
_TextBuilder.AppendFormat(@”hfdj  fdklhjfdh]]></Contents>”);
string pdfDirectorXml = _TextBuilder.ToString();

The String pdfDirectorXml contains some unicode charecters with classic Bell Charecter also.The easiest way to convert UTF8 encoded text to Binary –

      UTF8Encoding
encoding = new UTF8Encoding();
      byte[] bytes = encoding.GetBytes(pdfDirectorXml);

Hence, you can get the UTF-8 encoded byte[] representation of the string. J

New beta exam on Windows Communication Foundation and .Net3.5 – Completely Free

Microsoft just launched a new exam on the next generation Web Services with state of art technology: Windows Communication Foundation – Beta Exam 70-503 : TS: Microsoft.NET Framework3.5 – Windows Communication Foundation.

For details: http://www.microsoft.com/learning/exams/70-503.mspx.
By completing this exam, credit will be counted towards certification: MCTS: Microsoft .NET framework3.5 – Windows Communication Foundation.

The free registration already began on : November 14,2007 and beta exam period will be running during November 19, 2007 to December 14, 2007. You can get registered by contacting: http://www.register.prometric.com/ClientInformation.asp.

C# Null Coalescing Operator

Few new operators have been introduced in C#2.0. Null Coalescing operator, which is one of them, is discussed in this post. While coding, we frequently need to perform null checks as follows–

Contact contact = provider.GetContact(contactId);
if (contact == null)
{
   contact = new Contact();
}

Instead of using if block we could just write it –

Contact contact = provider.GetContact(contactId);
contact = contact ?? new Contact();

It is pretty useful in the context of nullable types. It can also be handy while converting nullable type to value type, as shown below–

int? nullableInt = null;
int valueInt = nullableInt ?? default(int);

The valueInt become value type after executing this statement. But if we write something like this–

int? nullableIntAgain = nullableInt ?? 5;

the int literal 5 is automatically converted to a nullable type by CLR and afterwards, being assigned to nullableIntAgain. Subsequently, we can assign it to null now–

nullableIntAgain = null;

Isn’t pretty slick and handy? However, when using this operator we need to keep in mind that–

glyphicons Null coalescing operator can only be used in nullable or reference type. Otherwise it will raise an InvalidOperationException.

glyphicons We need to consider thread-safety also. Otherwise it could end up in race condition.

Hope it helps. Thanks!

Revisions

[R-1: 29-03-2013] Updated formatting to make this post more consistent with current CSS.

Making [Web Method] Asynchronous using “OneWay “

By default Web methods called are synchronous like HTTP request and response architecture and rely on immediate response. But sometimes asynchronous operation might be the best suited for a particular operation. .Net Framework has good support for asynchronous operation. Simplest and easiest way to make a web method asynchronous is by using OneWay property, which we discuss in this post.

OneWay property is a part of SoapRpcMethod attribute. If we set its value to true – then client does not need to wait for the web service to finish the operation of the web method and returns immediately with Http Status code 202, which indicates that service started its operation and client does not need to come back to get the status. Example of an asynchronous web method using OneWay

[WebMethod]
[SoapRpcMethod(OneWay = true)]
public void StartLongRunningAsynchronousProcess() {
// start asynchronous process + client need not to wait for the response
}
view raw gistfile1.cs hosted with ❤ by GitHub

Return type of this type of web method should be nothing other than void. If we set the return type something else – then though it will compile – it will generate exception while it will be accessed.If we look at the WSDL for this service – we will find that there is no <output> element either in <portType> or <binding> elements of StartLongRunningAsyncronousProcess method.

We could see by looking at the <portType> element of the service:

<portType name="ServiceNameSoap">
<operation name= "StartLongRunningAsynchronousProcess">
<input message="s0: StartLongRunningAsynchronousProcessSoapIn"/>
</operation>
</portType>
view raw gistfile1.xml hosted with ❤ by GitHub

Execution status of a Workflow Instance

We can determine the Execution status of an workflow instance using the workflow definition.. Workflow definition of an workflow instance provice the current execution status. Base class Activity contain one field named – ExecutionStatus which is a member of ActivityExecutionStatus Enum. ActivityExecutionEnum contain following values –

  • Initialized
  • Cancelling
  • Closed
  • Executing
  • Faulting
  • Compensating

Example –

Console.WriteLine( "Workflow Execution status :{0} ", instance.GetWorkflowDefinition().ExecutionStatus.ToString());