Microsoft WCF测试客户端错误

时间:2015-09-22 04:46:38

标签: c# visual-studio-2010 wcf

我是WCF服务的新手。我浏览了一些教程,得到了一个简单的程序,并尝试在c#中的WCF服务应用程序中执行。代码如下所示。

IService1.cs

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

namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        int calculatedays(int day,int month,int year);
    }    
}

Service1.svc.cs

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

 namespace WcfService1
 {
     public class Service1 : IService1
     {
         public int calculatedays(int day, int month, int year)
         {
             DateTime dt = new DateTime(year, month, day);
             int datetodays = DateTime.Now.Subtract(dt).Days;
             return datetodays;
         }  
     }
}

的web.config

    <?xml version="1.0"?>
    <configuration>

    <system.web>
    <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
   </system.serviceModel>
   <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  </configuration>

当我运行应用程序时,它给出了错误

  

错误:无法从http://localhost:2049/Service1.svc获取元数据   如果这是您的Windows(R)Communication Foundation服务   有权访问,请检查您是否已启用元数据发布   指定的地址。有关启用元数据发布的帮助,请   请参阅MSDN文档   http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata交流   错误URI:http://localhost:2049/Service1.svc元数据包含   无法解决的引用:   &#39; http://localhost:2049/Service1.svc&#39 ;.服务器没有提供   有意义的答复这可能是由合同不匹配造成的,a   过早会话关闭或内部服务器错误

1 个答案:

答案 0 :(得分:2)

创建一个主机并在主机程序中添加appconfig文件。然后在appconfig中编写以下代码。

 <?xml version="1.0"?>
    <configuration>

    <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    </system.web>
    <system.serviceModel>
    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="maxBehaviour">
        <endpoint address="WcfService1" binding="netTcpBinding" contract="WcfService1.IService1">
        </endpoint>


        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:2049/"/>
            <add baseAddress="net.tcp://localhost:8090/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
   <behaviors>
      <serviceBehaviors>
        <behavior name="maxBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

将Wcfservice1项目的引用添加到此项目中。创建一个类并编写以下代码

        public static void Main()
        {
            using (ServiceHost host = new ServiceHost(typeof(WcfService1.Service1)))
            {
                host.Open();
                Console.WriteLine("Started Report Host");
                Console.ReadKey();
            }
        }