[英]Could not find default endpoint element that references contract - Hosting wcf
[英]Could not find default endpoint element that references contract (simple WCF Service not working)
完整的錯誤消息:
在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();
}
因此,事實證明,此示例中的問題是(因為我沒有使用Web配置),所以TextMessageClient沒有端點 。 (本地主機有一個,但沒有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;
}
}
您的基地址指定了服務頁面(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)的功能,並且在以前的版本中不可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.