【发布时间】:2014-08-29 14:44:35
【问题描述】:
我有一个项目,我正在尝试通过 TCP 传输实现一个简单的 WCF 服务。这就是下面共享的服务端代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
namespace HwWcfService
{
class Program
{
static void Main(string[] args)
{
string Uri = string.Format("net.tcp://localhost:8000/TelemetryProviderService");
ServiceHost host = new ServiceHost(
typeof(Calculator),
new Uri(Uri)
);
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
host.AddServiceEndpoint(
typeof(ICalc),
binding,
"CalcService");
host.Open();
Console.WriteLine("Done starting the new endpoint...");
Console.ReadKey();
}
}
class Calculator :ICalc
{
public int Add(int x, int y)
{
Console.WriteLine(x + " " + y);
return x + y;
}
}
}
namespace HwWcfService
{
[ServiceContract(
Namespace="HwWcfService"
)]
interface ICalc
{
[OperationContract]
int Add(int x, int y);
}
}
App.Config 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="HwWcfService.Calculator">
<endpoint address="net.tcp://localhost:8000/TelemetryProviderService"
name="TelemetryProviderService"
contract="HwWcfService.ICalc"
binding="netTcpBinding"
bindingConfiguration="calcTcpBindingConfiguration"
behaviorConfiguration="epBehaviourConfig"
/>
<endpoint address="net.tcp://localhost:8000/TelemetryProviderServiceMex"
name ="TelemetryProviderServiceMex"
binding="mexTcpBinding"
contract="HwWcfService.ICalc"
listenUriMode="Explicit"
/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="calcTcpBindingConfiguration" closeTimeout="00:02:00"/>
</netTcpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="epBehaviourConfig">
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetUrl="false" httpGetEnabled="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
当我运行 svcutil.exe 来查询所有参数时,我得到了错误: 错误:无法从 net.tcp://localhost:8000/TelemetryProviderService 获取元数据
如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在 spe 启用元数据发布 化地址。如需启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455 的 MSDN 文档。
WS-元数据交换错误 URI:net.tcp://localhost:8000/TelemetryProviderService
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8000/TelemetryProviderService'.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote hos
,或底层网络资源问题。本地套接字超时为 '00:04:59.9929977'。
An existing connection was forcibly closed by the remote host
有人知道我为什么会收到这个错误吗?
【问题讨论】: