2016-11-25 148 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服務))測試服務。