c# - Microsoft WCF 测试客户端错误

标签 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;
         }  
     }
}

网络配置

    <?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>

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

Error: Cannot obtain Metadata from http://localhost:2049/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:2049/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost:2049/Service1.svc'. The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error

最佳答案

创建一台Host,并在Host程序中添加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();
            }
        }

关于c# - Microsoft WCF 测试客户端错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32708566/

相关文章:

c# - 我可以有一个接口(interface)参数,通过引用传递吗?

c# - 使用单一方法类——最好的方法?

c++ - 在我的程序中使用for循环打印出矩阵的一个错误

c# - 如何让自动生成的文件识别它自己创建的方法?

.NET WCF 无法解码 "Chunked"响应

c# - 使用 HttpClient.GetAsync() 使用具有基本身份验证的 WCF REST 服务会导致 (401) 未经授权

c# - 具有数据库列名称到实际列值的字符串参数

c# - 这个能写的更好吗?根据之前的结果调用函数

C++ 是否有任何关于如何使用句柄的教程?

asp.net - aspNet兼容性 WCF 和 WinForm