【发布时间】:2015-08-22 14:46:35
【问题描述】:
我需要编写不会由 iis 托管的简单 Web 服务 => 所以我使用简单的控制台应用程序作为主机。
我创建了 2 个项目
1) 包含 IService.cd 和 Service.cs 的 dll
2)将主机的控制台应用程序。
我剩下的所有步骤都和here一样
但是当我运行我的应用程序时——当我将主机定义为 IIS 时,我无法看到来自其他机器的服务。
如何解决?
配置文件:
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="True"/>
<services>
<service name="CalcService.Calc" behaviorConfiguration="NewBehavior0">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9003/Calc"/>
<add baseAddress="net.tcp://localhost:9002/Calc"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:9003/Calc" binding="basicHttpBinding" contract="CalcService.ICalc"/>
<!--<endpoint address="net.tcp://localhost:9002/Calc" binding="netTcpBinding" contract="CalcService.ICalc"/>-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
static void Main(string[] args)
{
ServiceHost svcHost = null;
try
{
svcHost = new ServiceHost(typeof(CalcService.Calc));
svcHost.Open();
Console.WriteLine("\n\nService is Running at following address");
Console.WriteLine("\nhttp://localhost:9001/Calc");
Console.WriteLine("\nnet.tcp://localhost:9002/Calc");
}
catch(Exception eX)
{
svcHost = null;
Console.WriteLine("Service can not be started \n\nError Message [" + eX.Message + "]");
}
if(svcHost != null)
{
Console.WriteLine("\nPress any key to close the Service");
Console.ReadKey();
svcHost.Close();
svcHost = null;
}
}
【问题讨论】:
-
显示有关您如何托管服务的代码。
-
添加主机服务-嵌入式