Почему сериализация выполняется при использовании LocalChannel в WCF?

Существует образец WCF с именем LocalChannel , предоставленный Microsoft, чтобы показать, как можно реализовать пользовательскую привязку для обхода ненужных накладных расходов при вызове службы в том же ApplicationDomain. В описании образца указано, что:

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

Я использовал этот код в своем проекте, но, несмотря на утверждение, кажется, что сериализация происходит при вызове службы.

Чтобы сделать его более понятным, я изменил код на следующий, чтобы использовать контракт данных:поэтому можно легко определить, выполняется ли сериализация или нет.

# 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(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  to terminate...");
        Console.ReadLine();
    }
}

Запуск этого кода указывает, что свойство «Цена» класса «ProductDTO» сериализуется и десериализуется во время вызовов через локальную привязку!

Кто-нибудь использовал этот метод раньше или знает, если что-то не так?

5
задан Gholamreza 24 July 2012 в 11:22
поделиться