打开时的Localhost-空白页面

时间:2015-08-11 18:40:35

标签: c# wcf localhost

我正在关注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);
    }  
}

CalculatorService的

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文件中的地址(或默认保留),它永远不会工作(提到的链接 - 尽管服务器正常启动)

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

该服务没有任何暗示它会返回一个网页。它是一个服务 Web服务,可以执行操作。

使用{Visual Studio Install Directory}\Common7\IDE\WcfTestClient.exe处的WCF测试客户端对其进行测试。运行后,选择Add Service,然后在http://localhost:8000/GettingStarted/CalculatorService.svc

中输入对话框

如果没有错误消息,它将显示界面定义,例如我的下面:

enter image description here

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