2017-08-23 13 views
0

爲了證明概念,我試圖編寫兩個應用程序WcfApp1和WcfApp2。wcf應用程序之間的全雙工通信

問題: 我能夠在WcfApp2解決方案添加WcfApp1的服務引用,但無法添加WcfApp2在WcfApp1。雖然代碼是相同的(當然改變basic_httpbinding_url)。 任何人都可以告訴我這有什麼問題嗎?

詳情:

WcfApp1具有:

IWcfApp1_Class - >接口;

WcfApp1_Class - >從上面的接口派生的類;

WcfApp1_DClass - > class(as data structure);

WcfApp2具有:

IWcfApp2_Class - >接口;

WcfApp2_Class - >從上面的接口派生的類;

WcfApp2_DClass - > class(as data structure);

代碼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; 
       } 
      } 
     } 
    } 
} 

錯誤消息:

There was an error downloading ' http://localhost:9002/WcfApp2/ $metadata'. The request failed with HTTP status 405: Method Not Allowed. Metadata contains a reference that cannot be resolved: ' http://localhost:9002/WcfApp2 '. There was no endpoint listening at http://localhost:9002/WcfApp2 that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. The remote server returned an error: (404) Not Found. If the service is defined in the current solution, try building the solution and adding the service reference again.

enter image description here

+0

何時您收到錯誤信息? – Fildor

+0

當我嘗試使用Visual Studio添加服務引用。 –

+0

當你嘗試WcfApp2時,WcfApp2是否正在運行? – Fildor

回答

0

找到故障點。

我失蹤[ServiceContract]IWcfApp2_Class界面。