繁体   English   中英

Windows窗体应用程序调用WCF服务,而WCF服务调用另一个WCF服务

[英]windows form application calling a WCF service and the WCF service calling another WCF service

这是我为测试服务而建立的示例项目,该服务调用了另一个服务

这是其他服务调用的服务

namespace WCFPub
{
    [ServiceContract]
    public interface IStudent
    {
        [OperationContract]
        string getName(string name);
    }

}

namespace WCFPub
{
    public class Student : IStudent
    {
        public string getName(string name)
        {
            return "Your name is " + name;
        }
    }
}

承载上述服务的控制台应用程序

namespace WCFHost
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ServiceHost sh = new ServiceHost(typeof(WCFPub.Student));
                ServiceMetadataBehavior serviceMetadataBehaviour = new ServiceMetadataBehavior()
                {
                    HttpGetEnabled = true,

                };
                sh.Description.Behaviors.Add(serviceMetadataBehaviour);
                sh.AddServiceEndpoint(typeof(WCFPub.IStudent), new WSDualHttpBinding(), "PS");
                Console.WriteLine("Host Ready, Listening on 7060");
                Console.WriteLine("Hit Enter to Stop..");
                sh.Open();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}

调用第二项服务的服务

namespace WCFPub2
{
    [ServiceContract]
    public interface IMaster
    {
        [OperationContract]
        string getNameFromStudent(string name);
    }

}

namespace WCFPub2
{

    public class Master : IMaster
    {
        public string getNameFromStudent(string name)
        {
            Proxy2.StudentClient client = new Proxy2.StudentClient();
            return client.getName("ABdi");
        }
    }
}

托管上述服务的控制台应用程序

namespace WCFHost2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ServiceHost sh = new ServiceHost(typeof(WCFPub2.Master));
                ServiceMetadataBehavior serviceMetadataBehaviour = new ServiceMetadataBehavior()
                {
                    HttpGetEnabled = true,

                };
                sh.Description.Behaviors.Add(serviceMetadataBehaviour);
                sh.AddServiceEndpoint(typeof(WCFPub2.IMaster), new WSDualHttpBinding(), "PS");
                Console.WriteLine("Host Ready, Listening on 7061");
                Console.WriteLine("Hit Enter to Stop..");
                sh.Open();
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}

客户端

namespace WCFClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            Proxy.MasterClient client = new Proxy.MasterClient();
            MessageBox.Show(client.getNameFromStudent("ABdi"));
        }
    }
}

这不起作用并引发异常

System.ServiceModel.FaultException`1 [System.ServiceModel.ExceptionDetail]:

在ServiceModel客户端配置部分中找不到引用合同'Proxy2.IStudent'的默认终结点元素。 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

(故障详细信息等于ExceptionDetail,可能由IncludeExceptionDetailInFaults = true创建,其值为:
System.InvalidOperationException:在ServiceModel客户端配置部分中找不到引用合同'Proxy2.IStudent'的默认终结点元素。 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

在System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint,字符串configurationName)
在System.ServiceModel.ChannelFactory.ApplyConfiguration处(字符串configurationName,配置配置)
在System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
在System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串configurationName,EndpointAddress地址)
在System.ServiceModel.ChannelFactory`1..ctor(字符串endpointConfigurationName,EndpointAddress remoteAddress)
在System.ServiceModel.Configu ...)。

我需要帮助

我没有看到你指定服务和客户端地址(我没有看到前主机=新的ServiceHost(新Service.BossService() 的baseUrl);或新Proxy.MasterClient(endpointConfiguraionName)或Proxy.MasterClient(绑定,baseAddress))。 如果您到处都在使用配置文件(其中有地址),则不需要任何其他步骤来设置服务(Behaviors.Add,... AddServiceEndpoint)-所有服务创建步骤都将通过config自动执行。

我的建议:

1)删除所有服务托管代码,仅使用基于配置文件的配置(包括基地址,如果在* .svc中托管,则可以是相对地址,也可以是绝对地址)。 这是一种更加灵活和便捷的方式(如果您有可能使用配置文件,有时甚至没有),我已经使用了很多年。 参见简单的wcf配置示例

2)使用单调实例(更可预测)。 最终,您将只有一行代码,例如Host = new ServiceHost(new MyService())

3)不要使用生成的(svcutil)wcf客户端代码。 只需在服务和客户之间共享合同库(服务合同,数据合同)即可。 然后使用ChannelFactory调用服务方法: Channel = new ChannelFactory<MyService>("bindingConfigurationName").CreateChannel()new ChannelFactory<MyService>(binding, serviceAddress).CreateChannel() 这很好,足以同步调用服务方法(通过做一些额外的工作,您甚至可以通过了解和使用简单的同步服务接口来异步调用服务方法!)

4)不要使用WSDualHttpBinding-它不能正常工作(至少在Internet和防火墙上)。 我建议使用tcpbinding(本质上是双工的,几乎在任何地方都可以使用)。 但是,如果您使用WSDualHttpBinding-为什么没有双向方法(双工协定,请参见示例 )?

5)使用标准客户端(例如wcftestclient,SoapUI甚至是提琴手或邮递员)测试您的服务(用于RESTful服务)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM