wcf应用程序之间的全双工通信

时间:2017-08-23 14:16:00

标签: c# wcf

为了概念验证,我正在尝试编写两个应用程序WcfApp1和WcfApp2。

问题: 我能够在WcfApp2解决方案中添加WcfApp1的服务引用,但无法在WcfApp1中添加WcfApp2。虽然代码相同(当然正在改变basic_httpbinding_url)。 谁能告诉我这可能有什么问题?

详情:

WcfApp1有:

IWcfApp1_Class - >接口;

WcfApp1_Class - >从上面的接口派生的类;

WcfApp1_DClass - > class(作为数据结构);

WcfApp2有:

IWcfApp2_Class - >接口;

WcfApp2_Class - >从上面的接口派生的类;

WcfApp2_DClass - > class(作为数据结构);

WcfApp1代码:

Uri baseAddress_BasicHttpBinding = new Uri("http://localhost:9001/WcfApp1");
Uri baseAddress_NetTcpBinding = new Uri("net.tcp://localhost:9001/WcfApp1");
Uri baseAddress_NetNamedPipeBinding = new Uri("http://localhost:9001/WcfApp1");// new Uri("net.pipe://localhost:9001/WcfApp1");
Uri baseAddress_WSHttpBinding = new Uri("http://localhost:9001/WcfApp1");
Uri baseAddress_WSDualHttpBinding = new Uri("http://localhost:9001/WcfApp1");

ServiceHost host;

public void f_BasicHttpBinding()
{

    host = new ServiceHost(typeof(WcfApp1_Class), baseAddress_BasicHttpBinding);
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

    var behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
    behavior.InstanceContextMode = InstanceContextMode.Single;

    host.Description.Behaviors.Add(smb);

    try
    {
        host.Open();
    }
    catch (Exception exxx)
    {
        Console.WriteLine(exxx.ToString());
    }
}

IWcfApp1_Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using System.ServiceModel.Description;

using System.Runtime.Serialization;

namespace WcfApp1
{
    [ServiceContract]
    interface IWcfApp1_Class
    {
        [OperationContract]
        void Function1();

        [OperationContract]
        int Function2();

        [OperationContract]
        string Function3();

        [OperationContract]
        WcfApp1_DClass Function4();

        [OperationContract]
        void Function5(WcfApp1_DClass data);

        [OperationContract]
        WcfApp1_DClass Function6(WcfApp1_DClass data);
    }

}

WcfApp1_Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WcfApp1
{
    class WcfApp1_Class :
        IWcfApp1_Class
    {
        public void Function1()
        {
            Console.WriteLine("Function1 executed...");
        }

        public int Function2()
        {
            Console.WriteLine("Function2 executed...");
            return 1;
        }

        public string Function3()
        {
            Console.WriteLine("Function3 executed...");
            return "WcfApp1_Class->Function3";
        }

        public WcfApp1_DClass Function4()
        {
            WcfApp1_DClass data = new WcfApp1_DClass
            {
                student_Id = 1,
                student_Name = "WcfApp1_DClass"
            };
            Console.WriteLine("Function4 executed...");
            return data;
        }

        public void Function5(WcfApp1_DClass data)
        {
            if(data.student_Id == 2 && data.student_Name == "WcfApp2_DClass")
            {
                Console.WriteLine("Function5 executed... OK...");
                return;
            }
            Console.WriteLine("Function5 executed... NOT OK...");
            return;
        }

        public WcfApp1_DClass Function6(WcfApp1_DClass data)
        {
            if (data.student_Id == 2 && data.student_Name == "WcfApp2_DClass")
            {
                Console.WriteLine("Function6 executed... OK...");
                data.student_Name = "WcfApp1_DClass";
                return data;
            }
            Console.WriteLine("Function6 executed... NOT OK...");
            return data;
        }
    }
}

WcfApp1_DClass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using System.ServiceModel.Description;

using System.Runtime.Serialization;

namespace WcfApp1
{
    [DataContract]
    class WcfApp1_DClass
    {
        private int _student_Id;
        private string _student_Name;

        [DataMember]
        public int student_Id
        {
            get
            {
                return _student_Id;
            }
            set
            {
                if(_student_Id != value)
                {
                    _student_Id = value;
                }
            }
        }

        [DataMember]
        public string student_Name
        {
            get
            {
                return _student_Name;
            }
            set
            {
                if (_student_Name != value)
                {
                    _student_Name = value;
                }
            }
        }

    }
}

