ResolveUrl() from WCF service

I wanted to ResolveUrl() from WCF service and found

http://www.west-wind.com/weblog/posts/2007/Sep/18/ResolveUrl-without-Page .
However the function assumes that the call is synchronous, in asynchronous call (e.g called from TPL task) HttpContext.Current==null.
I had to split my asynchronous method into two-a long asynchronous one, invoked as task and generating relative URL and a post-task, that is calling wwWebUtils.ResolveServerUrl(relativeUrL)

The http://www.codeproject.com/Articles/205425/ASP-NET-ResolveUrl-Without-Page  article suggests to use
System.Web.VirtualPathUtility.ToAbsolute(“~/default.aspx”);
but i expect, it wouldn’t work from asynchronous thread as well.

Advertisements

DataContractSerializer truncated string when used with MemoryStream,but works with StringWriter

We’ve used the following DataContractSerializeToXml method for a long time, but recently noticed, that it doesn’t return full XML for a long object, but  truncated it and returns XML string with the length of  multiple-of-1024 , but the reminder is not included.


 internal static string DataContractSerializeToXml<T>(T obj)
{
string strXml =
“”;
Type type= obj.GetType();//typeof(T)
DataContractSerializer serializer = new DataContractSerializer(type);
System.IO.
MemoryStream aMemStr = new System.IO.MemoryStream();
System.Xml.
XmlTextWriter writer = new System.Xml.XmlTextWriter(aMemStr, null);
serializer.WriteObject(writer, obj);
strXml = System.Text.
Encoding.UTF8.GetString(aMemStr.ToArray());
return strXml;


  
I tried to debug and searched Google for similar problems, but didn’t find explanation of the error. The most closed http://forums.codeguru.com/showthread.php?309479-MemoryStream-allocates-size-multiple-of-1024-( talking about incorrect length, but not about truncated string.

fortunately replacing MemoryStream to StringWriter according to

http://billrob.com/archive/2010/02/09/datacontractserializer-converting-objects-to-xml-string.aspx
fixed the issue.

  1: var serializer = new DataContractSerializer(tempData.GetType());
  2: using (var backing = new System.IO.StringWriter())
  3: using (var writer = new System.Xml.XmlTextWriter(backing))
  4: {
  5:     serializer.WriteObject(writer, tempData);
  6:     data.XmlData = backing.ToString();
  7: }

v

Convert collections of enums to collection of strings and vice versa

Recently I needed to convert collections of  strings, that represent enum names, to collection of enums, and opposite,  to convert collections of   enums  to collection of  
strings. I didn’t find standard LINQ extensions.
However, in our big collection of helper extensions I found what I needed – just with different names:
/// <summary>
/// Safe conversion, ignore any unexpected strings

/// Consider to name as Convert extension
/// </summary>
/// <typeparam name=”EnumType”></typeparam>
/// <param name=”stringsList”></param>
/// <returns></returns>
public static
List<EnumType> StringsListAsEnumList<EnumType>(this List<string> stringsList) where EnumType : struct, IComparable, IConvertible, IFormattable
    {
List<EnumType> enumsList = new List<EnumType>();
foreach (string sProvider in stringsList)
    {
    EnumType provider;
    if (
EnumHelper.TryParse<EnumType>(sProvider, out provider))
    {
    enumsList.Add(provider);
    }
    }
    return enumsList;
    }


/// <summary>
/// Convert each element of collection to string
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”objects”></param>
/// <returns></returns>
public static
IEnumerable<string> ToStrings<T>(this IEnumerable<T> objects)
{//from http://www.c-sharpcorner.com/Blogs/997/using-linq-to-convert-an-array-from-one-type-to-another.aspx
return objects.Select(en => en.ToString());
}