我正在尝试构建一个相当基本的 WCF SOAP Web 服务,作为 Windows 服务自托管。Windows 服务本身已在我的机器上启动并运行——我只是无法通过 Visual Studio 或 Web 浏览器在本地访问它。
相关的 C# 代码如下。假设MyDummyService
执行合同IDummyService
:
public class Program : ServiceBase
{
private ServiceHost host = null;
private readonly Uri baseAddress = new Uri("http://localhost:8000/DummyAPI");
public static readonly ILog log = LogManager.GetLogger(typeof(Program));
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
public Program()
{
this.ServiceName = "DummyService";
}
protected override void OnStart(string[] args)
{
log.Info("Starting service");
try
{
base.OnStart(args);
host = new ServiceHost(typeof(MyDummyService), baseAddress);
host.Open();
}
catch (Exception ex)
{
log.Error(ex.ToString());
}
finally
{
if (host != null)
((IDisposable)host).Dispose();
}
}
protected override void OnStop()
{
log.Info("Stopping service");
base.OnStop();
host.Close();
}
}
相关app.config
:
<system.serviceModel>
<services>
<service name="DummyAPI.MyDummyService"
behaviorConfiguration="MyDummyBehavior">
<endpoint
address=""
binding="basicHttpBinding"
contract="DummyAPI.IDummyService" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyDummyBehavior">
<serviceMetadata httpGetEnabled="True" policyVersion="Policy15"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
当我访问
http://localhost:8000/DummyAPI
或者
http://localhost:8000/DummyAPI/MyDummyService
(或后面跟着 ?wsdl 的任何一个)在网络浏览器中,我得到一个 404。显而易见的第一个问题:我在上面搞砸了什么?
web.config
命名空间(或看起来像命名空间的东西)让我有点困惑。我可以在现场安全地弥补什么,以及什么需要反映 C# 类命名空间?