WcfApp2代码:

Uri baseAddress_BasicHttpBinding = new Uri("http://localhost:9002/WcfApp2");
        Uri baseAddress_NetTcpBinding = new Uri("net.tcp://localhost:9002/WcfApp2");
        Uri baseAddress_NetNamedPipeBinding = new Uri("http://localhost:9002/WcfApp2");// new Uri("net.pipe://localhost:9002/WcfApp2");
        Uri baseAddress_WSHttpBinding = new Uri("http://localhost:9002/WcfApp2");
        Uri baseAddress_WSDualHttpBinding = new Uri("http://localhost:9002/WcfApp2");

        ServiceHost host;

        public void f_BasicHttpBinding()
        {

            host = new ServiceHost(typeof(WcfApp2_Class), baseAddress_BasicHttpBinding);
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

            var behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
            behavior.InstanceContextMode = InstanceContextMode.Single;

            host.Description.Behaviors.Add(smb);

            try
            {
                host.Open();
            }
            catch (Exception exxx)
            {
                Console.WriteLine(exxx.ToString());
            }
        }

IWcfApp2_Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using System.ServiceModel.Description;

namespace WcfApp2
{
    interface IWcfApp2_Class
    {
        [OperationContract]
        void Function1();

        [OperationContract]
        int Function2();

        [OperationContract]
        string Function3();

        [OperationContract]
        WcfApp2_DClass Function4();

        [OperationContract]
        void Function5(WcfApp2_DClass data);

        [OperationContract]
        WcfApp2_DClass Function6(WcfApp2_DClass data);
    }
}

WcfApp2_Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WcfApp2
{
    class WcfApp2_Class :
        IWcfApp2_Class
    {
        public void Function1()
        {
            Console.WriteLine("Function1 executed...");
        }

        public int Function2()
        {
            Console.WriteLine("Function2 executed...");
            return 1;
        }

        public string Function3()
        {
            Console.WriteLine("Function3 executed...");
            return "WcfApp1_Class->Function3";
        }

        public WcfApp2_DClass Function4()
        {
            WcfApp2_DClass data = new WcfApp2_DClass
            {
                student_Id = 1,
                student_Name = "WcfApp1_DClass"
            };
            Console.WriteLine("Function4 executed...");
            return data;
        }

        public void Function5(WcfApp2_DClass data)
        {
            if (data.student_Id == 2 && data.student_Name == "WcfApp2_DClass")
            {
                Console.WriteLine("Function5 executed... OK...");
                return;
            }
            Console.WriteLine("Function5 executed... NOT OK...");
            return;
        }

        public WcfApp2_DClass Function6(WcfApp2_DClass data)
        {
            if (data.student_Id == 2 && data.student_Name == "WcfApp1_DClass")
            {
                Console.WriteLine("Function6 executed... OK...");
                data.student_Name = "WcfApp2_DClass";
                return data;
            }
            Console.WriteLine("Function6 executed... NOT OK...");
            return data;
        }
    }
}

WcfApp2_DClass:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using System.ServiceModel.Description;

using System.Runtime.Serialization;

namespace WcfApp2
{
    [DataContract]
    class WcfApp2_DClass
    {
        private int _student_Id;
        private string _student_Name;

        [DataMember]
        public int student_Id
        {
            get
            {
                return _student_Id;
            }
            set
            {
                if (_student_Id != value)
                {
                    _student_Id = value;
                }
            }
        }

        [DataMember]
        public string student_Name
        {
            get
            {
                return _student_Name;
            }
            set
            {
                if (_student_Name != value)
                {
                    _student_Name = value;
                }
            }
        }
    }
}

错误消息:

  

下载时出错   'http://localhost:9002/WcfApp2/ $的元数据'。请求失败了   HTTP状态405:不允许的方法。元数据包含引用   无法解决的问题:“http://localhost:9002/WcfApp2”。没有   在http://localhost:9002/WcfApp2听取的端点可以接受   消息。这通常是由不正确的地址或SOAP引起的   行动。有关更多详细信息,请参阅InnerException(如果存在)。遥控器   服务器返回错误:(404)未找到。如果定义了服务   在当前的解决方案中,尝试构建解决方案并添加   服务参考。

enter image description here

1 个答案:

答案 0 :(得分:0)

找到麻烦点。

我在[ServiceContract]界面中遗漏了IWcfApp2_Class