簡體   English   中英

Microsoft WCF 測試客戶端錯誤

[英]Microsoft WCF test client Error

我是 WCF 服務的新手。 我瀏覽了一些教程,得到了一個簡單的程序,並嘗試在 C# 中的 WCF 服務應用程序中執行。 代碼如下所示。

服務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);
    }    
}

服務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;
         }  
     }
}

網頁配置

    <?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 Exchange Error URI: http://localhost:2049/Service1.svc Metadata contains無法解析的引用:“ http://localhost:2049/Service1.svc ”。 服務器沒有提供有意義的回復; 這可能是由於合同不匹配、會話過早關閉或內部服務器錯誤造成的

創建 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>

將 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