C# 无法建立连接,因为目标计算机主动拒绝127.0.0.1:8000内部异常
我是WCF的新手,创建了一个包含2个项目的解决方案,一个是客户端应用程序,另一个是web服务。 当我编译web服务代码时,它不会出错:C# 无法建立连接,因为目标计算机主动拒绝127.0.0.1:8000内部异常,c#,wcf,C#,Wcf,我是WCF的新手,创建了一个包含2个项目的解决方案,一个是客户端应用程序,另一个是web服务。 当我编译web服务代码时,它不会出错: namespace Microsoft.ServiceModel.Samples { class Program { static void Main(string[] args) { // Step 1 of the address configuration procedure: Create a URI to serve
namespace Microsoft.ServiceModel.Samples
{
class Program
{
static void Main(string[] args)
{
// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8888/ServiceModelSamples/Service");
/* Step 2 of the hosting procedure: Create ServiceHost
* Use the ServiceHost class to configure and expose a service for use by client applications when you are not using Internet Information Services (IIS) to expose a service.
* IIS interacts with a ServiceHost object on your behalf.
*/
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
//WSHttpBinding is an interoperable binding that supports distributed transactions and secure, reliable sessions.
WSHttpBinding ws = new WSHttpBinding();
ws.Security.Mode = SecurityMode.Message; //Use SOAP message security
ws.Security.Message.ClientCredentialType = MessageCredentialType.Windows; //Use windows authentication
// Step 3 of the hosting procedure: Add a service endpoint.
// Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address.
// The contract is the definition of what functionality the web service offers (i.e. its API)
// The binding specifies how the service communicates (protocols, transports, and message encoders)
// The address is the name of the endpoint being added to this service host
selfHost.AddServiceEndpoint(
typeof(ICalculator),
ws,
"CalculatorService");
// Step 4 of the hosting procedure: Enable metadata exchange.
// Controls the publication of service metadata and associated information.
// HttpGetEnabled indicates whether to publish service metadata for retrieval using an HTTP/GET request.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
//add the service metadata behavior to the list of service host behaviors
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
Console.ReadLine();
}
}
我的app.config看起来像:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/ServiceModelSamples/Service"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
contract="CalculatorServiceReference.ICalculator" name="WSHttpBinding_ICalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>
专家能给我指出正确的方向吗?我尝试了许多不同的端点地址,但没有成功。提前谢谢 当您的客户端试图连接到端口8000时,您的服务器托管在8888上。更改服务器或客户端端口,使其匹配 看起来你希望它是8000,而不是8888
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
我测试了你的代码。我相信您需要像下面这样将服务添加到客户端app.config文件中的基本地址
端点地址=”http://localhost:8888/ServiceModelSamples/Service/CalculatorService“
当我编译web服务代码时,它运行时没有错误
仅供参考编译=/=“运行时没有错误”我已更正,您是对的,它确实编译了,但显然不会运行时没有错误,谢谢!我将app.config更改为引用端口8000,因此它们都匹配,但得到的结果是相同的:将其更改为8000,现在获取:没有可以接受消息的端点正在侦听。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。您将需要发布新问题!这是一个完全不同的错误,原因完全不同!
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");