2016-11-25 142 views
0

这是我建的测试服务调用另一个服务Windows窗体应用程序调用WCF服务和WCF服务调用另一个WCF服务

这是得到由其他服务调用的服务的示例项目

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

(故障详细等于一个ExceptionDetail,可能是由IncludeExceptionDetailInFaults =真,其价值是创造找不到默认终结点元素引用合同“Proxy2.IStudent”在ServiceModel客户端配置节。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到匹配此合同的端点元素。 )
at System.ServiceModel.ChannelFactory.ApplyConfigurati上(字符串configurationName,配置结构)
在System.ServiceModel.ChannelFactory.ApplyConfiguration(字符串configurationName)
在System.ServiceModel.ChannelFactory.InitializeEndpoint(字符串configurationName,的EndpointAddress地址)
在System.ServiceModel.ChannelFactory`1。 。(字符串endpointConfigurationName,EndpointAddress remoteAddress)
at System.ServiceModel.Configu ...)。

我需要帮助,请

+0

哪一个是抛出异常?客户? – Prajwal

+0

是的。客户端无法找到Proxy2.IStudent服务合同,但客户端没有拨打该服务的主服务。 –

回答

0

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

我的建议:

1)删除所有服务托管代码,只能使用基于文件的config配置(包括基本地址,如果你是托管在可相对* .SVC或绝对的)。这是更加灵活和方便的方式(如果你有使用配置文件的可能性,有时候你没有) - 我用了很多年。见simple wcf configuration example

2)使用singletone实例(更可预测)。 最后,你将只有一行代码,如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服务))测试服务。