这是我的代码,有两种宿主方式,我用的是宿主在控制台里面也就是自宿主,而且下面的代码也能启动起来,我是在启动的状态下添加service服务引用的,但是我添加服务的时候缺报错。请问是怎么回事?
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8087/'.
Could not connect to net.tcp://localhost:8087/. The connection attempt lasted for a time span of 00:00:01.9531250. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8087.
No connection could be made because the target machine actively refused it 127.0.0.1:8087
If the service is defined in the current solution, try building the solution and adding the service reference again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Cyclone.Chat.Contracts;
using Cyclone.Chat.Service;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;namespace Cyclone.Chat.Host
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("net.tcp://localhost:8087/");
using (ServiceHost host = new ServiceHost(typeof(ChatService), baseAddress))
{
BindingElement bindingElement = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElement); host.AddServiceEndpoint(typeof(IChat), binding, "ChatService");
ServiceMetadataBehavior metadataBehavior; metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
host.Open();
Console.Write("The service is ready,press any key to exit!");
Console.ReadLine();
}
}
}
}
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8087/'.
Could not connect to net.tcp://localhost:8087/. The connection attempt lasted for a time span of 00:00:01.9531250. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8087.
No connection could be made because the target machine actively refused it 127.0.0.1:8087
If the service is defined in the current solution, try building the solution and adding the service reference again.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Cyclone.Chat.Contracts;
using Cyclone.Chat.Service;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;namespace Cyclone.Chat.Host
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("net.tcp://localhost:8087/");
using (ServiceHost host = new ServiceHost(typeof(ChatService), baseAddress))
{
BindingElement bindingElement = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElement); host.AddServiceEndpoint(typeof(IChat), binding, "ChatService");
ServiceMetadataBehavior metadataBehavior; metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
host.Open();
Console.Write("The service is ready,press any key to exit!");
Console.ReadLine();
}
}
}
}
解决方案 »
- <关于C#读取EXCEL的问题>
- Trouble opening project
- C#的两个很小问题 望高人指点一下
- 在线等待,马上结贴,各位大哥求教啊,Windows程序里面我想双击treeView的一个节点,弹出一个窗口,并且只有双击子节点才弹出来,最顶的父节点
- winform用Data.OracleClient连接oracle数据库,可以直接通过ip地址连接么?即客户端不需要安装oracle客户端
- C# 事务Update,超难!!!!!!!!
- 100分求助~如何把整个数据库导入到一个DataSet中。
- 求助 回车焦点切换的问题
- C#调用WinHttp.WinHttpRequest.5.1 Get网页 乱码问题
- asp.net中crystal report转成excel格式问题,如何保证表格和数据格式不改变?急~~~
- 给定关键字不在字典中
- datagridview的列宽问题
<host>
<baseAddresses>
<add baseAddress="http://localhost:8087/ChatService"/>
</baseAddresses>
</host>当然,如果你完全使用代码来配置也可以,请测试以下代码:
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8087/ChatService");
using (ServiceHost host = new ServiceHost(typeof(ChatService), baseAddress))
{
BindingElement bindingElement = new TcpTransportBindingElement();
CustomBinding binding = new CustomBinding(bindingElement); host.AddServiceEndpoint(typeof(IChat), binding, "net.tcp://localhost:8087/ChatService");
ServiceMetadataBehavior metadataBehavior; metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metadataBehavior == null)
{
metadataBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metadataBehavior);
}
host.Open();
Console.Write("The service is ready,press any key to exit!");
Console.ReadLine();
}
}
HTTP could not register URL http://+:8087/ChatService/ because TCP port 8087 is being used by another application.
我一开始使用WCF的时候也是添加service服务引用,那是看着例子设置的app.config。后来发现可以通过代码设置的时候,已经不再添加service服务引用了,客户端直接代码设置,引用公共接口dll,这样效率很高,所以你这种写法我没有试过,你可以考虑将地址写在配置文件中。类似这样的配置文件节点,是可以正常添加引用的,引用地址:http://localhost:8000/ckth/service
<services>
<service name="ConsoleApplication1.DataManagerService"
behaviorConfiguration="DataManagerBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ckth/service"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at: net.tcp://localhost:9000/servicemodelsamples/service -->
<endpoint address="net.tcp://localhost:9000/ckth/service"
binding="netTcpBinding"
bindingConfiguration="Binding1"
contract="ConsoleApplication1.IDataManager" />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
static void Main(string[] args)
{
// Create a ServiceHost for the CalculatorService type.
using (ServiceHost serviceHost = new ServiceHost(typeof(DataManagerService)))
{
// Open the ServiceHost to create listeners and start listening for messages.
serviceHost.Open();
// The service can now be accessed. Console.WriteLine("服务已开启.");
Console.WriteLine("按 <ENTER> 终止服务.");
Console.WriteLine();
Console.ReadLine();
serviceHost.Close();
}
}
<configuration>
<connectionStrings> </connectionStrings>
<system.serviceModel>
<services>
<service name="ConsoleApplication1.DataManagerService"
behaviorConfiguration="DataManagerBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/ckth/service"/>
</baseAddresses>
</host>
<!-- this endpoint is exposed at: net.tcp://localhost:9000/servicemodelsamples/service -->
<endpoint address="net.tcp://localhost:9000/ckth/service"
binding="netTcpBinding"
bindingConfiguration="Binding1"
contract="ConsoleApplication1.IDataManager" />
<!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services> <bindings>
<!--
Following is the expanded configuration section for a NetTcpBinding.
Each property is configured with the default values.
See the Message Security, and Transport Security samples in the WS binding samples
to learn how to configure these features.
-->
<netTcpBinding>
<binding name="Binding1"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
transactionFlow="false"
transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
listenBacklog="10"
maxBufferPoolSize="1073741824"
maxBufferSize="1073741824"
maxConnections="10"
maxReceivedMessageSize="1073741824">
<readerQuotas maxDepth="32"
maxStringContentLength="1073741824"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession ordered="true"
inactivityTimeout="00:01:00"
enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
</security>
</binding>
</netTcpBinding>
</bindings> <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="DataManagerBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>