1

我正在编写 WCF 服务 - 客户端。该服务将端点 url 获取为 arg,这是 Windows 窗体应用程序。
服务实现是:

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

1 回答 1

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 上输入接口。

于 2013-03-18T14:02:27.857 回答