简体   繁体   English

Microsoft WCF 测试客户端错误

[英]Microsoft WCF test client Error

I am new to WCF services.我是 WCF 服务的新手。 I was browsing with some tutorials and got a simple program and tried to execute in WCF service application in c#.我浏览了一些教程,得到了一个简单的程序,并尝试在 C# 中的 WCF 服务应用程序中执行。 The code is shown below.代码如下所示。

IService1.cs服务1.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服务1.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>

when i run the application its giving the error as当我运行应用程序时,它给出的错误是

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.错误:无法从http://localhost:2049/Service1.svc获取元数据 如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。 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 '.有关启用元数据发布的帮助,请参阅 MSDN 文档,网址http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:2049/Service1.svc Metadata contains无法解析的引用:“ 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这可能是由于合同不匹配、会话过早关闭或内部服务器错误造成的

Create One Host and add appconfig file inside the host program.Then write the following code inside appconfig.创建 One 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>

Add Reference of the Wcfservice1 project to this project.create one class and write the following code将 Wcfservice1 项目的 Reference 添加到该项目中。创建一个类并编写以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM