WCF学习笔记之ChannelFactory

WCF学习笔记之ChannelFactory

通信到底是怎么发生的?简单说就是两个终结点一个通道,实际上客户端也是有一个终结点的,客户端会在这两个终结点之间建立一个通道,然后把对服务端服务的调用封装成消息沿通道送出,服务器端获得消息后在服务器端建立服务对象,然后执行操作,将返回值再封装成消息发给客户端。

过程大概是这样的,有些地方可能不太严谨,但是这个逻辑我们是可以理解的。如此看来,通讯的工作主要部分都在客户端这边,他要建立通道、发送消息,服务端基本上在等待请求。

还是接着上面的那个例子滴呀;

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace WCF_ChannelFactory
{
    //首先你要写一个同样的接口方法滴呀;
    [ServiceContract]
    public interface IUser
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        complex GetSome(complex com);
    }
   
    [DataContract]
    public class complex
    {
        bool boolValue = true;
        string str = "Hello";
       [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }
        [DataMember]
        public string StringValue
        {
            get { return str; }
            set { str = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            //我们只有接着来试一试的啦;

            //当程序中出现序列化参数的时候,那还是一个麻烦的事情滴呀;

            EndpointAddress address = new EndpointAddress("http://localhost/WcfService1/UserService.svc");
            WSHttpBinding binding = new WSHttpBinding();

            ChannelFactory<IWcfChannel> factory = new ChannelFactory<IWcfChannel>(binding, address);
            IWcfChannel channel = factory.CreateChannel();
            string result = channel.GetData(110);  //尼玛,为什么,这里会报错呢
            channel.Close();
            Console.WriteLine(result);
            Console.ReadLine();

            
        }
    }

    public interface IWcfChannel : IUser, IClientChannel
    {

    }
}

会报错?

操尼玛~

debug中......

原文地址:https://www.cnblogs.com/mc67/p/5084778.html