WCF服务错误,没有配置文件或端点.net 3.5

本文关键字:端点 net 配置文件 服务 错误 WCF | 更新日期: 2023-09-27 18:04:40

我正在编写WCF服务-客户端。服务正在获取端点url作为参数,这是Windows窗体应用程序。
Service的含义是:

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 !");
    }
}

WCF服务错误,没有配置文件或端点.net 3.5

由于。net 4.5和。net 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上输入接口。