【问题标题】:Why Serialization performed when using LocalChannel in WCF?为什么在 WCF 中使用 LocalChannel 时执行序列化?
【发布时间】:2012-07-24 11:22:35
【问题描述】:

Microsoft 提供了一个名为 LocalChannel 的 WCF 示例,用于展示如何实现自定义绑定以绕过在同一 ApplicationDomain 中调用服务时的不必要开销。它的示例描述表明:

这对于客户端和服务在同一个应用程序域中运行并且必须避免典型 WCF 通道堆栈(消息的序列化和反序列化)的开销的情况很有用。

我在我的项目中使用了此代码,但尽管声称它似乎在调用服务时发生了序列化。

为了更清楚,我已将代码更改为以下代码以使用数据协定,因此可以轻松确定是否正在执行序列化。

# 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();
    }
}

运行此代码表明“ProductDTO”类的“Price”属性正在通过本地绑定调用期间被序列化和反序列化!

有没有人用过这个方法或者知道有什么问题?

【问题讨论】:

    标签: c# .net wcf wcf-binding


    【解决方案1】:

    通过添加回调,我可以确认确实发生了序列化/反序列化:

    [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");
    }
    

    这向我暗示了几种可能性之一:

    • 他们打算保留您通常在 WCF 中看到的复制语义,客户端和服务器查看不同的副本
    • 它根本不起作用
    • 文档有误

    鉴于声明 here 说:

    这对于客户端和服务在同一个应用程序域中运行并且必须避免典型 WCF 通道堆栈(消息的序列化和反序列化)的开销的情况很有用。

    我宁愿怀疑中间那个,即编写示例的人假设它正在工作,而没有实际检查是否正在发生序列化。

    但是!也有可能是打算删掉栈的某些部分(主要是IO栈),但是写文档页的人误解了,错误地说序列化也被省略了。 p>

    【讨论】:

    • 感谢您的快速回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多