gpt4 book ai didi

c# - Localhost- 打开时出现空白页

转载 作者:行者123 更新时间:2023-11-30 17:42:28 25 4
gpt4 key购买 nike

我正在关注 MSDN tutorial创建 WCF 服务,当我运行它时,它已正确启动,但是当我跟随 localhost 时,服务器是空白的。

我能做什么?

我正在使用 Chrome,我已经按照许多帖子中的建议更改了设置。

ICalculator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace GettingStartedLib
{
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Substract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}

计算器服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace GettingStartedLib
{
public class CalculatorService:ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Otrzymano: Add({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}

public double Substract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Otrzymano: Substract({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}

public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Otrzymano: Multiply({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}

public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Otrzymano: Divide({0},{1})", n1, n2);
Console.WriteLine("Wynik: {0}", result);
return result;
}
}
}

主持人:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;
using GettingStartedLib;

namespace GettingStartedHost
{
class Program
{
static void Main(string[] args)
{
//Krok 1: utworzenie URI, które będzie służyło jako adres bazowy
Uri uri = new Uri("http://localhost:8000/GettingStarted/");


//Krok 2: utworzenie instancji obiektu ServiceHost (hosta serwera)
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), uri);


try
{
//Krok 3: dodanie punktu końcowego serwera
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");

//Krok 4: umożliwienie wymiany metadanych
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

//Krok 5: uruchomienie hosta serwera
selfHost.Open();
Console.WriteLine("Serwer jest gotowy!");
Console.WriteLine("Naciśnij <ENTER>, by go zamknąć.");
Console.WriteLine();
Console.ReadLine();

//zamknięcie obiektu ServiceHost w celu zamknięcia serwera
selfHost.Close();

}
catch (CommunicationException e)
{
Console.WriteLine("Wystąpił wyjątek: {0}",e.Message);
selfHost.Abort();
}
}
}
}

库的 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="GettingStartedLib.CalculatorService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.ICalculator">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>

编辑 2:

我按照@OmegaMan 的建议使用 WCFTestClient 对其进行了测试。它给了我一个错误。

但是当我尝试地址时:http://localhost:8000/GettingStarted/(没有 CalculatorService,尽管在 App.config 中是这样说的)它有效!

这是为什么呢?这很奇怪。

编辑 3:好的,我发现我需要在 *.config 文件中添加“/”:

<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />

就这样

<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService/" />

但是有人能告诉我为什么我的编程方法不起作用吗?我的意思是,如果我更改 *.config 文件中的地址(或默认保留它),它永远不会工作(提到的链接 - 尽管服务器正常启动)

最佳答案

尝试将 localhost 更改为实际的计算机名称或 IP。我的 WCF 服务遇到了类似的问题

关于c# - Localhost- 打开时出现空白页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31949554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com