【发布时间】:2016-09-02 06:28:46
【问题描述】:
我正在学习 WCF,我开始创建一个非常基本的主机应用程序,它使用如下方法定义一个类:
[ServiceContract()]
public interface IMath
{
[OperationContract]
int Add(int a, int b);
}
public class MathCalcs : IMath
{
public MathCalcs()
{
Console.WriteLine("Service await two numbers...");
}
public int Add(int a, int b)
{
return a + b;
}
}
这就是我配置 App.config 文件的方式:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ConsoleHost.MathCalcs" behaviorConfiguration="MathServiceMEXBehavior">
<endpoint address="http://localhost:8080/MathCalcs"
binding="basicHttpBinding"
contract="ConsoleHost.IMath"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/MathCalcs"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MathServiceMEXBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
然后我从Main调用服务
using (ServiceHost host = new ServiceHost(typeof(MathCalcs)))
{
host.Open();
Console.WriteLine("***The service is ready***");
}
Console.ReadLine();
但它无法通过 URI http://localhost:8080/MathCalcs 查看服务的元数据,我确信我正在按照我正在阅读的书的步骤操作,并且前面的示例工作正常,唯一的区别是我没有在一个独立的类库中分离服务逻辑(接口和类)。 我错过了什么?
【问题讨论】: