我正在编写WCF服务 - 客户端。该服务将端点URL作为arg,这是Windows Form Application 服务impl是:
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,我给它地址,然后它侦听给定的地址,并将接口上的方法暴露给不同的客户端。
错误:
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 :(得分: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上输入Interface。