【问题标题】:Localhost- blank page while opening本地主机 - 打开时出现空白页
【发布时间】:2015-08-11 18:40:34
【问题描述】:

我是按照 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>

EDIT2:

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

但是当我尝试地址时:http://localhost:8000/GettingStarted/(没有 CalculatorService,即使在 App.config 中这样说)它可以工作!

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

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

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

原来如此

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

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

【问题讨论】:

    标签: c# wcf localhost


    【解决方案1】:

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

    【讨论】:

    • 您的意思是在使用资源管理器打开它时更改它?如果是,那么我这样做了,但还是一样(Internet Explorer 抛出 http 错误 400)
    【解决方案2】:

    该服务没有任何迹象表明它会返回一个网页。它是一个执行操作的service Web 服务。

    使用位于{Visual Studio Install Directory}\Common7\IDE\WcfTestClient.exe 的 WCF 测试客户端对其进行测试。一旦运行 selectAdd Service 并在对话框中输入http://localhost:8000/GettingStarted/CalculatorService.svc

    如果它没有错误消息,它将显示接口定义,如下面的:

    然后在应用程序中您可以测试不同的方法,例如 Add 在您的情况下,然后 Invoke 操作。

    【讨论】:

    • 它给了我一个错误,但你能读懂我的“Edit2”吗?也许你会知道为什么会这样?而且 WCFTestClient 总是给我一个错误..
    猜你喜欢
    • 2021-12-06
    • 2021-10-13
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    相关资源
    最近更新 更多