求高手指导,本人小白。
报错
服务“Services.HelloService”有零个应用程序(非基础结构)终结点。这可能是因为未找到应用程序的配置文件,或者在配置文件中未找到与服务名称匹配的服务元素,或者服务元素中未定义终结点。
运行代码
class Program { static void Main(string[] args) { //ServiceHost host = new ServiceHost(typeof(HelloWorld), new Uri("http://localhost:8080/HelloService")); //host.AddServiceEndpoint(typeof(IHello),new BasicHttpBinding(),"SVC"); //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) //{ // ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); // behavior.HttpGetEnabled = true; // behavior.HttpGetUrl = new Uri("http://localhost:8080/HelloService"); // host.Description.Behaviors.Add(behavior); //} //host.Open(); //Console.WriteLine("Start Your Service."); //Console.ReadKey(); //host.Close(); ServiceHost host1 = new ServiceHost(typeof(HelloService)); host1.Open(); Console.WriteLine("Hello service start"); Console.ReadLine(); host1.Close(); } }
这是Contract类
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace Contracts { [ServiceContract] public interface IHello { [OperationContract] void Hello(); } }
这是Service代码
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using Contracts; namespace Services { public class HelloService : IHello { public void Hello() { Console.WriteLine("Hello service hello"); } } }
这是config文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Services.HelloService"> <endpoint address="http://localhost:8080/HelloService" binding="basicHttpBinding" contract="Contracts.IHello"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/HelloService" /> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
<endpoint address="" binding="basicHttpBinding" contract="Artech.WCFService.Contract.IGeneralCalculator">
</endpoint>
明显你没有配置contract 属性
配置了啊,
contract="Contracts.IHello"
@瓜&瓜:
<service name="Services.HelloService"> <endpoint address="HelloService" binding="basicHttpBinding" contract="Contracts.IHello"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/" /> </baseAddresses> </host> </service>
ServiceHost host1 = new ServiceHost(typeof(HelloService), new Uri("http://localhost:8080/HelloService"));
这样就可以了,不是可以用相对的uri吗?
@瓜&瓜:
<baseAddresses>
<add baseAddress="http://192.168.17.169:8080/MailSys"/>
</baseAddresses>
<endpoint address="" binding ="basicHttpBinding" contract="MailSys.IMailSys"></endpoint>
这个是配置在
endpoint 前面的,你也试试