【发布时间】:2014-11-18 10:05:54
【问题描述】:
我正在开发一个基于 tcp 的 WCF 服务,并且在尝试在主机应用程序中启动该服务时遇到了这个臭名昭著的 WCF 异常:
System.ServiceModel.dll 中出现“System.InvalidOperationException”类型的未处理异常
附加信息:服务“WcfServiceWas.MathService”的应用程序(非基础设施)端点为零。这可能是因为找不到您的应用程序的配置文件,或者因为在配置文件中找不到与服务名称匹配的服务元素,或者因为在服务元素中没有定义端点。
这个问题之前已经描述过很多次了,建议的解决方案是完全按照类库中定义的方式命名服务,问题是这个解决方案对我不起作用。
托管服务拒绝启动Service的这个配置有什么问题?
服务接口(IMathService.cs):
namespace WcfServiceWas
{
[ServiceContract]
public interface IMathService
{
[OperationContract]
OperationResult Add(int x, int y);
[OperationContract]
int Subtract(int x, int y);
}
}
服务实现(MathService.cs):
namespace WcfServiceWas
{
public class MathService : IMathService
{
public OperationResult Add(int x, int y)
{
return new OperationResult { Result = new MyInteger { Value = x + y } };
}
public int Subtract(int x, int y)
{
return x - y;
}
}
}
Svc 文件(MathService.svc):
<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceWas.MathService" CodeBehind="MathService.cs" %>
Web.Config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceWas.MathServiceBehavior" >
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WcfServiceWas.MathService" behaviorConfiguration="WcfServiceWas.MathServiceBehavior" >
<endpoint contract="WcfServiceWas.IMathService" binding="netTcpBinding" address="" />
<endpoint contract="IMetadataExchange" binding="mexTcpBinding" address="mex" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:999/MathServiceX/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
服务主机:
class Program
{
static void Main(string[] args)
{
using (var myServiceHost = new ServiceHost(typeof(MathService)))
{
myServiceHost.Open();
}
}
}
更新:宿主应用程序的 app.config 也必须包含 wcf 配置。
【问题讨论】:
标签: c# .net web-services wcf tcp