2011-08-24 94 views
0

默認終結點元素完整的錯誤消息:找不到引用合同(不工作簡單的WCF服務)

找不到默認終結點元素,在ServiceModel客戶端配置的參考合同 「SampleProject.ITextMessage」 部分。這可能是因爲沒有爲您的應用程序找到配置文件 ,或者因爲在客戶端元素中找不到與此 合同匹配的端點元素。

這是一個控制檯應用程序。無法從這個簡單得多。

在這種情況下沒有應用程序配置需要,只是想獲得一個簡單的示例。我幾乎拿起這裏的代碼:http://msdn.microsoft.com/en-us/library/ms731758.aspx

任何幫助,將不勝感激。

public class SampleProject 
    { 
     static void Main(string[] args) 
     { 
      var baseAddr = new Uri("http://localhost:6000/TextMessageSvc.svc"); 

      using (var localHost = new ServiceHost(typeof(TextMessageClient), baseAddr)) 
      { 
       try 
       { 
        //THIS page says an endpoint is not needed, a default will be created automatically: 
        //http://msdn.microsoft.com/en-us/library/ms731758.aspx 
        localHost.AddServiceEndpoint(typeof(ITextMessage), 
               new WSHttpBinding(), 
               "TextMessageSvc"); 

        var behavior = new ServiceMetadataBehavior(); 
        behavior.HttpGetEnabled = true; 
        behavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 

        localHost.Description.Behaviors.Add(behavior); 
        localHost.Open(); 

        Console.WriteLine("Service initialized."); 

        //************** Blows up on this line *********************** 
        var x = new TextMessageClient(); 
        x.SendTextMessage(); 

        Console.WriteLine("Press the ENTER key to terminate service."); 
        Console.ReadLine(); 

        localHost.Close(); 
       } 
       catch (CommunicationException ex) 
       { 
        Console.WriteLine("Oops! Exception: {0}", ex.Message); 
        localHost.Abort(); 
       } 
      } 
     } 
    } 

    public class TextMessageClient : ClientBase<ITextMessage>, ITextMessage 
    { 
     public void SendTextMessage() 
     { 
      base.Channel.SendTextMessage(); 
     } 
    } 

    [ServiceBehavior] 
    public class TextMessageSvc : ITextMessage 
    { 
     public TextMessageSvc() 
     { 

     } 

     [OperationBehavior] 
     public void SendTextMessage() 
     { 

     } 
    } 

    [ServiceContract] 
    public interface ITextMessage 
    { 
     [OperationContract] 
     void SendTextMessage(); 
    } 

回答

1

所以事實證明,這個例子中的問題是(因爲我沒有使用web配置),TextMessageClient沒有端點。 (localhost有一個,但不是TextMessageClient)當我調用SendTextMessage時,它將如何進行通信?是誰去跟??

嗯,我可以創建一個端點並將其傳遞到TextMessageClient中,或者只是在其構造函數中創建一個,我現在正在這樣做。

所以在重命名TextMessageSvc到TextMessageProxy然後重構。 這是我現在需要(如果你發現新的代碼僅消耗服務,現在,而不是試圖主機之一,也是:

我現在有地方的功能,設置了一個端點對於TextMessageClient使用

var uri = _appSettings.Get("FredServiceURI"); //"http://localhost:3333/FredService.svc/" 
var wcfSendTextMessage = new TextMessageProxy(uri); 

的TextMessage的類現在提供了自己的終點:

public class TextMessageProxy : ClientBase<IFredContract>, IFredContract 
    { 
     public TextMessageProxy(string url, WebHttpSecurityMode securityMode = WebHttpSecurityMode.None) 
      : base(ConstructEndpoint(url, securityMode)) 
     { 
     } 

     public string SendTextMessage(string sendToPhoneNumber, string messageText) 
     { 
      return base.Channel.SendTextMessage(sendToPhoneNumber, messageText); 
     } 

     // This method constructs a WebHttpBinding endpoint with all the appropriate 
     // settings for talking to our services. 
     private static ServiceEndpoint ConstructEndpoint(string serviceUri, WebHttpSecurityMode securityMode) 
     { 
      var contract = ContractDescription.GetContract(typeof(IFredContract)); 
      var binding = new WebHttpBinding(securityMode); 

      var address = new EndpointAddress(serviceUri); 
      var endpoint = new ServiceEndpoint(
       contract, 
       binding, 
       address); 

      //custom stuff. You may or may not need these settings, but I do.. 
      //I would think you need at least the behavior, as it is likely required, as it is in the web.config. 
      //------------- 
      var webHttpBehavior = new WebHttpBehavior() 
      { 
       FaultExceptionEnabled = true 
      }; 

      endpoint.Behaviors.Add(webHttpBehavior); 
      //------------- 

      return endpoint; 
     } 
    } 
0

您的基地址是指定一個服務頁面(TextMessageSvc.svc),所以我懷疑它是有效的基地址。所以,當你添加端點,你得到這樣的事情:

`http://localhost:6000/TextMessageSvc/TestMessage.svc/extMesssageSvc")` 

試試這個:

var baseAddr = new Uri("http://localhost:6000/TextMessageSvc"); 

而且,請注意,默認端點(如你似乎可以用它們)是WCF 4.0(.NET 4.0/VS 2010)的功能,並且在以前的版本中不可用。