Welcome to MSDN Blogs Sign in | Join | Help

Syndication

WCF security
Technorati Tags:

So, I have had a lot of questions on how to use WCF stack on NETCF and what the supported modes are for security and so forth. Really glad to say that Michele has written a cool guide for WCF mobile. This is based on NETCF 3.5. Here is a link to it: http://wcfguidanceformobile.codeplex.com/

Posted Tuesday, June 16, 2009 8:22 AM by mahathi | 0 Comments

Finalizers and ordering

There was recently a good post by Abhinaba about finalizers and threads Finalizers and Thread local storage. One of the things mentioned in MSDN is this:  “Finalizers can be executed in any order”. The consequence of this is not too obvious.

Consider the case below:

 

class MyClass
{
    MyClass2 myClass2; //Member.
    /// <summary>
    /// Finalizer method
    /// </summary>
    ~MyClass()
    {       myClass2.Cleanup(); //This is incorrect/
    }

}
MyClass myClass = new MyClass();

Although it seems like an object of MyClass (myClass) holds a reference to myClass2, and hence it should be accessible from the finalizer, the truth is that, given the fact that the myClass object is being finalized because it has no references pointing to it (it is garbage), this could very well mean that the myClass2 object maynot have any references either (if myClass is the only one that holds a reference to it). Hence when myClass becomes garbage, GC is free to cleanup and call finalizers in any order it wants. So myClass2 might not even exist at the point of myClass’s finalizer being executed.

The golden rule here is to remember this: No managed object access should be done in Finalizers.

Posted Monday, May 18, 2009 11:57 AM by mahathi | 1 Comments

Using WCF NetcfSvcUtil.exe: Setting value types to be sent from WCF mobile client

If you had a desktop client that worked with the desktop’s AddServiceReference, and now wanted to use the same code on a mobile client – generating the proxy with a NetcfSvcUtil.exe tool, there is a gotcha that I recently got a customer query about. If you tried to send out an object which had value types like int and double as data members, the server might behave as if the values were not sent. The reason is because of the differences in the proxy stub data contract generated by the NetcfSvcUtil.exe.

Lets say, the original datacontract of the class on the server specifies:

public class MyCustomData
    {
        int intValue = 10;

        [DataMember]
        public int IntValue
        {
            get { return intValue; }
            set { intValue = value; }
        }
    }

The NetcfSvcUtil.exe though generates a class of the following format:

public class MyCustomData {

[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public int IntValue
{
    get
    {
        return this.intValueField;
    }
    set
    {
        this.intValueField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IntValueSpecified
{
    get
    {
        return this.intValueFieldSpecified;
    }
    set
    {
        this.intValueFieldSpecified = value;
    }
} }

Now, notice the fact that both IntValue and IntValueSpecified are defined on the client side, and both need to be specified for the proxy to generate the xml necessary for the IntValue data in the request to be sent to the server.

So, unlike a desktop client, that just writes to the IntValue property, to achieve the right results, you need to do the following:

MyCustomData data = new MyCustomData();

data.IntValue = 100;

data.IntValueSpecified = true;

And now call into the proxy methods.

Posted Monday, April 27, 2009 7:21 AM by mahathi | 1 Comments

Filed under:

Adding Custom headers to WCF messages on NETCF

I recently got a question from a customer on how one can add custom headers to NETCF app. There is a lot of documentation on how to do this using OperationContextScope and IDispatchMessageInspector on the desktop side, but neither is supported on CF 3.5 itself.

The trick on NETCF is to use the Message class, and the Message.Headers.Add() method to add new custom headers to the outgoing message.

Below is an outline of the steps that you could use in conjunction with NetcfSvcUtil.exe that generates a proxy to talk to the service.

  1. Point netcfsvcutil.exe to the service address (http://localhost/service1.svc), and generate the proxy classes – CFClientBase.cs and Service1.cs
  2. Inside CFClientBase.cs, you see an implementation of the Request/Response handling and a CFContractSerializer implementation (CFContractSerializer inherits from XmlObjectSerializer, which is needed to serialize any data using WCF).
  3. You need to make 2 modifications now:
    • Change the CFContractSerializer (the serializer generated by NETCFSvcUtil), to move functionality from WriteObject to WriteObjectContent. The generated one throws an exception when WriteObjectContent was called.

          public override void WriteObject(System.Xml.XmlDictionaryWriter writer, object graph)
           {
               if (this.info.IsWrapped)
               {
                   this.serializer.Serialize(writer, graph);
               }
               else
               {
                   this.WriteObjectContent(writer, graph);
               }
           }

           public override void WriteObjectContent(System.Xml.XmlDictionaryWriter writer, object graph)
           {
                   System.IO.MemoryStream ms = new System.IO.MemoryStream();
                   System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
                   settings.OmitXmlDeclaration = true;
                   System.Xml.XmlWriter innerWriter = System.Xml.XmlDictionaryWriter.Create(ms, settings);
                   this.serializer.Serialize(innerWriter, graph);
                   innerWriter.Close();
                   ms.Position = 0;
                   System.Xml.XmlReader innerReader = System.Xml.XmlDictionaryReader.Create(ms);
                   innerReader.Read();
                   writer.WriteAttributes(innerReader, false);
                   if ((innerReader.IsEmptyElement == false))
                   {
                       innerReader.Read();
                       for (
                       ; ((innerReader.NodeType == System.Xml.XmlNodeType.EndElement)
                                   == false);
                       )
                       {
                           writer.WriteNode(innerReader, false);
                       }
                   }
                   innerReader.Close();
               }

    • In the Invoke<> function that invokes the method call functionality, added a new CFContractSerializerInfo for the type of “custom header” class (here I took a simple string, you can have a class instead), and create a new header from it.

              protected TRESPONSE Invoke<TREQUEST, TRESPONSE>(CFInvokeInfo info, TREQUEST request)

             {                …

               CFContractSerializerInfo customHeaderSerializerInfo = new CFContractSerializerInfo();
               customHeaderSerializerInfo .MessageContractType = typeof( string );
              customHeaderSerializerInfo .IsWrapped = info.RequestIsWrapped;
              customHeaderSerializerInfo .ExtraTypes = info.ExtraTypes;
              customHeaderSerializerInfo .UseEncoded = info.UseEncoded;
              msg.Headers.Add( System.ServiceModel.Channels.MessageHeader.CreateHeader( "myHeader", "
http://tempuri.org", "myData", this.GetContractSerializer( customHeaderSerializerInfo ) ) );

               return this.getResult<TRESPONSE>(this.getReply(msg), info);

              }

Posted Monday, April 20, 2009 6:21 AM by mahathi | 1 Comments

Filed under:

Installing NETCF 3.5 in CE 6.0 image

As I embark on my blogging journey, I hope to have fun sharing my experiences with NETCF - I currently work as a developer with the team.

I was recently trying to create a CE image with NETCF 3.5 built into the image, but realised that the instructions weren't too straight forward. For my first post, I thought I should give some tips on how to install NETCF 3.5 into a WinCE 6.0 image using platform builder. The default option allows you to use NETCF 2.0, so you need to go through a few steps to get there.

Prerequisites:

  1. Install VS 2005. http://www.microsoft.com/downloads/details.aspx?familyid=bb4a75ab-e2d4-4c96-b39d-37baf6b5b1dc&displaylang=en
  2. Install PB 6.0. This downloads a CE 6.0 OS image onto your machine. http://www.microsoft.com/downloads/details.aspx?FamilyId=BF0DC0E3-8575-4860-A8E3-290ADF242678&displaylang=en
  3. Install CE 6.0 R2 update from: http://www.microsoft.com/downloads/details.aspx?FamilyID=f41fc7c1-f0f4-4fd6-9366-b61e0ab59565&displaylang=en
  4. Although NETCF 3.5 was available starting Jan 2008 on CE 6.0, you might need to download the entire 2008 rollup: http://www.microsoft.com/downloads/details.aspx?FamilyID=b478949e-d020-465e-b451-73127b30b79f&DisplayLang=en as I was unable to find the monthly update for Jan 2008 currently on the download site.
  5. Now you should find NETCF 3.5 folder in your WinCE 6.0 image folder c:\wince600\others\dotnetcfv35

Creating an OS image:

  1. Using PB, create a new OS design. By default this allows you to pick only NETCFv2. Pick this up.
  2. After the project is created, open the View->Other Windows->Catalog Items Window. Here under --- Core OS -> CEBASE -> Applications and Services Development, find the NET Compact Framework 3.5 option. Uncheck NETCF 2.0 and check NETCF 3.5 option.
  3. Clean build your OS image.
  4. Double check in the final build image folder if you see the mscoree3_5.dll placed along with the NK.bin

You now have a CE 6.0 image with NETCF 3.5 on it :).

Note:

For WM you can install the NETCF 3.5 cab from: http://www.microsoft.com/downloads/details.aspx?FamilyID=E3821449-3C6B-42F1-9FD9-0041345B3385&displaylang=en

Posted Sunday, April 05, 2009 9:21 AM by mahathi | 1 Comments

Filed under:

Page view tracker