繁体   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