2013-03-18 96 views
1

我正在編寫WCF服務 - 客戶端。該服務正在將端點URL作爲arg獲取,這是Windows窗體應用程序。
的服務IMPL是:WCF服務錯誤無配置文件或端點.net 3.5

BaseAddress = new Uri(args[0]); 
using (ServiceHost host = new ServiceHost(typeof(DriverService), BaseAddress)) 
{ 
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
    smb.HttpGetEnabled = true; 
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
    host.Description.Behaviors.Add(smb); 
//host.AddServiceEndpoint(typeof(DriverService), new BasicHttpBinding(), BaseAddress); 
    host.Open(); 

後host.Open()我得到的錯誤,當我寫了另一種解決方案相同的WCF的代碼,它的工作就好了,我有客戶端和服務。現在我只需要等到客戶端連接時纔打開服務。

據我所知,這是服務,我給它的地址,然後它監聽給定的地址,並暴露接口上的方法,以不同的客戶端。

錯誤:

接口和類的
Service 'DriverHost.DriverService' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element. 

代碼:

[ServiceContract] 
public interface IDriverService 
{ 
    [OperationContract] 
    string WhoAmI(); 
} 

public class DriverService : IDriverService 
{ 
    public string WhoAmI() 
    { 
     return string.Format("Im on port !"); 
    } 
} 
+0

你有綁定在你的app.config – DaveHogan 2013-03-18 10:28:03

+0

我沒有在我的app.config,但在我做的樣本,我沒有寫任何綁定 – ilansch 2013-03-18 10:31:23

+0

這可能是重複http://stackoverflow.com/questions/2807525/wcf-self-host-service-endpoints-in -c-sharp?rq = 1 ...檢查答案... – ilansch 2013-03-18 10:40:45

回答

2

,因爲有不同的.NET 4.5和3.5之間我明白沒有默認的端點。
所以需要聲明:

BaseAddress = new Uri(args[0]); 
using (ServiceHost host = new ServiceHost(typeof(Namespace.Classname), BaseAddress)) 
{ 
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
    smb.HttpGetEnabled = true; 
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
    host.Description.Behaviors.Add(smb); 
    host.AddServiceEndpoint(typeof(Namespace.IInterface), new BasicHttpBinding(), args[0]); 
    host.Open(); 

意義 - 添加.AddServiceEndpoint(..) 並確保在ServiceHost的()寫的類,並且在AddServiceEndpoint輸入接口。