簡體   English   中英

單例並在測試中運行時如何調試WCF調用

[英]How to debug a WCF call when it's a singleton and being run in a test

我正在自行托管WCF服務。 在編寫一個測試程序來測試我的客戶端是否正在接收來自服務的消息時,我發現我無法調試服務本身。 因此,我做了一個簡單的例子,似乎可以幫助我重復這個問題。 這是我正在嘗試的測試的一個示例:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        ServiceRunner.Run(null);
        var client = new ServiceReference1.Service1Client();
        var result = client.GetData(11);
        Assert.IsNotNull(result);
        ServiceRunner.Host.Close();
    }
}

ServiceRunner將單例托管WCF合同。 客戶端來自指向自托管服務的服務引用。 當我調用GetData(11)時 ,得到一個響應,只是我的服務中的斷點從未命中。 這是為什么?

為了實現完整性,這是該服務的實現:

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

namespace CanYouDebugThis
{

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
    }

    [ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.Single)]
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            Console.WriteLine($"Get data with {value}");
            return string.Format("You entered: {0}", value);
        }
    }

    public class ServiceRunner
    {
        public static ServiceHost Host;
        public static void Run(String[] args)
        {
            var serviceInstance = new Service1();
            Uri baseAddress = new Uri("http://localhost:8080/hello");

            Host = new ServiceHost(serviceInstance, baseAddress);
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            Host.Description.Behaviors.Add(smb);
            Host.Open();
        }
    }
}

托管服務有問題。 我們應該添加服務端點和MEX端點以交換元數據。 請參考以下代碼段。

public static ServiceHost Host;
        public static void Main(String[] args)
        {
            var serviceInstance = new Service1();
            Uri baseAddress = new Uri("http://localhost:8080/hello");
            BasicHttpBinding binding = new BasicHttpBinding();
            //Host = new ServiceHost(serviceInstance, baseAddress);
            Host = new ServiceHost(typeof(Service1), baseAddress);
            Host.AddServiceEndpoint(typeof(IService1), binding, "");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            Host.Description.Behaviors.Add(smb);

            Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
            Host.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
            Host.Open();
            Console.WriteLine("Service is ready...");
            //pause, accepting a word would teminate the service.
            Console.ReadLine();
            Host.Close();
            Console.WriteLine("Service is closed....");
        }

請先將服務托管在一個單獨的Console項目中。 然后在客戶端,我們通過添加服務引用來生成客戶端代理。 請注意自動生成的服務端點,該端點應與實際的服務器端點相對應。
結果。
在此處輸入圖片說明
請隨時告訴我是否有什么我可以幫助的。
更新。

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        ServiceRunner.Run(null);
        ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
        var result = client.GetData(34);
        Assert.IsNotNull(result);
    }
}
[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}

[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.Single)]
public class Service1 : IService1
{
    public string GetData(int value)
    {
        Console.WriteLine($"Get data with {value}");
        return string.Format("You entered: {0}", value);
    }
}

public class ServiceRunner
{
    public static ServiceHost Host;
    public static void Run(String[] args)
    {
        var serviceInstance = new Service1();
        Uri baseAddress = new Uri("http://localhost:8080/hello");

        Host = new ServiceHost(serviceInstance, baseAddress);
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

        smb.HttpGetEnabled = true;
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        Host.Description.Behaviors.Add(smb);
        Host.Open();
    }
}

結果。
在此處輸入圖片說明
最后,請注意自動生成的客戶端端點地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM