【问题标题】:windows form application calling a WCF service and the WCF service calling another WCF serviceWindows 窗体应用程序调用 WCF 服务和 WCF 服务调用另一个 WCF 服务
【发布时间】:2017-04-09 09:43:56
【问题描述】:

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

这是被其他服务调用的服务

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”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。

(Fault Detail等于An ExceptionDetail,可能由IncludeExceptionDetailInFaults=true创建,其值为:
System.InvalidOperationException:在 ServiceModel 客户端配置部分中找不到引用合同“Proxy2.ISTudent”的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端元素中找不到与此协定匹配的端点元素。

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

我需要帮助

【问题讨论】:

  • 哪个抛出异常?客户?
  • 是的。客户端找不到 Proxy2.IStudent 服务合同,但客户端没有调用 Master 服务所在的服务。

标签: c# wcf


【解决方案1】:

我看不到您在哪里为服务和客户端指定地址(例如,我看不到 Host = new ServiceHost(new Service.BossService(), baseUrl); 或 new Proxy .MasterClient(endpointConfiguraionName) 或 Proxy.MasterClient(binding, baseAddress))。 如果您在任何地方都使用配置文件(其中包含地址),那么您不需要任何额外的步骤来设置服务(Behaviors.Add,...AddServiceEndpoint) - 所有服务创建步骤都将使用 config 自动执行。

我的建议:

1) 删除所有服务托管代码,仅使用基于配置文件的配置(包括基地址,如果您在 *.svc 中托管,则可以是相对的或绝对的)。这是更加灵活和方便的方式(如果您有可能使用配置文件,因为有时您没有) - 我使用了多年。见simple wcf configuration example)

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

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

4) 不要使用 WSDualHttpBinding - 它不能正常工作(至少在互联网和防火墙上)。我推荐 tcpbinding (本质上是双工,几乎在任何地方都很好用)。但是如果您使用 WSDualHttpBinding - 为什么没有双向方法(双工合同,请参阅example)?

5) 使用标准客户端测试您的服务,例如 wcftestclient、SoapUI,甚至 fiddler 或 postman(用于 RESTful 服务)。

【讨论】:

    猜你喜欢
    • 2011-01-31
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2013-09-30
    • 1970-01-01
    • 2011-04-20
    • 2014-12-01
    相关资源
    最近更新 更多