Why is serialization performed when using LocalChannel in WCF?

There is a WCF sample called LocalChannel provided by Microsoft to show how custom binding can be configured to bypass unnecessary overhead when calling a service on the same ApplicationDomain. The sample description states that:

This is useful for scenarios where the client and the service are running in the same application domain, and the overhead of a typical WCF channel stack (message serialization and deserialization) should be avoided.

I used this code in my project, but despite the statement, it seems that serialization happens when the service is called.

To make it more understandable, I changed the code to the next to use a data contract, so it’s easy to determine if serialization is in progress or not.

# region Service Contract

[ServiceContract]
public interface IGetPrice
{
    [OperationContract]
    ProductDTO GetPriceForProduct(int productCode);
}


[DataContract]
public class ProductDTO
{
    private string _price;

    public ProductDTO(string price)
    {
        _price = price;
    }

    #region Overrides of Object

    public override string ToString()
    {
        return string.Format("Price = '{0}'", _price);
    }

    #endregion

    [DataMember]
    public string Price
    {
        get { return _price; }
        set { _price = value; }
    }
}

public class GetPrice : IGetPrice
{
    #region IGetPrice Members

    public ProductDTO GetPriceForProduct(int productId)
    {
        return new ProductDTO((String.Format("The price of product Id {0} is ${1}.",
                                             productId, new Random().Next(50, 100))));
    }

    #endregion
}

# endregion



internal class Program
{
    private static void Main(string[] args)
    {
        var baseAddress = "net.local://localhost:8080/GetPriceService";

        // Start the service host
        var host = new ServiceHost(typeof (GetPrice), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof (IGetPrice), new LocalBinding(), "");
        host.Open();
        Console.WriteLine("In-process service is now running...\n");

        // Start the client
        var channelFactory
            = new ChannelFactory<IGetPrice>(new LocalBinding(), baseAddress);
        var proxy = channelFactory.CreateChannel();

        // Calling in-process service
        var priceForProduct = proxy.GetPriceForProduct(101);
        Console.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"
                          , 101, priceForProduct);
        Console.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"
                          , 202, proxy.GetPriceForProduct(202));
        Console.WriteLine("Calling in-process service to get the price of product Id {0}: \n\t {1}"
                          , 303, proxy.GetPriceForProduct(303));

        Console.WriteLine("\nPress <ENTER> to terminate...");
        Console.ReadLine();
    }
}

Running this code indicates that the "Price" property of the "ProductDTO" class is serialized and deserialized during calls through local binding!

Has anyone used this method before or knows something is wrong?

+5
source share
1 answer

By adding callbacks, I can confirm that indeed serialization / deserialization is definitely happening:

[OnSerializing]
internal void OnSerializing(StreamingContext context) {
    Console.WriteLine("OnSerializing");
}
[OnSerialized]
internal void OnSerialized(StreamingContext context) {
    Console.WriteLine("OnSerialized");
}
[OnDeserializing]
internal void OnDeserializing(StreamingContext context) {
    Console.WriteLine("OnDeserializing");
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext context) {
    Console.WriteLine("OnDeserialized");
}

This offers me one of several options:

  • they intend to preserve the copy semantics that you usually see in WCF, while the client and server are viewing different copies.
  • it just doesn't work
  • wrong documentation

, :

, , WCF ( ).

, , .. , , , , , .

! , ( , IO), , , , .

+6

All Articles